In my python program, an if statement is not being entered. I have simplified the code to the following:
x = -5
while x < 5:
if (x == 0):
print 0
x += .01
This program does not output anything.
However, changing the last line to x += .5 makes the program output 0. What's the problem?
Floating point number representation might not be accurate enough. You should never test for zero equality but instead use something along
if (abs(x) < 1E-10) ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With