lua-users home
lua-l archive

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


Egor Skriptunoff <egor.skriptunoff@gmail.com> wrote:

> > for j = "1", 2, 3 do print(j, math.type(j)) end 
> 1.0     float 
> > for j = 1, "2", 3 do print(j, math.type(j)) end 
> 1       integer 
> > for j = 1, 2, "3" do print(j, math.type(j)) end 
> 1.0     float 
>  
> Why does the loop variable hold not an integer sometimes?

This discrepancy happens in the OP_FORPREP instruction:

    if (ttisinteger(init) && ttisinteger(pstep) &&
         forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) {

If the initial value and the step value are integers then
it calls forlimit() which will covert "2" into an integer
value as well, and you will get an integer for loop.

However, if either init or pstep are not integer values then
the if/else drops down the the next block which attempts to
convert all loop parameters into floating point values.

~Paige