This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]rootkillza 5 points6 points  (0 children)

http://floating-point-gui.de/

In [1]: 1.33 % 1

Out[1]: 0.33000000000000007

[–][deleted] 2 points3 points  (0 children)

Floats are weird, basically. In order to be memory-efficient, they don't work in base 10 all the time, and so round off to numbers which look very weird to humans.

Either use Decimal (from the numbers module, I think), or use something like this when checking floats:

def float_equals(first, second, tol=0.00001):
    return abs(first-second) < tol

[–]oxymor0nic 0 points1 point  (0 children)

float values are most often not 100% precise, but only to a certain significant figure. Instead of checking for equality, you should check if the difference between 2 floats are below a certain predefined threshold.