This math scares me

Dan Maas dmaas at dcine.com
Mon Mar 12 16:56:32 EST 2001


> Ok, I can see maybe division having problems. But why does addition of
> the two numbers below:
>
> 5.01+5.54
>
> give me this?
>
> 10.550000000000001

Python uses a standard 4- or 8-byte limited-precision representation for
rational numbers. You will never get more than a few significant digits of
precision. (there are techniques to get more if you really need it, but they
are slow and complicated)

If this bothers you just ignore all but the first few digits...

>>> a = 5.01 + 5.54
>>> print "%.4f" % a

10.5500

Dan





More information about the Python-list mailing list