Skip to content

Commit 709955b

Browse files
stinosdpgeorge
authored andcommitted
py: Fix printing of complex number when imaginary part is nan
1 parent 1db3577 commit 709955b

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

py/objcomplex.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ STATIC void complex_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_
5959
} else {
6060
mp_format_float(o->real, buf, sizeof(buf), 'g', 7, '\0');
6161
mp_printf(print, "(%s", buf);
62-
if (o->imag >= 0) {
62+
if (o->imag >= 0 || isnan(o->imag)) {
6363
mp_print_str(print, "+");
6464
}
6565
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_
7373
} else {
7474
sprintf(buf, "%.16g", (double)o->real);
7575
mp_printf(print, "(%s", buf);
76-
if (o->imag >= 0) {
76+
if (o->imag >= 0 || isnan(o->imag)) {
7777
mp_print_str(print, "+");
7878
}
7979
sprintf(buf, "%.16g", (double)o->imag);

tests/float/complex1.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
# float on lhs should delegate to complex
4444
print(1.2 + 3j)
4545

46+
# check printing of inf/nan
47+
print(float('nan') * 1j)
48+
print(float('inf') * (1 + 1j))
49+
print(float('-inf') * (1 + 1j))
50+
4651
# convert bignum to complex on rhs
4752
ans = 1j + (1 << 70); print("%.5g %.5g" % (ans.real, ans.imag))
4853

0 commit comments

Comments
 (0)