I noticed this strange behavior in a part of my code:
num = 0.14
num += 0.01
if num == 0.15:
print('equal')
elif num > 0.15:
print('greater')
elif num < 0.15:
print('less')
> greater
I assume this is because of binary rounding errors, so num
is actually stored as 0.1500000000001
or something.
I'm just wondering what is the best way to avoid these kinds of errors in conditional statements?