8000 py/objfloat: Fix handling of negative float to power of nan. · tve/micropython@8d5a40c · GitHub
[go: up one dir, main page]

Skip to content

Commit 8d5a40c

Browse files
committed
py/objfloat: Fix handling of negative float to power of nan.
Prior to this commit, pow(-2, float('nan')) would return (nan+nanj), or raise an exception on targets that don't support complex numbers. This is fixed to return simply nan, as CPython does. Signed-off-by: Damien George <damien@micropython.org>
1 parent 709398d commit 8d5a40c

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

py/objfloat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ mp_obj_t mp_obj_float_binary_op(mp_binary_op_t op, mp_float_t lhs_val, mp_obj_t
293293
if (lhs_val == 0 && rhs_val < 0 && !isinf(rhs_val)) {
294294
goto zero_division_error;
295295
}
296-
if (lhs_val < 0 && rhs_val != MICROPY_FLOAT_C_FUN(floor)(rhs_val)) {
296+
if (lhs_val < 0 && rhs_val != MICROPY_FLOAT_C_FUN(floor)(rhs_val) && !isnan(rhs_val)) {
297297
#if MICROPY_PY_BUILTINS_COMPLEX
298298
return mp_obj_complex_binary_op(MP_BINARY_OP_POWER, lhs_val, 0, rhs_in);
299299
#else

tests/float/inf_nan_arith.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Test behaviour of inf and nan in basic float operations
2+
3+
inf = float("inf")
4+
nan = float("nan")
5+
6+
values = (-2, -1, 0, 1, 2, inf, nan)
7+
8+
for x in values:
9+
for y in values:
10+
print(x, y)
11+
print(" + - *", x + y, x - y, x * y)
12+
try:
13+
print(" /", x / y)
14+
except ZeroDivisionError:
15+
print(" / ZeroDivisionError")
16+
try:
17+
print(" ** pow", x ** y, pow(x, y))
18+
except ZeroDivisionError:
19+
print(" ** pow ZeroDivisionError")
20+
print(" == != < <= > >=", x == y, x != y, x < y, x <= y, x > y, x >= y)

0 commit comments

Comments
 (0)
0