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

A weird quirk with numerical for-loops and decimal increments. What's happening?

Asked by
XAXA 1569 Moderation Voter
8 years ago

As some of you may know, doing a for loop like this:

for i = 1, 2, 0.1 do 
       print(i)
end

would print:

1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9

because of floating-point rounding errors. However, when I set the start and end conditions to 0 and 1, like this:

for i = 0, 1, 0.1 do 
       print(i)
end

it correctly terminates at 1:

0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0

This only seems to be the case when the increment is 0.1 (or when it's a multiple of 0.5^n) and start/end is 0, 1, respectively. When I set it to 0.01, it terminates at 0.99. Furthermore, when I work backwards, from 1 to 0 with a decrement of -0.1, it terminates at a very small number (that's not quite 0):

1.0
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
1.3877787807814e-16

It looks like this only ever terminates correctly when the start, end, and increment is a multiple of 0, 1, and 0.1, respectively:

for i = 0, 3, 0.3 do 
       print(i)
end

prints:

0.0
0.3
0.6
0.9
1.2
1.5
1.8
2.1
2.4
2.7
3.0

My question is: is this a quirk with this particular numeric for-loop, or is there something else going on?

Answer this question