summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-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;