From 709955b601a0b7de7a85e91d97846660979904a7 Mon Sep 17 00:00:00 2001 From: stijn Date: Wed, 13 May 2015 21:35:58 +0200 Subject: [PATCH] py: Fix printing of complex number when imaginary part is nan --- py/objcomplex.c | 4 ++-- tests/float/complex1.py | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/py/objcomplex.c b/py/objcomplex.c index 12f10ec10b..8a424f7f26 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 2395271090..ebe4b0f37c 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))