Simple addition

Brian pythonista_at_sbcglobal_dot_net at invalid.net
Sun Feb 15 21:24:32 CET 2004


Mathieu Malaterre wrote:
> $python
>  >>464.73+279.78
> 744.50999999999999
> 
> weird isn't it ? I use python2.3.

Mathieu, you can find a full explanation of what you're seeing at the 
following documentation link: 
http://www.python.org/doc/current/tut/node15.html

 From that page:

"Note that this is in the very nature of binary floating-point: this is 
not a bug in Python, it is not a bug in your code either, and you'll see 
the same kind of thing in all languages that support your hardware's 
floating-point arithmetic (although some languages may not display the 
difference by default, or in all output modes).

Python's builtin str() function produces only 12 significant digits, and 
you may wish to use that instead. It's unusual for eval(str(x)) to 
reproduce x, but the output may be more pleasant to look at:


 >>> print str(0.1)
0.1

It's important to realize that this is, in a real sense, an illusion: 
the value in the machine is not exactly 1/10, you're simply rounding the 
display of the true machine value."



More information about the Python-list mailing list