[New-bugs-announce] [issue1580] Use shorter float repr when possible

Noam Raphael report at bugs.python.org
Mon Dec 10 20:13:27 CET 2007

New submission from Noam Raphael:

The current float repr() always calculates the 17 first digits of the
decimal representation of a float, and displays all of them (discarding
trailing zeros). This is a simple method for making sure that
eval(repr(f)) == f. However, many times it results in a long string,
where a shorter string would have sufficed. For example, currently
repr(1.1) is '1.1000000000000001', where obviously '1.1' would have been
good enough, and much easier to read.

This patch implements an algorithm for finding the shortest string that
will evaluate to the right number. It is based on the code from
http://www.cs.indiana.edu/~burger/fp/index.html, and also on the
floating-point formatting function of TCL, Tcl_PrintDouble.

The patch also adds a test case, which takes a long list of floating
point numbers, created especially for testing binary to decimal
conversion, and makes sure that eval(repr(f)) == f. See
floating_points.txt for the source of the list.

components: Interpreter Core
files: short_float_repr.diff
messages: 58359
nosy: noam
severity: normal
status: open
title: Use shorter float repr when possible
type: rfe
versions: Python 3.0
Added file: http://bugs.python.org/file8910/short_float_repr.diff

Tracker <report at bugs.python.org>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: short_float_repr.diff
Type: text/x-patch
Size: 35017 bytes
Desc: not available
Url : http://mail.python.org/pipermail/new-bugs-announce/attachments/20071210/9e2c6095/attachment-0001.bin 

More information about the New-bugs-announce mailing list