diff --git a/py/objcomplex.c b/py/objcomplex.c index 12f10ec10..8a424f7f2 100644 --- a/py/objcomplex.c +++ b/py/objcomplex.c @@ -59,7 +59,7 @@ STATIC void complex_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_ } else { mp_format_float(o->real, buf, sizeof(buf), 'g', 7, '\0'); mp_printf(print, "(%s", buf); - if (o->imag >= 0) { + if (o->imag >= 0 || isnan(o->imag)) { mp_print_str(print, "+"); } mp_format_float(o->imag, buf, sizeof(buf), 'g', 7, '\0'); @@ -73,7 +73,7 @@ STATIC void complex_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_ } else { sprintf(buf, "%.16g", (double)o->real); mp_printf(print, "(%s", buf); - if (o->imag >= 0) { + if (o->imag >= 0 || isnan(o->imag)) { mp_print_str(print, "+"); } sprintf(buf, "%.16g", (double)o->imag); diff --git a/tests/float/complex1.py b/tests/float/complex1.py index 239527109..ebe4b0f37 100644 --- a/tests/float/complex1.py +++ b/tests/float/complex1.py @@ -43,6 +43,11 @@ print("%.5g" % abs(1j + 2)) # float on lhs should delegate to complex print(1.2 + 3j) +# check printing of inf/nan +print(float('nan') * 1j) +print(float('inf') * (1 + 1j)) +print(float('-inf') * (1 + 1j)) + # convert bignum to complex on rhs ans = 1j + (1 << 70); print("%.5g %.5g" % (ans.real, ans.imag))