From ad476dab097c3a0701faf035974c7c1f4b916396 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Thu, 23 Apr 2009 19:14:16 +0000 Subject: Issue #5816: Simplify code for parsing and printing of complex numbers. nans and infs are no longer given special treatment; as a result, repr(complex(z)) recovers z for any complex number z. --- Python/pystrtod.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'Python/pystrtod.c') diff --git a/Python/pystrtod.c b/Python/pystrtod.c index 95fbd891520..002714f7c29 100644 --- a/Python/pystrtod.c +++ b/Python/pystrtod.c @@ -630,8 +630,9 @@ PyAPI_FUNC(char *) PyOS_double_to_string(double val, } p = result; - /* Never add sign for nan/inf, even if asked. */ - if (flags & Py_DTSF_SIGN && buf[0] != '-' && t == Py_DTST_FINITE) + /* Add sign when requested. It's convenient (esp. when formatting + complex numbers) to include a sign even for inf and nan. */ + if (flags & Py_DTSF_SIGN && buf[0] != '-') *p++ = '+'; strcpy(p, buf); @@ -733,6 +734,10 @@ format_float_short(double d, char format_code, so convert Infinity to inf and NaN to nan, and ignore sign of nan. Then return. */ + /* ignore the actual sign of a nan */ + if (digits[0] == 'n' || digits[0] == 'N') + sign = 0; + /* We only need 5 bytes to hold the result "+inf\0" . */ bufsize = 5; /* Used later in an assert. */ buf = (char *)PyMem_Malloc(bufsize); @@ -742,13 +747,13 @@ format_float_short(double d, char format_code, } p = buf; + if (sign == 1) { + *p++ = '-'; + } + else if (always_add_sign) { + *p++ = '+'; + } if (digits[0] == 'i' || digits[0] == 'I') { - if (sign == 1) { - *p++ = '-'; - } - else if (always_add_sign) { - *p++ = '+'; - } strncpy(p, float_strings[OFS_INF], 3); p += 3; @@ -756,8 +761,6 @@ format_float_short(double d, char format_code, *type = Py_DTST_INFINITE; } else if (digits[0] == 'n' || digits[0] == 'N') { - /* note that we *never* add a sign for a nan, - even if one has explicitly been requested */ strncpy(p, float_strings[OFS_NAN], 3); p += 3; -- cgit v1.2.3