very simple questions about integer division

  • From: Francesco Abbate <>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 30 Aug 2012 18:10:20 +0200


I've a very simple question about integer division. In C this is a
really simple matter:

int a, b;
int c;

c = a / b;

and the operation is a elementary one in term of CPU.

Lua does always consider numbers as doubles and the division operator
(/) is always, implicitly, the FP division.

I was therefore wondering how to obtain the integer division in
Lua/LuaJIT. Of course it can be done using some tricks:

c = (a - a % b) / b


c = math.floor(a / b)

but I was wondering what was the better way to do it with LuaJIT2 to
be sure that the code is properly optimized.

For me it is also quite annoying that there isn't simple way to do
integer division. After all this is an elementary operation and the %
operator, which is related, is already available. I'm also missing the
divmod operator that returns the quotient *and* the remainder. I've
added a divmod function myself in the math module but I'm afraid that
this could be not well optimised by LuaJIT2.

Thanks in advance for any help :-)


Other related posts: