lua-users home
lua-l archive

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



Whilst we're nitpicking, the suggested patch is not ANSI C:

int n = (int)(((unsigned int)-1) >> 1);
I'm pretty certain casting a negative number to an unsigned number is
a bad thing. Although I could be wrong. In any case, on a system that
uses a sign bit, instead of two's compliment, the equation wouldn't work.

The best way to check if the number is a valid integer is to simply do

lua_Number n = luaL_checknumber(L, 2);
int k = (int) n;
if (lua_Number)k != n {
  // raise error
}

Which is what is seen all over the place in lua, although using the better
optimized lua_number2int macro. Also accounts for invalid choices like
str:rep(math.pi)