lua-users home
lua-l archive

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


The "Go Long Lua!" patch in the wiki at lua-users.org removes floating
point operations by implementing Lua numbers as longs.  Greps of the
assembler output generated by compiling all.c with lmathlib.c removed
suggests the current patch achieves its goal.

The current patch implements modulus as follows.

#define luai_nummod(a,b) ((a)<0==(b)<0||(a)%(b)==0?(a)%(b):(a)%(b)+(b))

I expect a C optimizer will ensure that the remainder operation is
performed once for each usage, but I have not checked.

The current patch implements division as follows.

#define luai_numdiv(a,b) ((a)/(b))

A user of the patch has suggested to me that Lua's division and
modulus operator should satisfy the same equation as C's integer
division and remainder operators.

        a == (a / b) * b + a % b

Therefore, the user suggests the following definition of division.

#define luai_numdiv(a,b) ((a)<0==(b)<0||(a)%(b)==0?(a)/(b):(a)/(b)-1)

Which definition is in the spirit of the Lua language?  Perhaps Lua
should provide a remainder operator in addition to modulus.

John