Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

Trouble with numeric for loop and step variant?

Asked by 8 years ago

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.

1 answer

Log in to vote
2
Answered by 8 years ago

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;
0
PIL for Lua 5.0 advises against modifying the counting variable in a for loop: http://www.lua.org/pil/4.3.4.html I do not have a copy of PIL for 5.1 so I don't know if the advice still stands BlueTaslem 18071 — 8y
0
Yeah it advises against it but I've not had any unusual behaviour from doing it in the past (I wanted to skip a loop to a step but it wouldn't do it) User#6546 35 — 8y
Ad

Answer this question