[issue1580] Use shorter float repr when possible

Tim Peters report at bugs.python.org
Fri Jul 4 03:02:28 CEST 2008


Tim Peters <tim.peters at gmail.com> added the comment:

If you think using 16 (when possible) will stop complaints, think again
;-)  For example,

>>> for x in 0.07, 0.56:
...     putatively_improved_repr = "%.16g" % x
...     assert float(putatively_improved_repr) == x
...     print putatively_improved_repr
0.07000000000000001
0.5600000000000001

(those aren't the only "2-digit" examples, but I expect those specific
examples work the same under Linux and Windows).

IOW, 16 doesn't eliminate base-conversion surprises, except at the 8
"single-digit surprises" (i/10.0 for i in range(1, 5) + range(6, 10)).

To eliminate "2-digit surprises" (like 0.07 and 0.56 above), and higher,
 in this way, you need to add a loop and keeping trying smaller
conversion widths so long as they convert back to the original input.

Keep it up, and you can make repr(float) so slow it would be faster to
do perfect rounding in Python ;-)

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue1580>
_______________________________________


More information about the Python-bugs-list mailing list