lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Soni L. <> wrote:

> On 2017-11-15 11:18 PM, Paige DePol wrote:
>> Soni L. <> wrote:
>>> On 2017-11-15 08:34 PM, Paige DePol wrote:
>>>> Sorry for the noise... I meant to upload an optimised version of the patch
>>>> with the last post to this list! :[
>>>> The patch attached to this post has a small optimisation vs the previously
>>>> posted patch; the limit check has simply been replaced with the step size
>>>> check. This can be done as a limit bounds check is added to OP_FORPREP with
>>>> the patch to check if the loop should iterate at all. This new step size
>>>> check simply adds one numeric subtraction each loop iteration, well, and a
>>>> negation of 'step' for loops with negative step values.
>>>> Again, sorry for the noise, this should be the final patch in relation to
>>>> fixing the 'for' loop overflows in Lua... hopefully! ;)
>>>> ~Paige
>>> What happens if you use excessively large loop steps?
>> Like this?
>> local reps = 0
>> for i=math.mininteger,math.maxinteger,math.maxinteger do reps = reps + 1 end
>> assert(reps == 1)
> Shouldn't this run 3 times, once for i=math.mininteger, once for i=-1, once for i=math.maxinteger-1?
> Or do for loops not work as I thought they did?

Yes, it probably should as the loop with a step size of maxint/2 does run
the correct number of times... testing shows the loop is running with floats
and not integers, interesting.

I will look into this issue and see what has happened, and devise tests to
ensure both the float and integer code paths are taken! Thanks very much for
your help with testing Soni, it is very appreciated! :)

I will also keep in mind that 'maxinteger' is not the full range of the
integer size but rather just the positive half... otherwise I really should
have spotted this issue myself!