我在代码中处理皮秒(数字 > 10^12)。
C代码给Lua传数据(atime和eventid都是size_t类型)
lua_getglobal ( luactx, "timer_callback" );
lua_pushunsigned ( luactx, atime );
lua_pushunsigned ( luactx, eventid );
lua_pcall ( luactx, 2, 0, 0 );
Lua函数
function timer_callback(time, eventid)
if eventid == TX_CLOCK then
out_log(tostring(time)) --result is random garbage
set_callback(time + 1000000000000, TX_CLOCK)
return
end
end
我尝试使用 lua_pushnumber,但结果在 lua 中我得到了负数。
最佳答案
从 5.3 开始,Lua 支持 lua_Integer
,默认为 64 位。来自reference manual :
lua_Integer
typedef ... lua_Integer;
The type of integers in Lua.
By default this type is long long (usually a 64-bit two-complement integer), but that can be changed to long or int, usually a 32-bit two-complement integer. (See LUA_INT in luaconf.h.) Lua also defines the constants LUA_MININTEGER and LUA_MAXINTEGER, with the minimum and the maximum values that fit in this type.
Lua 5.2 lua 可以通过编辑 luaconf.h
很容易地强制使用不同的数字类型。数字类型定义为 LUA_NUMBER
。
对于 lua 5.1,你可以安装 lnum补丁,这将更改整数类型。
关于c - Lua C API : handling large numbers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26000176/