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

Why is the minimum not yielded in this function?

Asked by
Klamman 220 Moderation Voter
9 years ago
for i = 5, 1, -0.2 do
    print(i)
end

I can't wrap my head around it. changing the arguments for the for loop to "5, 1, -1" yields results that end in "1" as the last value of i. However, the aforementioned code's minimum value in the output is 1.2. Can someone explain why this is?

1
That's a Numeric Loop not a function. rexbit 707 — 9y

1 answer

Log in to vote
0
Answered by
XAXA 1569 Moderation Voter
9 years ago

Possibly because 0.2 is a floating point that cannot be represented precisely in binary. Therefore, you're really subtracting 5 with a number that is very very /very/ slightly greater than 0.2. In fact, it's exact value is about:

0.200000000000000011102230246251565404236316680908203125

You'd notice that if you decrement it by 0.5 instead of 0.2, it actually prints out 1 in the end. This is because 0.5 can be precisely represented in memory.

Here are some useful resources: link link

Ad

Answer this question