Why lua_tonumber() has different behavior from lua_tointeger() when dealing with big integer?
我试图将字符串从Lua(5.1.5)转换为整数,然后检查数字是否为有效的整数(0?99999)。 但是,我发现在处理大整数时,lua_tonumber()与lua_tointeger()具有不同的行为。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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 ", db); // DOUBLE:213232127162767176000119210017101447168.000000 printf("INT:%d ", in); // INT:0 }; |
如果我使用lua_tointeger(),它将返回0并通过我的检查。
我检查了两个API描述,但我仍然不知道为什么它们具有不同的行为。 这些行为是否与机器无关? 使用lua_tonumber()是更好的方法吗?
我可以使用以下代码检查结果吗? (跨平台)
1 2 3 4 | if (!lua_isnumber(Lua, -1)) { //error } result = lua_tonumber(Lua, -1); if (result < 0 || result > 99999) { // error2 } // pass |
从
Converts the Lua value at the given acceptable index to the signed integral type
lua_Integer . The Lua value must be a number or a string convertible to a number (see §2.2.1); otherwise,lua_tointeger returns0
Are these behaviors machine-independent? Is using lua_tonumber() a better way?
在某种程度上,它与机器有关,因为类型
参考:将双精度数舍入为32位int的快速方法