c - Lua C API : handling large numbers

标签 c lua

我在代码中处理皮秒(数字 > 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/

相关文章:

c - C 中的 Solaris 时区(在 strftime 上缺少 %z)

windows - Lua io.popen() - 在 Windows 上访问共享驱动器

lua - LÖVE 中的背景颜色

sockets - 如何使用 Nginx/Openresty 将 tcp 流复用到多个 websocket 客户端?

c - 动态分配矩阵并处理所有错误的函数

c - 如何将外部文件中的数据导入到我的程序中

c - 控制台程序应该在关闭时释放它的资源吗?

lua - Lua vm 的简明描述?

lua - 将字符串格式化为lua中最小长度的数字

C中使用链表将二进制转换为十进制