This is a problem with numbers on our computer not having infinite precision. When you write 0.1
, it doesn't actually store is as "one tenth". It stores it as a base-2 approximation of 0.1, which is something like this:
1 | 0.10000000000000000555111512312578 |
That means the number you get after you get just around 1
is about 20 * .00000000000000000555111512312578
under 1
, so it declares the loop over.
The simplest solution is to use integers, and divide on the inside:
Depending on what you are doing, other options might make sense. For example, if you're doing something like an animation, you could not worry about the for
loop altogether and just use the time:
3 | while tick() < start + duration do |
4 | local time = tick() - start |
5 | local progress = time / duration |