I tried to convert a string from Lua(5.1.5) to an integer, and check if the number is a valid integer(0~99999). However, I found lua_tonumber() has different behavior from lua_tointeger() when dealing with big integer.
int main()
{
int in;
double db;
lua_State* Lua = luaL_newstate();
luaL_openlibs(Lua);
lua_pushstring(Lua, "213232127162767162736718238168263816873");
db = lua_tonumber(Lua, -1);
in = lua_tointeger(Lua, -1);
printf("DOUBLE:%f\n", db); // DOUBLE:213232127162767176000119210017101447168.000000
printf("INT:%d\n", in); // INT:0
};
If I use lua_tointeger(), it returns 0 and will pass my checking.
I check both API description, but I still don't know why they have different behaviors. Are these behaviors machine-independent? Is using lua_tonumber() a better way?
Can I use the following code to check the result? (cross-platform)
if (!lua_isnumber(Lua, -1)) { //error }
result = lua_tonumber(Lua, -1);
if (result < 0 || result > 99999) { // error2 }
// pass