Search code examples
pythonsubtractionarithmetic-expressions

Python error in basic subtraction?


Possible Duplicate:
Python rounding error with float numbers
python maths is wrong

I can't get Python to correctly do the subtraction 1 - 0.8 and assign it. It keeps coming up with the incorrect answer, 0.19999999999999996.

I explored a bit:

sq = {}
sub = {}
for i in range(1000):
    sq[str(i/1000.)+'**2']=((i/1000.)**2)
    sub['1-'+str(i/1000.)]=(1.0-(i/1000.))

and discovered that this error happens with a somewhat random group of the floats between 0 and 1 to the third decimal place. A similar error also occurs when you square those floats, but to a different subset.

I'm hoping for an explanation of this and how to make Python do the arithmetic right. Using round(x,3) is the work-around I'm using for now, but it's not elegant.

Thanks!

This is a session in my Python 2.7.3 shell:

*** Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32. ***
*** Remote Python engine  is active ***
>>> 1-0.8
0.19999999999999996
>>> print 1-0.8
0.2
>>> a = 1-0.8
>>> a
0.19999999999999996
>>> print a
0.2
>>> a = 0.2
>>> print a
0.2
>>> a
0.2
>>> 

Here's the code I put into a couple online interpreters:

def doit():
    d = {'a':1-0.8}
    return d

print doit()

and the output:

{'a': 0.19999999999999996}

Solution

  • Floating numbers don't work as you're expecting them to.

    For starters, read the floating point guide. Long story short: computers represent floating point numbers as binary, and it turns out that storing a precise decimal fraction as binary is not possible (try it for yourself on paper to see why). For practical purposes, 0.19999999999999996 is "close enough" to 0.2. If you wanted to print it as 0.2, then you could do something like:

    print "%0.1f" % floating_point_value
    

    So what you're seeing isn't an error. It's expected behavior.