[Tutor] greater precision?

eryksun eryksun at gmail.com
Mon Oct 29 17:20:15 CET 2012

On Mon, Oct 29, 2012 at 11:15 AM, Mark Lawrence <breamoreboy at yahoo.co.uk> wrote:
> On 29/10/2012 12:00, Dave Angel wrote:
>> Not silly at all.  I didn't realize str(float) would truncate to 12
>> digits either.  I found out by experimenting (with 2.7) in the
>> interpreter.
> It's 16 digits with 3.3.0.

It was changed to use the repr in 3.2. See tp_repr/tp_str in PyFloat_Type:


>From "What’s New In Python 3.2":


The str() of a float or complex number is now the same as its repr().
Previously, the str() form was shorter but that just caused confusion
and is no longer needed now that the shortest possible repr() is
displayed by default

As repr() does, it chooses the minimum number of digits required to
round exactly to the original double:

repr with 1 digit:

    >>> pack('d', 0.1)
    >>> unpack('d', '\x9a\x99\x99\x99\x99\x99\xb9?')[0]

repr with 16 digits:

    >>> pack('d', 0.0999999999999995)
    >>> unpack('d', 'v\x99\x99\x99\x99\x99\xb9?')[0]

repr with 17 digits:

    >>> pack('d', 0.10000000000000002)
    >>> unpack('d', '\x9b\x99\x99\x99\x99\x99\xb9?')[0]

More information about the Tutor mailing list