lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>From: "Ralf HECKHAUSEN" <RHeckhau@frequentis.com>

>We want to run Lua on an machine without floating-point-unit and basically only need integer numbers. How much effort is it to switch the number type to long instead of double?

Not much effort really.
The simplest solution is just to choose -DLUA_NUM_TYPE=long in config.
This should compile without problems, although it will still %g in sprintf
for converting numbers to string.
Moreover, the lexer will still use floating-point arithmetic to read numbers
from Lua programs.
If you really want *no* floating-point arithmetic at all, then you need to go
through the code and look for all places marked "LUA_NUMBER" and then remove
the floating-point arithmetic.
For Lua 3.2, these places are:

 lobject.c:double luaO_str2d (char *s) {  /* LUA_NUMBER */
 lvm.c:int luaV_tonumber (TObject *obj) {  /* LUA_NUMBER */
 lvm.c:int luaV_tostring (TObject *obj) {  /* LUA_NUMBER */
 lundump.h:#define NUMBER_FMT      "%.16g"         /* LUA_NUMBER */

plus the "fraction:" loop in llex.c.
You may want to change the API to use longs intead of doubles too.

 lapi.c:double lua_getnumber (lua_Object object)
 lapi.c:void lua_pushnumber (double n)
 lauxlib.c:double luaL_check_number (int numArg)
 lauxlib.c:double luaL_opt_number (int numArg, double def)

and the corresponding declarations in lua.h and lauxlib.h.

I plan to write a Lua Technical Note about this when I find the time.

>Is it possible to have a new user type float next to number with all standard operators? Did someone already implement this?

You could implement those using tag methods on userdata.
--lhf