Issue #7117, continued: Remove substitution of %g-style formatting for
%f-style formatting, which used to occur at high precision. Float formatting should now be consistent between 2.7 and 3.1.
This commit is contained in:
parent
faa25999a3
commit
9dd5e16c5d
|
@ -1453,9 +1453,9 @@ that ``'\0'`` is the end of the string.
|
||||||
|
|
||||||
.. XXX Examples?
|
.. XXX Examples?
|
||||||
|
|
||||||
For safety reasons, floating point precisions are clipped to 50; ``%f``
|
.. versionchanged:: 2.7
|
||||||
conversions for numbers whose absolute value is over 1e50 are replaced by ``%g``
|
``%f`` conversions for numbers whose absolute value is over 1e50 are no
|
||||||
conversions. [#]_ All other errors raise exceptions.
|
longer replaced by ``%g`` conversions.
|
||||||
|
|
||||||
.. index::
|
.. index::
|
||||||
module: string
|
module: string
|
||||||
|
@ -2875,10 +2875,6 @@ The following attributes are only supported by :term:`new-style class`\ es.
|
||||||
.. [#] To format only a tuple you should therefore provide a singleton tuple whose only
|
.. [#] To format only a tuple you should therefore provide a singleton tuple whose only
|
||||||
element is the tuple to be formatted.
|
element is the tuple to be formatted.
|
||||||
|
|
||||||
.. [#] These numbers are fairly arbitrary. They are intended to avoid printing endless
|
|
||||||
strings of meaningless digits without hampering correct use and without having
|
|
||||||
to know the exact precision of floating point values on a particular machine.
|
|
||||||
|
|
||||||
.. [#] The advantage of leaving the newline on is that returning an empty string is
|
.. [#] The advantage of leaving the newline on is that returning an empty string is
|
||||||
then an unambiguous EOF indication. It is also possible (in cases where it
|
then an unambiguous EOF indication. It is also possible (in cases where it
|
||||||
might matter, for example, if you want to make an exact copy of a file while
|
might matter, for example, if you want to make an exact copy of a file while
|
||||||
|
|
|
@ -601,10 +601,25 @@ class TypesTests(unittest.TestCase):
|
||||||
test(-1.0, ' f', '-1.000000')
|
test(-1.0, ' f', '-1.000000')
|
||||||
test( 1.0, '+f', '+1.000000')
|
test( 1.0, '+f', '+1.000000')
|
||||||
test(-1.0, '+f', '-1.000000')
|
test(-1.0, '+f', '-1.000000')
|
||||||
test(1.1234e90, 'f', '1.1234e+90')
|
|
||||||
test(1.1234e90, 'F', '1.1234e+90')
|
# Python versions <= 2.6 switched from 'f' to 'g' formatting for
|
||||||
test(1.1234e200, 'f', '1.1234e+200')
|
# values larger than 1e50. No longer.
|
||||||
test(1.1234e200, 'F', '1.1234e+200')
|
f = 1.1234e90
|
||||||
|
for fmt in 'f', 'F':
|
||||||
|
# don't do a direct equality check, since on some
|
||||||
|
# platforms only the first few digits of dtoa
|
||||||
|
# will be reliable
|
||||||
|
result = f.__format__(fmt)
|
||||||
|
self.assertEqual(len(result), 98)
|
||||||
|
self.assertEqual(result[-7], '.')
|
||||||
|
self.assertTrue(result[:12] in ('112340000000', '112339999999'))
|
||||||
|
f = 1.1234e200
|
||||||
|
for fmt in 'f', 'F':
|
||||||
|
result = f.__format__(fmt)
|
||||||
|
self.assertEqual(len(result), 208)
|
||||||
|
self.assertEqual(result[-7], '.')
|
||||||
|
self.assertTrue(result[:12] in ('112340000000', '112339999999'))
|
||||||
|
|
||||||
|
|
||||||
test( 1.0, 'e', '1.000000e+00')
|
test( 1.0, 'e', '1.000000e+00')
|
||||||
test(-1.0, 'e', '-1.000000e+00')
|
test(-1.0, 'e', '-1.000000e+00')
|
||||||
|
|
|
@ -12,6 +12,9 @@ What's New in Python 2.7 alpha 1
|
||||||
Core and Builtins
|
Core and Builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- Remove switch from "%f" formatting to "%g" formatting for floats
|
||||||
|
larger than 1e50 in absolute value.
|
||||||
|
|
||||||
- Remove restrictions on precision when formatting floats. E.g.,
|
- Remove restrictions on precision when formatting floats. E.g.,
|
||||||
"%.120g" % 1e-100 used to raise OverflowError, but now gives the
|
"%.120g" % 1e-100 used to raise OverflowError, but now gives the
|
||||||
requested 120 significant digits instead.
|
requested 120 significant digits instead.
|
||||||
|
|
|
@ -957,12 +957,6 @@ format_float_internal(PyObject *value,
|
||||||
if (precision < 0)
|
if (precision < 0)
|
||||||
precision = default_precision;
|
precision = default_precision;
|
||||||
|
|
||||||
#if PY_VERSION_HEX < 0x03010000
|
|
||||||
/* 3.1 no longer converts large 'f' to 'g'. */
|
|
||||||
if ((type == 'f' || type == 'F') && fabs(val) >= 1e50)
|
|
||||||
type = 'g';
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Cast "type", because if we're in unicode we need to pass a
|
/* Cast "type", because if we're in unicode we need to pass a
|
||||||
8-bit char. This is safe, because we've restricted what "type"
|
8-bit char. This is safe, because we've restricted what "type"
|
||||||
can be. */
|
can be. */
|
||||||
|
|
|
@ -4398,9 +4398,6 @@ formatfloat(PyObject *v, int flags, int prec, int type)
|
||||||
if (prec < 0)
|
if (prec < 0)
|
||||||
prec = 6;
|
prec = 6;
|
||||||
|
|
||||||
if (type == 'f' && fabs(x) >= 1e50)
|
|
||||||
type = 'g';
|
|
||||||
|
|
||||||
p = PyOS_double_to_string(x, type, prec,
|
p = PyOS_double_to_string(x, type, prec,
|
||||||
(flags & F_ALT) ? Py_DTSF_ALT : 0, NULL);
|
(flags & F_ALT) ? Py_DTSF_ALT : 0, NULL);
|
||||||
|
|
||||||
|
|
|
@ -8318,9 +8318,6 @@ formatfloat(PyObject *v, int flags, int prec, int type)
|
||||||
if (prec < 0)
|
if (prec < 0)
|
||||||
prec = 6;
|
prec = 6;
|
||||||
|
|
||||||
if (type == 'f' && fabs(x) >= 1e50)
|
|
||||||
type = 'g';
|
|
||||||
|
|
||||||
p = PyOS_double_to_string(x, type, prec,
|
p = PyOS_double_to_string(x, type, prec,
|
||||||
(flags & F_ALT) ? Py_DTSF_ALT : 0, NULL);
|
(flags & F_ALT) ? Py_DTSF_ALT : 0, NULL);
|
||||||
if (p == NULL)
|
if (p == NULL)
|
||||||
|
|
Loading…
Reference in New Issue