[Python-ideas] Python Numbers as Human Concept Decimal System

Guido van Rossum guido at python.org
Sat Mar 8 03:19:14 CET 2014


On Fri, Mar 7, 2014 at 6:10 PM, Chris Angelico <rosuav at gmail.com> wrote:

> On Sat, Mar 8, 2014 at 1:02 PM, Guido van Rossum <guido at python.org> wrote:
> > The repr() function does not round to a fixed number of decimals. It
> > produces (in theory, dynamically, although I suspect that the current
> > algorithm is better) the shortest decimal string that, when converted
> back
> > to binary, equals *exactly* the input.
>
> If that's the definition of a float's repr, then I'd support using
> that by default for the construction of a Decimal from a float. You
> can always use .as_integer_ratio() to get the exact stored
> representation.
>

Sadly I can't point to exactly where this is documented, but since Python
2.7 and some early version of 3.x that is indeed how repr(<float>) works.

(And str(<float>) too -- before 2.7, str() would give only 12 digits while
repr() gave 17, but the new repr() makes such shenanigans unnecessary. So I
could have saved myself a few characters by using str() instead of repr()
everywhere in this thread. :-)

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140307/c2f7661c/attachment.html>


More information about the Python-ideas mailing list