123.3 + 0.1 is 123.3999999999 ?

Tim Peters tim.one at comcast.net
Sat May 17 20:53:14 EDT 2003


[A Puzzled User]
>I just wonder why the interactive command line gives us 12.949999999
> while print 12.5 will give us 12.95.... why doesn't the command line
> lie also?

[Tim Roberts]
> Because the "print" command uses str() which ROUNDS the number to 12
> decimal places, whereas the command line uses repr() gives you the
> "exact" value, even if the exact value is not what you expected.

Do note that they both lie:  repr() lies less, rounding to 17 significant
decimal digits.  The reasons for this are explained in a tutorial appendix:

    http://www.python.org/doc/current/tut/node14.html

The exact value stored in the computer in this case is

    12.949999999999999289457264239899814128875732421875

Rounding that to 17 significant decimal digits gives the

    12.949999999999999

displayed at the prompt.






More information about the Python-list mailing list