Skip to content

Commit a985a3a

Browse files
committed
Issue python#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.
1 parent 929ab93 commit a985a3a

9 files changed

Lines changed: 110 additions & 27 deletions

File tree

‎Doc/c-api/conversion.rst‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ The following functions provide locale-independent string to number conversions.
8686
Convert a :ctype:`double` *val* to a string using supplied
8787
*format_code*, *precision*, and *flags*.
8888

89-
*format_code* must be one of ``'e'``, ``'E'``, ``'f'``, ``'F'``, ``'g'``,
90-
``'G'``, ``'s'``, or ``'r'``. For ``'s'`` and ``'r'``, the supplied
91-
*precision* must be 0 and is ignored. These specify the standard
92-
:func:`str` and :func:`repr` formats, respectively.
89+
*format_code* must be one of ``'e'``, ``'E'``, ``'f'``, ``'F'``,
90+
``'g'``, ``'G'`` or ``'r'``. For ``'r'``, the supplied *precision*
91+
must be 0 and is ignored. The ``'r'`` format code specifies the
92+
standard :func:`repr` format.
9393

9494
*flags* can be zero or more of the values *Py_DTSF_SIGN*,
9595
*Py_DTSF_ADD_DOT_0*, or *Py_DTSF_ALT*, or-ed together:

‎Include/floatobject.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ PyAPI_DATA(PyTypeObject) PyFloat_Type;
2121
#define PyFloat_Check(op) PyObject_TypeCheck(op, &PyFloat_Type)
2222
#define PyFloat_CheckExact(op) (Py_TYPE(op) == &PyFloat_Type)
2323

24+
/* The str() precision PyFloat_STR_PRECISION is chosen so that in most cases,
25+
the rounding noise created by various operations is suppressed, while
26+
giving plenty of precision for practical use. */
27+
28+
#define PyFloat_STR_PRECISION 12
29+
2430
#ifdef Py_NAN
2531
#define Py_RETURN_NAN return PyFloat_FromDouble(Py_NAN)
2632
#endif

‎Lib/test/test_complex.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,16 @@ def test_format(self):
467467
self.assertEqual(format(3+0j, ''), str(3+0j))
468468
self.assertEqual(format(3.2+0j, ''), str(3.2+0j))
469469

470+
# empty presentation type should still be analogous to str,
471+
# even when format string is nonempty (issue #5920).
472+
self.assertEqual(format(3.2+0j, '-'), str(3.2+0j))
473+
self.assertEqual(format(3.2+0j, '<'), str(3.2+0j))
474+
z = 4/7. - 100j/7.
475+
self.assertEqual(format(z, ''), str(z))
476+
self.assertEqual(format(z, '-'), str(z))
477+
self.assertEqual(format(z, '<'), str(z))
478+
self.assertEqual(format(z, '10'), str(z))
479+
470480
self.assertEqual(format(1+3j, 'g'), '1+3j')
471481
self.assertEqual(format(3j, 'g'), '0+3j')
472482
self.assertEqual(format(1.5+3.5j, 'g'), '1.5+3.5j')

‎Lib/test/test_float.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,53 @@ def test_underflow_sign(self):
257257
self.assertEquals(math.atan2(float('-1e-1000'), -1),
258258
math.atan2(-0.0, -1))
259259

260+
def test_format(self):
261+
# these should be rewritten to use both format(x, spec) and
262+
# x.__format__(spec)
263+
264+
self.assertEqual(format(0.0, 'f'), '0.000000')
265+
266+
# the default is 'g', except for empty format spec
267+
self.assertEqual(format(0.0, ''), '0.0')
268+
self.assertEqual(format(0.01, ''), '0.01')
269+
self.assertEqual(format(0.01, 'g'), '0.01')
270+
271+
# empty presentation type should format in the same way as str
272+
# (issue 5920)
273+
x = 100/7.
274+
self.assertEqual(format(x, ''), str(x))
275+
self.assertEqual(format(x, '-'), str(x))
276+
self.assertEqual(format(x, '>'), str(x))
277+
self.assertEqual(format(x, '2'), str(x))
278+
279+
self.assertEqual(format(1.0, 'f'), '1.000000')
280+
281+
self.assertEqual(format(-1.0, 'f'), '-1.000000')
282+
283+
self.assertEqual(format( 1.0, ' f'), ' 1.000000')
284+
self.assertEqual(format(-1.0, ' f'), '-1.000000')
285+
self.assertEqual(format( 1.0, '+f'), '+1.000000')
286+
self.assertEqual(format(-1.0, '+f'), '-1.000000')
287+
288+
# % formatting
289+
self.assertEqual(format(-1.0, '%'), '-100.000000%')
290+
291+
# conversion to string should fail
292+
self.assertRaises(ValueError, format, 3.0, "s")
293+
294+
# other format specifiers shouldn't work on floats,
295+
# in particular int specifiers
296+
for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
297+
[chr(x) for x in range(ord('A'), ord('Z')+1)]):
298+
if not format_spec in 'eEfFgGn%':
299+
self.assertRaises(ValueError, format, 0.0, format_spec)
300+
self.assertRaises(ValueError, format, 1.0, format_spec)
301+
self.assertRaises(ValueError, format, -1.0, format_spec)
302+
self.assertRaises(ValueError, format, 1e100, format_spec)
303+
self.assertRaises(ValueError, format, -1e100, format_spec)
304+
self.assertRaises(ValueError, format, 1e-100, format_spec)
305+
self.assertRaises(ValueError, format, -1e-100, format_spec)
306+
260307
@unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
261308
"test requires IEEE 754 doubles")
262309
def test_format_testfile(self):

‎Misc/NEWS‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ What's New in Python 2.7 alpha 1
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #5920: For float.__format__, change the behavior with the
16+
empty presentation type (that is, not one of 'e', 'f', 'g', or 'n')
17+
to be like 'g' but with at least one decimal point and with a
18+
default precision of 12. Previously, the behavior the same but with
19+
a default precision of 6. This more closely matches str(), and
20+
reduces surprises when adding alignment flags to the empty
21+
presentation type. This also affects the new complex.__format__ in
22+
the same way.
23+
1524
- Issue #5890: in subclasses of 'property' the __doc__ attribute was
1625
shadowed by classtype's, even if it was None. property now
1726
inserts the __doc__ into the subclass instance __dict__.

‎Objects/complexobject.c‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ complex_dealloc(PyObject *op)
354354

355355

356356
static PyObject *
357-
complex_format(PyComplexObject *v, char format_code)
357+
complex_format(PyComplexObject *v, int precision, char format_code)
358358
{
359359
PyObject *result = NULL;
360360
Py_ssize_t len;
@@ -374,23 +374,23 @@ complex_format(PyComplexObject *v, char format_code)
374374
if (v->cval.real == 0. && copysign(1.0, v->cval.real)==1.0) {
375375
re = "";
376376
im = PyOS_double_to_string(v->cval.imag, format_code,
377-
0, 0, NULL);
377+
precision, 0, NULL);
378378
if (!im) {
379379
PyErr_NoMemory();
380380
goto done;
381381
}
382382
} else {
383383
/* Format imaginary part with sign, real part without */
384384
pre = PyOS_double_to_string(v->cval.real, format_code,
385-
0, 0, NULL);
385+
precision, 0, NULL);
386386
if (!pre) {
387387
PyErr_NoMemory();
388388
goto done;
389389
}
390390
re = pre;
391391

392392
im = PyOS_double_to_string(v->cval.imag, format_code,
393-
0, Py_DTSF_SIGN, NULL);
393+
precision, Py_DTSF_SIGN, NULL);
394394
if (!im) {
395395
PyErr_NoMemory();
396396
goto done;
@@ -421,7 +421,10 @@ complex_print(PyComplexObject *v, FILE *fp, int flags)
421421
{
422422
PyObject *formatv;
423423
char *buf;
424-
formatv = complex_format(v, (flags & Py_PRINT_RAW) ? 's' : 'r');
424+
if (flags & Py_PRINT_RAW)
425+
formatv = complex_format(v, PyFloat_STR_PRECISION, 'g');
426+
else
427+
formatv = complex_format(v, 0, 'r');
425428
if (formatv == NULL)
426429
return -1;
427430
buf = PyString_AS_STRING(formatv);
@@ -435,13 +438,13 @@ complex_print(PyComplexObject *v, FILE *fp, int flags)
435438
static PyObject *
436439
complex_repr(PyComplexObject *v)
437440
{
438-
return complex_format(v, 'r');
441+
return complex_format(v, 0, 'r');
439442
}
440443

441444
static PyObject *
442445
complex_str(PyComplexObject *v)
443446
{
444-
return complex_format(v, 's');
447+
return complex_format(v, PyFloat_STR_PRECISION, 'g');
445448
}
446449

447450
static long

‎Objects/floatobject.c‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ convert_to_double(PyObject **v, double *dbl)
352352
void
353353
PyFloat_AsString(char *buf, PyFloatObject *v)
354354
{
355-
_PyOS_double_to_string(buf, 100, v->ob_fval, 's', 0,
355+
_PyOS_double_to_string(buf, 100, v->ob_fval, 'g', PyFloat_STR_PRECISION,
356356
Py_DTSF_ADD_DOT_0, NULL);
357357
}
358358

@@ -368,9 +368,13 @@ static int
368368
float_print(PyFloatObject *v, FILE *fp, int flags)
369369
{
370370
char buf[100];
371-
_PyOS_double_to_string(buf, sizeof(buf), v->ob_fval,
372-
(flags & Py_PRINT_RAW) ? 's' : 'r',
373-
0, Py_DTSF_ADD_DOT_0, NULL);
371+
if (flags & Py_PRINT_RAW)
372+
_PyOS_double_to_string(buf, sizeof(buf), v->ob_fval,
373+
'g', PyFloat_STR_PRECISION,
374+
Py_DTSF_ADD_DOT_0, NULL);
375+
else
376+
_PyOS_double_to_string(buf, sizeof(buf), v->ob_fval,
377+
'r', 0, Py_DTSF_ADD_DOT_0, NULL);
374378
Py_BEGIN_ALLOW_THREADS
375379
fputs(buf, fp);
376380
Py_END_ALLOW_THREADS
@@ -390,7 +394,8 @@ static PyObject *
390394
float_str(PyFloatObject *v)
391395
{
392396
char buf[100];
393-
_PyOS_double_to_string(buf, sizeof(buf), v->ob_fval, 's', 0,
397+
_PyOS_double_to_string(buf, sizeof(buf), v->ob_fval, 'g',
398+
PyFloat_STR_PRECISION,
394399
Py_DTSF_ADD_DOT_0, NULL);
395400
return PyString_FromString(buf);
396401
}

‎Objects/stringlib/formatter.h‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ format_float_internal(PyObject *value,
881881
int has_decimal;
882882
double val;
883883
Py_ssize_t precision = format->precision;
884+
Py_ssize_t default_precision = 6;
884885
STRINGLIB_CHAR type = format->type;
885886
int add_pct = 0;
886887
STRINGLIB_CHAR *p;
@@ -907,9 +908,10 @@ format_float_internal(PyObject *value,
907908
}
908909

909910
if (type == '\0') {
910-
/* Omitted type specifier. This is like 'g' but with at least
911-
one digit after the decimal point. */
911+
/* Omitted type specifier. This is like 'g' but with at least one
912+
digit after the decimal point, and different default precision.*/
912913
type = 'g';
914+
default_precision = PyFloat_STR_PRECISION;
913915
flags |= Py_DTSF_ADD_DOT_0;
914916
}
915917

@@ -933,7 +935,7 @@ format_float_internal(PyObject *value,
933935
}
934936

935937
if (precision < 0)
936-
precision = 6;
938+
precision = default_precision;
937939

938940
#if PY_VERSION_HEX < 0x03010000
939941
/* 3.1 no longer converts large 'f' to 'g'. */
@@ -1039,6 +1041,7 @@ format_complex_internal(PyObject *value,
10391041
int re_has_decimal;
10401042
int im_has_decimal;
10411043
Py_ssize_t precision = format->precision;
1044+
Py_ssize_t default_precision = 6;
10421045
STRINGLIB_CHAR type = format->type;
10431046
STRINGLIB_CHAR *p_re;
10441047
STRINGLIB_CHAR *p_im;
@@ -1100,6 +1103,7 @@ format_complex_internal(PyObject *value,
11001103
if (type == '\0') {
11011104
/* Omitted type specifier. Should be like str(self). */
11021105
type = 'g';
1106+
default_precision = PyFloat_STR_PRECISION;
11031107
add_parens = 1;
11041108
if (re == 0.0)
11051109
skip_re = 1;
@@ -1115,7 +1119,7 @@ format_complex_internal(PyObject *value,
11151119
type = 'f';
11161120

11171121
if (precision < 0)
1118-
precision = 6;
1122+
precision = default_precision;
11191123

11201124
/* Cast "type", because if we're in unicode we need to pass a
11211125
8-bit char. This is safe, because we've restricted what "type"

‎Python/pystrtod.c‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -660,16 +660,15 @@ _PyOS_double_to_string(char *buf, size_t buf_len, double val,
660660
/* Supplied precision is unused, must be 0. */
661661
if (precision != 0)
662662
return;
663+
/* The repr() precision (17 significant decimal digits) is the
664+
minimal number that is guaranteed to have enough precision
665+
so that if the number is read back in the exact same binary
666+
value is recreated. This is true for IEEE floating point
667+
by design, and also happens to work for all other modern
668+
hardware. */
663669
precision = 17;
664670
format_code = 'g';
665671
break;
666-
case 's': /* str format */
667-
/* Supplied precision is unused, must be 0. */
668-
if (precision != 0)
669-
return;
670-
precision = 12;
671-
format_code = 'g';
672-
break;
673672
default:
674673
assert(0);
675674
return;

0 commit comments

Comments
 (0)