Lua C api: handling large quantities

I am dealing with picoseconds in my code (digits> 10 ^ 12).
C for transferring data to Lua (atime and eventid are of type size_t)

lua_getglobal ( luactx, "timer_callback" ); lua_pushunsigned ( luactx, atime ); lua_pushunsigned ( luactx, eventid ); lua_pcall ( luactx, 2, 0, 0 ); 

Lua function

 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 

I tried with lua_pushnumber, but as a result in lua I got negative numbers.

+5
source share
1 answer

Lua since 5.3 supports lua_Integer , which defaults to 64 bits. From the reference guide:

lua_Integer

typedef ... lua_Integer;

The type of integers in Lua.

By default, this type is long (usually a 64-bit integer with two additions), but it can be changed to long or int, usually a 32-bit integer with two additions. (See LUA_INT in the luaconf.h file.) Lua also defines the constants LUA_MININTEGER and LUA_MAXINTEGER with minimum and maximum values ​​that correspond to this type.

Lua 5.2 lua can be forced to use another type of number by simply editing luaconf.h . The type of number is defined as LUA_NUMBER .

For lua 5.1, you can install the lnum patch, which will change the integer type.

+5
source

Source: https://habr.com/ru/post/1203235/


All Articles