I tried converting a string from Lua (5.1.5) to an integer and checking if the number is a real integer (0 ~ 99999). However, I found that lua_tonumber () has a different behavior from lua_tointeger () when dealing with a large 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);
If I use lua_tointeger (), it returns 0 and will conduct my check.
I check both API descriptions, but I still don't know why they have different types of behavior. Are these behaviors independent of the machine? Does lua_tonumber () use the best 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
source share