summaryrefslogtreecommitdiffstatshomepage
path: root/py/modmath.c
diff options
context:
space:
mode:
authorstijn <stijn@ignitron.net>2020-09-08 15:22:34 +0200
committerDamien George <damien@micropython.org>2020-09-11 10:04:57 +1000
commit2e54d9d146b34d7ad00e4394c9767f4319244cdf (patch)
treeb6d56e3d53b9fef5d5898964d7bdd086e23016d5 /py/modmath.c
parent8d5a40c86e384bf3cddb2f687374e0bb1ae6df7d (diff)
downloadmicropython-2e54d9d146b34d7ad00e4394c9767f4319244cdf.tar.gz
micropython-2e54d9d146b34d7ad00e4394c9767f4319244cdf.zip
py: Fix handling of NaN in certain pow implementations.
Adds a new compile-time option MICROPY_PY_MATH_POW_FIX_NAN for use with toolchains that don't handle pow-of-NaN correctly.
Diffstat (limited to 'py/modmath.c')
-rw-r--r--py/modmath.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/py/modmath.c b/py/modmath.c
index b312eeb3d2..b7948f39e7 100644
--- a/py/modmath.c
+++ b/py/modmath.c
@@ -98,7 +98,19 @@ mp_float_t MICROPY_FLOAT_C_FUN(log2)(mp_float_t x) {
// sqrt(x): returns the square root of 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) {
+ // 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);
+ }
+ return MICROPY_FLOAT_C_FUN(pow)(x, y);
+}
+MATH_FUN_2(pow, pow_func)
+#else
MATH_FUN_2(pow, pow)
+#endif
// exp(x)
MATH_FUN_1(exp, exp)
#if MICROPY_PY_MATH_SPECIAL_FUNCTIONS