Definion of 'for' statement in the manual

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Definion of 'for' statement in the manual

Niccolo Medici
The reference for Lua 5.2 says:

  """
  More precisely, a 'for' statement like

    for v = e1, e2, e3 do block end

  is equivalent to the code:

    do
      local var, limit, step = tonumber(e1), tonumber(e2), tonumber(e3)
      if not (var and limit and step) then error() end
      while (step > 0 and var <= limit) or (step <= 0 and var >= limit) do
        local v = var
        block
        var = var + step
      end
    end
  """

Whereas the reference for Lua 5.3 says it's equivalent to:

  """
  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
  """

My question:

Why does Lua 5.3's reference does "var = var - step" instead of simply
moving the "var = var + step" after "block" (as in Lua 5.2)?

Reply | Threaded
Open this post in threaded view
|

Re: Definion of 'for' statement in the manual

Roberto Ierusalimschy
> My question:
>
> Why does Lua 5.3's reference does "var = var - step" instead of simply
> moving the "var = var + step" after "block" (as in Lua 5.2)?

This is how it is implemented, and both ways can give different results.
(That it is why we do not like to specify things with code, it gives too
much details that should be left open.)

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Definion of 'for' statement in the manual

Coda Highland
On Fri, Sep 25, 2015 at 6:13 AM, Roberto Ierusalimschy
<[hidden email]> wrote:

>> My question:
>>
>> Why does Lua 5.3's reference does "var = var - step" instead of simply
>> moving the "var = var + step" after "block" (as in Lua 5.2)?
>
> This is how it is implemented, and both ways can give different results.
> (That it is why we do not like to specify things with code, it gives too
> much details that should be left open.)
>
> -- Roberto
>

How could both ways give different results?

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: Definion of 'for' statement in the manual

Roberto Ierusalimschy
> >> Why does Lua 5.3's reference does "var = var - step" instead of simply
> >> moving the "var = var + step" after "block" (as in Lua 5.2)?
> >
> > This is how it is implemented, and both ways can give different results.
> > (That it is why we do not like to specify things with code, it gives too
> > much details that should be left open.)
>
> How could both ways give different results?

Floating point operations. See the next example:

  > for i = -2^53, -2^53 + 2 do print(string.format("%.0f", i)) end
  -9007199254740991
  -9007199254740990

(Note that the given initial value, -2^53, is -9007199254740992.)

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Definion of 'for' statement in the manual

Coda Highland
On Sat, Sep 26, 2015 at 11:32 AM, Roberto Ierusalimschy
<[hidden email]> wrote:

>> >> Why does Lua 5.3's reference does "var = var - step" instead of simply
>> >> moving the "var = var + step" after "block" (as in Lua 5.2)?
>> >
>> > This is how it is implemented, and both ways can give different results.
>> > (That it is why we do not like to specify things with code, it gives too
>> > much details that should be left open.)
>>
>> How could both ways give different results?
>
> Floating point operations. See the next example:
>
>   > for i = -2^53, -2^53 + 2 do print(string.format("%.0f", i)) end
>   -9007199254740991
>   -9007199254740990
>
> (Note that the given initial value, -2^53, is -9007199254740992.)
>
> -- Roberto
>

Huh. I'm surprised that's not using int64 since it's Lua 5.3, unless
tonumber() is expressly asking for a double conversion. I wouldn't
have expected this behavior.

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: Definion of 'for' statement in the manual

Dirk Laurie-2
In reply to this post by Roberto Ierusalimschy
2015-09-26 20:32 GMT+02:00 Roberto Ierusalimschy <[hidden email]>:

>> >> Why does Lua 5.3's reference does "var = var - step" instead of simply
>> >> moving the "var = var + step" after "block" (as in Lua 5.2)?
>> >
>> > This is how it is implemented, and both ways can give different results.
>> > (That it is why we do not like to specify things with code, it gives too
>> > much details that should be left open.)
>>
>> How could both ways give different results?
>
> Floating point operations. See the next example:
>
>   > for i = -2^53, -2^53 + 2 do print(string.format("%.0f", i)) end
>   -9007199254740991
>   -9007199254740990
>
> (Note that the given initial value, -2^53, is -9007199254740992.)

On my machine Lua 5.1, 5.2, 5.3 all give that, so where is the
difference?

Reply | Threaded
Open this post in threaded view
|

Re: Definion of 'for' statement in the manual

Roberto Ierusalimschy
In reply to this post by Coda Highland
> Huh. I'm surprised that's not using int64 since it's Lua 5.3, unless
> tonumber() is expressly asking for a double conversion. I wouldn't
> have expected this behavior.

Eponentiation always result in a float, so 2^53 is a float.

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Definion of 'for' statement in the manual

Roberto Ierusalimschy
In reply to this post by Dirk Laurie-2
> On my machine Lua 5.1, 5.2, 5.3 all give that, so where is the
> difference?

In the documentation.

-- Roberto