diff options
author | Eric Smith <eric@trueblade.com> | 2009-05-05 14:04:18 (GMT) |
---|---|---|
committer | Eric Smith <eric@trueblade.com> | 2009-05-05 14:04:18 (GMT) |
commit | 63376228a3f2d3ac4a21a10c0653c3b984c2d686 (patch) | |
tree | e781a7c8a1abe5c032d83bbc2948c4bd34d4d77e /Python/pystrtod.c | |
parent | 86a05ecdb5eb91cf174e9b3c8adf0187e868aa68 (diff) | |
download | cpython-63376228a3f2d3ac4a21a10c0653c3b984c2d686.zip cpython-63376228a3f2d3ac4a21a10c0653c3b984c2d686.tar.gz cpython-63376228a3f2d3ac4a21a10c0653c3b984c2d686.tar.bz2 |
Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Diffstat (limited to 'Python/pystrtod.c')
-rw-r--r-- | Python/pystrtod.c | 46 |
1 files changed, 13 insertions, 33 deletions
diff --git a/Python/pystrtod.c b/Python/pystrtod.c index a50d360..d36f931 100644 --- a/Python/pystrtod.c +++ b/Python/pystrtod.c @@ -746,18 +746,15 @@ PyAPI_FUNC(char *) PyOS_double_to_string(double val, PyErr_BadInternalCall(); return NULL; } + /* The repr() precision (17 significant decimal digits) is the + minimal number that is guaranteed to have enough precision + so that if the number is read back in the exact same binary + value is recreated. This is true for IEEE floating point + by design, and also happens to work for all other modern + hardware. */ precision = 17; format_code = 'g'; break; - case 's': /* str format */ - /* Supplied precision is unused, must be 0. */ - if (precision != 0) { - PyErr_BadInternalCall(); - return NULL; - } - precision = 12; - format_code = 'g'; - break; default: PyErr_BadInternalCall(); return NULL; @@ -889,18 +886,19 @@ static char *uc_float_strings[] = { Arguments: d is the double to be converted - format_code is one of 'e', 'f', 'g', 'r' or 's'. 'e', 'f' and 'g' - correspond to '%e', '%f' and '%g'; 'r' and 's' correspond - to repr and str. + format_code is one of 'e', 'f', 'g', 'r'. 'e', 'f' and 'g' + correspond to '%e', '%f' and '%g'; 'r' corresponds to repr. mode is one of '0', '2' or '3', and is completely determined by - format_code: 'e', 'g' and 's' use mode 2; 'f' mode 3, 'r' mode 0. + format_code: 'e' and 'g' use mode 2; 'f' mode 3, 'r' mode 0. precision is the desired precision always_add_sign is nonzero if a '+' sign should be included for positive numbers add_dot_0_if_integer is nonzero if integers in non-exponential form - should have ".0" added. Only applies to format codes 'r', 's', and 'g'. + should have ".0" added. Only applies to format codes 'r' and 'g'. use_alt_formatting is nonzero if alternative formatting should be - used. Only applies to format codes 'e', 'f' and 'g'. + used. Only applies to format codes 'e', 'f' and 'g'. For code 'g', + at most one of use_alt_formatting and add_dot_0_if_integer should + be nonzero. type, if non-NULL, will be set to one of these constants to identify the type of the 'd' argument: Py_DTST_FINITE @@ -1041,13 +1039,6 @@ format_float_short(double d, char format_code, if (decpt <= -4 || decpt > 16) use_exp = 1; break; - case 's': - /* if we're forcing a digit after the point, convert to - exponential format at 1e11. If not, convert at 1e12. */ - if (decpt <= -4 || decpt > - (add_dot_0_if_integer ? precision-1 : precision)) - use_exp = 1; - break; default: PyErr_BadInternalCall(); goto exit; @@ -1220,17 +1211,6 @@ PyAPI_FUNC(char *) PyOS_double_to_string(double val, } break; - /* str format */ - case 's': - mode = 2; - /* Supplied precision is unused, must be 0. */ - if (precision != 0) { - PyErr_BadInternalCall(); - return NULL; - } - precision = 12; - break; - default: PyErr_BadInternalCall(); return NULL; |