summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2025-10-01 12:21:27 +1000
committerDamien George <damien@micropython.org>2025-10-01 23:59:15 +1000
commitdcbda765d1dc654cdcba06e0a01649256330794e (patch)
treebb540d2031bfe305a8a6ce70a5e38f02135871e1
parent381cd730c834a1d071e7e90787ef7354b8532142 (diff)
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>
-rw-r--r--py/modmath.c6
-rw-r--r--py/mpconfig.h1
-rw-r--r--py/objfloat.c4
3 files changed, 10 insertions, 1 deletions
diff --git a/py/modmath.c b/py/modmath.c
index 919a8ccd9..045c84215 100644
--- a/py/modmath.c
+++ b/py/modmath.c
@@ -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)
diff --git a/py/mpconfig.h b/py/mpconfig.h
index 45178034a..a6a21dd1c 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -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
diff --git a/py/objfloat.c b/py/objfloat.c
index 125b576fb..b0ad70de4 100644
--- a/py/objfloat.c
+++ b/py/objfloat.c
@@ -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;