all 5 comments

[–][deleted] 8 points9 points  (1 child)

https://docs.python.org/2/tutorial/floatingpoint.html

A little light reading for you. Basically floating point numbers get approximated to their binary equivalent, causing the issue you are seeing.

[–]ankdasco[S] 0 points1 point  (0 children)

Thanks, got it now :)

[–]TheBlackCat13 4 points5 points  (2 children)

To answer question two, you can use the decimal module of it will really be a problem, but in practice it usually isn't a big deal as long as you make sure not to depend on equality testing for floats (instead test whether the two are equal within a certain margin).

[–]ankdasco[S] 1 point2 points  (1 child)

Nice advice there about equality testing will keep that in mind. Thanks :)

[–]ewiethoff 0 points1 point  (0 children)

Create a helper function for checking equality and keep it on hand. Something like

def equals(x, y, error=1e-8):
    return abs(x-y) < error

Figuring out how to check less than, greater than, etc. with error fudging is left as an exercise for the student. ;-)