From 3ed0e5e5d4af1ad2603fa1b87935cf65a27b083e Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 3 Feb 2017 00:01:45 +1100 Subject: py/objcomplex: Correctly handle case of 0j to power of something. 0j to the power of negative now raises ZeroDivisionError, and 0j to the power of positive returns 0. --- py/objcomplex.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'py/objcomplex.c') diff --git a/py/objcomplex.c b/py/objcomplex.c index 96be25255..4c05886f0 100644 --- a/py/objcomplex.c +++ b/py/objcomplex.c @@ -222,8 +222,8 @@ mp_obj_t mp_obj_complex_binary_op(mp_uint_t op, mp_float_t lhs_real, mp_float_t // = exp(x3)*(cos(y3) + i*sin(y3)) mp_float_t abs1 = MICROPY_FLOAT_C_FUN(sqrt)(lhs_real*lhs_real + lhs_imag*lhs_imag); if (abs1 == 0) { - if (rhs_imag == 0) { - lhs_real = 1; + if (rhs_imag == 0 && rhs_real >= 0) { + lhs_real = 1 ? rhs_real == 0 : 0; rhs_real = 0; } else { mp_raise_msg(&mp_type_ZeroDivisionError, "0.0 to a complex power"); -- cgit v1.2.3 From bd04ed3e8a4235664743fd4c452091a9ce603011 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 4 Feb 2017 00:23:56 +1100 Subject: py/objcomplex: Fix typo in ternary expression. This typo actually did the correct thing, but it was very obscure (came about from think in terms of Python's "x if cond else y" expression). --- py/objcomplex.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'py/objcomplex.c') diff --git a/py/objcomplex.c b/py/objcomplex.c index 4c05886f0..426e76e5f 100644 --- a/py/objcomplex.c +++ b/py/objcomplex.c @@ -223,7 +223,7 @@ mp_obj_t mp_obj_complex_binary_op(mp_uint_t op, mp_float_t lhs_real, mp_float_t mp_float_t abs1 = MICROPY_FLOAT_C_FUN(sqrt)(lhs_real*lhs_real + lhs_imag*lhs_imag); if (abs1 == 0) { if (rhs_imag == 0 && rhs_real >= 0) { - lhs_real = 1 ? rhs_real == 0 : 0; + lhs_real = (rhs_real == 0); rhs_real = 0; } else { mp_raise_msg(&mp_type_ZeroDivisionError, "0.0 to a complex power"); -- cgit v1.2.3