I've been messing around with numeric for
loops (including the use of the step
variant), and came across what I thought to be a rather strange result...
For example, if I say something like:
-- From 0-1, increasing i by 0.1 every iteration (i like the look of fractions better than decimal points) for i = 0,1,1/10 do print(i) end
As expected, the code above prints the following:
0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1
However, when working with larger decimal places (i.e, 0.01 and smaller), the loop will end on i - x/step
. For example:
-- This time dividing the step into hundreths... for i = 0,1,1/100 do print(i) end
Except this time instead of i
starting at 0
and ending on 1
, it will start at 0
and end on 0.99
. Is there any specific reason for this? And like I said, same goes for all other larger decimal places:
for i = 0,1,1/1000 do print(i) end
0, 0.001, 0.002 ... 0.999
Question
How can I be 100% certain that my for loop will iterate having it's control variable
equal my start value, all the way through my end value while using the step variant? (i.e, 0, 1, 1/100 looping 0 - 1 in hundredths
) If anyone has some insight on this, I'd really appreciate it.
Precision
This has been asked before, and it comes down to precision. Numbers like 0.1 aren't really accurately representable in binary, so they have to have an approximation which is often slightly more than you expected.
What this means for you is that the step is going up by slightly more than expected, and therefore ends before it hits the final point.
The solution is to use whole numbers and divide afterwards
for i=1,10 do i = i*0.1; end;