summaryrefslogtreecommitdiffstats
path: root/Python/pystrtod.c
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2009-04-23 19:14:16 (GMT)
committerMark Dickinson <dickinsm@gmail.com>2009-04-23 19:14:16 (GMT)
commitad476dab097c3a0701faf035974c7c1f4b916396 (patch)
tree4cd97c289085e0224f1e00803391d68b46329a04 /Python/pystrtod.c
parentf16e71d889175a76c711dc2c02a33978e5e7e5f7 (diff)
downloadcpython-ad476dab097c3a0701faf035974c7c1f4b916396.zip
cpython-ad476dab097c3a0701faf035974c7c1f4b916396.tar.gz
cpython-ad476dab097c3a0701faf035974c7c1f4b916396.tar.bz2
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.
Diffstat (limited to 'Python/pystrtod.c')
-rw-r--r--Python/pystrtod.c23
1 files changed, 13 insertions, 10 deletions
diff --git a/Python/pystrtod.c b/Python/pystrtod.c
index 95fbd89..002714f 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;