summaryrefslogtreecommitdiff
path: root/Objects/complexobject.c
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2008-02-15 06:57:08 +0000
committerChristian Heimes <christian@cheimes.de>2008-02-15 06:57:08 +0000
commit386308bc577b0c8f2d64202dfdbe8c3ceb832a6e (patch)
tree14e0f85b708f48eabaee7cd4384f0b4450601c0d /Objects/complexobject.c
parenta8b81f2951ceebbce431ac4cbabf9621a5a68000 (diff)
downloadcpython-386308bc577b0c8f2d64202dfdbe8c3ceb832a6e.tar.gz
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Diffstat (limited to 'Objects/complexobject.c')
-rw-r--r--Objects/complexobject.c47
1 files changed, 40 insertions, 7 deletions
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 4777ed1481..c87b9e8de2 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -322,16 +322,49 @@ complex_to_buf(char *buf, int bufsz, PyComplexObject *v, int precision)
{
char format[32];
if (v->cval.real == 0.) {
- PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
- PyOS_ascii_formatd(buf, bufsz - 1, format, v->cval.imag);
- strncat(buf, "j", 1);
+ if (!Py_IS_FINITE(v->cval.imag)) {
+ if (Py_IS_NAN(v->cval.imag))
+ strncpy(buf, "nan*j", 6);
+ /* else if (copysign(1, v->cval.imag) == 1) */
+ else if (v->cval.imag > 0)
+ strncpy(buf, "inf*j", 6);
+ else
+ strncpy(buf, "-inf*j", 7);
+ }
+ else {
+ PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
+ PyOS_ascii_formatd(buf, bufsz - 1, format, v->cval.imag);
+ strncat(buf, "j", 1);
+ }
} else {
char re[64], im[64];
/* Format imaginary part with sign, real part without */
- PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
- PyOS_ascii_formatd(re, sizeof(re), format, v->cval.real);
- PyOS_snprintf(format, sizeof(format), "%%+.%ig", precision);
- PyOS_ascii_formatd(im, sizeof(im), format, v->cval.imag);
+ if (!Py_IS_FINITE(v->cval.real)) {
+ if (Py_IS_NAN(v->cval.real))
+ strncpy(re, "nan", 4);
+ /* else if (copysign(1, v->cval.real) == 1) */
+ else if (v->cval.real > 0)
+ strncpy(re, "inf", 4);
+ else
+ strncpy(re, "-inf", 5);
+ }
+ else {
+ PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
+ PyOS_ascii_formatd(re, sizeof(re), format, v->cval.real);
+ }
+ if (!Py_IS_FINITE(v->cval.imag)) {
+ if (Py_IS_NAN(v->cval.imag))
+ strncpy(im, "+nan*", 6);
+ /* else if (copysign(1, v->cval.imag) == 1) */
+ else if (v->cval.imag > 0)
+ strncpy(im, "+inf*", 6);
+ else
+ strncpy(im, "-inf*", 6);
+ }
+ else {
+ PyOS_snprintf(format, sizeof(format), "%%+.%ig", precision);
+ PyOS_ascii_formatd(im, sizeof(im), format, v->cval.imag);
+ }
PyOS_snprintf(buf, bufsz, "(%s%sj)", re, im);
}
}