I’m confused with this snippet of code:
t=0
while t<5: #currently loop runs for 10 seconds
print "in loop",t
if (t<5):
print "true"
t=t+0.01
prints in the last run through of the loop:
in loop 5.0
true
Now if it is true that t = 5.0 on the last pass through the loop, shouldn’t the condition that t < 5 in the if statement not be met? And furthermore, shouldn’t it not even be running through the loop for t=5 since it should have also failed the while condition?
5 isn’t necessarily 5:
produces
[…]
0.1 can’t be represented exactly in binary.
[Ah, I just noticed that I used 0.1 instead of 0.01, like you did. Well, it’s the same issue.]
Two “how floating point works” references: classical and gentler.