[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Numeric for loop in Lua 5.3, thoughts reframed
- From: Roberto Ierusalimschy <roberto@...>
- Date: Tue, 14 Nov 2017 08:47:22 -0200
> It is also used elsewhere in the book. Initially I thought this is a
> cute idiom. But what happens when we mix integers and floats? For
> the purposes of this discussion, let us consider the positive case
> only. Now, the reference manual does not say anything about how
> mixed-number loops are set up.
The manual (3.3.5) says that a for loop is equivalent to the following code:
do
local var, limit, step = tonumber(e1), tonumber(e2), tonumber(e3)
if not (var and limit and step) then error() end
var = var - step
while true do
var = var + step
if (step >= 0 and var > limit) or (step < 0 and var < limit) then
break
end
local v = var
block
end
end
If you apply the rules for arithmetic and comparison operations
over mixed numbers, explained elsewhere in the manual, you have
the semantics of mixed-number loops. What exactly do you think
is missing?
-- Roberto