-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
enhancementNew feature or requestNew feature or request
Description
背景
- 在Lua5.1自身的规格中,Lua语言的数字类型只有number。这个类型对应C中的double。纯Lua代码不借助C的话,没有内置方式分辨一个number是整数还是小数。
- 在LuaJIT/Lua5.1的C API中,数字类型除了上述的number外,有一个专用的integer类型。这个类型的初衷是为了和C的整数相互转化,并且对应C中的ptrdiff_t(这个类型相当于long,实测用msvc2019编译到32位上其上下限PTRDIFF_MIN和PTRDIFF_MAX分别是-2147483648和2147483647)。相关的函数有lua_pushinteger, lua_tointeger, luaL_checkinteger, luaL_optinteger,但是没有检查栈顶是否是整数的isinteger。
- 在RMVA所用的Ruby中,数字类型Numeric分为两种,Integer和Float;其中Integer默认是FixNum("which is 31 bits long in most cases" - RMVA文档),需要更大数时自动转到Bignum大数;Float则是对应C中的double。
问题
传入、传出数字类型时,类型转换的关系应该如何设置?
可能的方案
Integer/Float -> number; number -> Float
统一成C的double表示的方案;用到的C API很清晰;缺点是Ruby侧可能常需要把返回的Float转化成Integer,并且在数本身很大时可能有精度损失
Integer -> integer; Float -> number; number -> float; integer -> Integer
比较自然的转换,但是怎么分辨lua栈顶是integer(并且还需要把它成功取出来)是个挑战
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request