Skip to content

Commit 2f0da53

Browse files
committed
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
1 parent e247f00 commit 2f0da53

3 files changed

Lines changed: 57 additions & 7 deletions

File tree

‎Lib/test/test_complex.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from random import random
1212
from math import atan2
1313

14+
INF = float("inf")
15+
NAN = float("nan")
1416
# These tests ensure that complex math does the right thing
1517

1618
class ComplexTest(unittest.TestCase):
@@ -337,6 +339,18 @@ def test_repr(self):
337339
self.assertEqual(-6j,complex(repr(-6j)))
338340
self.assertEqual(6j,complex(repr(6j)))
339341

342+
self.assertEqual(repr(complex(1., INF)), "(1+inf*j)")
343+
self.assertEqual(repr(complex(1., -INF)), "(1-inf*j)")
344+
self.assertEqual(repr(complex(INF, 1)), "(inf+1j)")
345+
self.assertEqual(repr(complex(-INF, INF)), "(-inf+inf*j)")
346+
self.assertEqual(repr(complex(NAN, 1)), "(nan+1j)")
347+
self.assertEqual(repr(complex(1, NAN)), "(1+nan*j)")
348+
self.assertEqual(repr(complex(NAN, NAN)), "(nan+nan*j)")
349+
350+
self.assertEqual(repr(complex(0, INF)), "inf*j")
351+
self.assertEqual(repr(complex(0, -INF)), "-inf*j")
352+
self.assertEqual(repr(complex(0, NAN)), "nan*j")
353+
340354
def test_neg(self):
341355
self.assertEqual(-(1+6j), -1-6j)
342356

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 2.6 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- Fixed repr() and str() of complex numbers with infinity or nan as real or
16+
imaginary part.
17+
1518
- Clear all free list during a gc.collect() of the highest generation in order
1619
to allow pymalloc to free more arenas. Python may give back memory to the
1720
OS earlier.

‎Objects/complexobject.c‎

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -322,16 +322,49 @@ complex_to_buf(char *buf, int bufsz, PyComplexObject *v, int precision)
322322
{
323323
char format[32];
324324
if (v->cval.real == 0.) {
325-
PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
326-
PyOS_ascii_formatd(buf, bufsz - 1, format, v->cval.imag);
327-
strncat(buf, "j", 1);
325+
if (!Py_IS_FINITE(v->cval.imag)) {
326+
if (Py_IS_NAN(v->cval.imag))
327+
strncpy(buf, "nan*j", 6);
328+
/* else if (copysign(1, v->cval.imag) == 1) */
329+
else if (v->cval.imag > 0)
330+
strncpy(buf, "inf*j", 6);
331+
else
332+
strncpy(buf, "-inf*j", 7);
333+
}
334+
else {
335+
PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
336+
PyOS_ascii_formatd(buf, bufsz - 1, format, v->cval.imag);
337+
strncat(buf, "j", 1);
338+
}
328339
} else {
329340
char re[64], im[64];
330341
/* Format imaginary part with sign, real part without */
331-
PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
332-
PyOS_ascii_formatd(re, sizeof(re), format, v->cval.real);
333-
PyOS_snprintf(format, sizeof(format), "%%+.%ig", precision);
334-
PyOS_ascii_formatd(im, sizeof(im), format, v->cval.imag);
342+
if (!Py_IS_FINITE(v->cval.real)) {
343+
if (Py_IS_NAN(v->cval.real))
344+
strncpy(re, "nan", 4);
345+
/* else if (copysign(1, v->cval.real) == 1) */
346+
else if (v->cval.real > 0)
347+
strncpy(re, "inf", 4);
348+
else
349+
strncpy(re, "-inf", 5);
350+
}
351+
else {
352+
PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
353+
PyOS_ascii_formatd(re, sizeof(re), format, v->cval.real);
354+
}
355+
if (!Py_IS_FINITE(v->cval.imag)) {
356+
if (Py_IS_NAN(v->cval.imag))
357+
strncpy(im, "+nan*", 6);
358+
/* else if (copysign(1, v->cval.imag) == 1) */
359+
else if (v->cval.imag > 0)
360+
strncpy(im, "+inf*", 6);
361+
else
362+
strncpy(im, "-inf*", 6);
363+
}
364+
else {
365+
PyOS_snprintf(format, sizeof(format), "%%+.%ig", precision);
366+
PyOS_ascii_formatd(im, sizeof(im), format, v->cval.imag);
367+
}
335368
PyOS_snprintf(buf, bufsz, "(%s%sj)", re, im);
336369
}
337370
}

0 commit comments

Comments
 (0)