Floating point multiplication in python

Gary Herron gherron at islandtraining.com
Tue Sep 6 09:00:39 CEST 2011


On 09/05/2011 10:57 PM, xyz wrote:
> hi all:
>
> As we know ,  1.1 * 1.1 is 1.21 .
> But in python ,I got following :
>
>>>> 1.1 * 1.1
> 1.2100000000000002
>
> why python get wrong result? Who can tell me  where's the 0.0000000000000002 from?

It's not a python error    It's the nature of floating point arithmetic 
to be inaccurate on *ANY* computer.    Python just allows you to see the 
inaccuracies.

(But try:
   print 1.1*1.1
and see that the print statement does hide the roundoff error from you.)

Read this for more info:
     
http://pyfaq.infogami.com/why-are-floating-point-calculations-so-inaccurate




More information about the Python-list mailing list