mirror of
https://github.com/micropython/micropython.git
synced 2025-12-16 09:50:15 +01:00
py/modmath: Make MICROPY_PY_MATH_POW_FIX_NAN also fix pow(x, NaN) cases.
This is needed by the zephyr port. Combining it with the existing `MICROPY_PY_MATH_POW_FIX_NAN` option should be safe, and eliminates the need for a separate option. Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
@@ -99,12 +99,16 @@ mp_float_t MICROPY_FLOAT_C_FUN(log2)(mp_float_t x) {
|
||||
MATH_FUN_1(sqrt, sqrt)
|
||||
// pow(x, y): returns x to the power of y
|
||||
#if MICROPY_PY_MATH_POW_FIX_NAN
|
||||
mp_float_t pow_func(mp_float_t x, mp_float_t y) {
|
||||
mp_float_t MICROPY_FLOAT_C_FUN(pow_func)(mp_float_t x, mp_float_t y) {
|
||||
// pow(base, 0) returns 1 for any base, even when base is NaN
|
||||
// pow(+1, exponent) returns 1 for any exponent, even when exponent is NaN
|
||||
if (x == MICROPY_FLOAT_CONST(1.0) || y == MICROPY_FLOAT_CONST(0.0)) {
|
||||
return MICROPY_FLOAT_CONST(1.0);
|
||||
}
|
||||
// pow(base, NaN) returns NaN for any other value of base
|
||||
if (isnan(y)) {
|
||||
return y;
|
||||
}
|
||||
return MICROPY_FLOAT_C_FUN(pow)(x, y);
|
||||
}
|
||||
MATH_FUN_2(pow, pow_func)
|
||||
|
||||
@@ -1559,6 +1559,7 @@ typedef time_t mp_timestamp_t;
|
||||
#endif
|
||||
|
||||
// Whether to provide fix for pow(1, NaN) and pow(NaN, 0), which both should be 1 not NaN.
|
||||
// Also fixes pow(base, NaN) to return NaN for other values of base.
|
||||
#ifndef MICROPY_PY_MATH_POW_FIX_NAN
|
||||
#define MICROPY_PY_MATH_POW_FIX_NAN (0)
|
||||
#endif
|
||||
|
||||
@@ -301,6 +301,10 @@ mp_obj_t mp_obj_float_binary_op(mp_binary_op_t op, mp_float_t lhs_val, mp_obj_t
|
||||
lhs_val = MICROPY_FLOAT_CONST(1.0);
|
||||
break;
|
||||
}
|
||||
if (isnan(rhs_val)) {
|
||||
lhs_val = rhs_val;
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
lhs_val = MICROPY_FLOAT_C_FUN(pow)(lhs_val, rhs_val);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user