[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Unintuitive + undocumented behavior when NaN used in for-loop
- From: "Sebastian" <sebastian@...>
- Date: Mon, 26 Jun 2023 19:05:04 -0400
I found some strange behavior when NaN is used in a numeric for-loop:
-- only executes once
for i = 0/0, 10 do
print(i) -- -nan
end
-- only executes once
for i = 0/0, 0/0 do
print(i) -- -nan
end
-- never executes
for i = 1, 10, 0/0 do
print(i)
end
-- executes once
for i = 10, 1, 0/0 do
print(i) -- 10
end
In the first two examples, the crux of the problem is the initial check
in FORPREP checks that limit < init, but subsequent checks in FORLOOP
check that idx <= limit. So NaN passes the first time, but not the
second time.
The latter two examples show that NaN is always treated as negative when
used as the step, even if you do math.abs(0/0). The check for whether
the step is negative is 0 < step, which is of course false for NaN.
Maybe using NaN within a numeric for-loop should be disallowed (just as
using 0 as the step is disallowed)?