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.

This commit is contained in:
Eric Smith 2009-05-05 14:04:18 +00:00
parent 86a05ecdb5
commit 63376228a3
9 changed files with 68 additions and 51 deletions

View file

@ -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;