Integer division in Lua with numbers as longs

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Integer division in Lua with numbers as longs

John D. Ramsdell-2
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
Reply | Threaded
Open this post in threaded view
|

Re: Integer division in Lua with numbers as longs

John D. Ramsdell-2
> 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.

I see this topic was recently discussed in another thread I happened
not to be following.  I concluded that integer division should work as
it does in Python, and changed the patch to define division as
follows.

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

John
Reply | Threaded
Open this post in threaded view
|

Re: Integer division in Lua with numbers as longs

Reuben Thomas-5
On Tue, 17 Apr 2006, John D. Ramsdell wrote:

>> 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.
>
> I see this topic was recently discussed in another thread I happened
> not to be following.  I concluded that integer division should work as
> it does in Python, and changed the patch to define division as
> follows.
>
> #define luai_numdiv(a,b) ((a)<0==(b)<0||(a)%(b)==0?(a)/(b):(a)/(b)-1)

This is not reliable: ANSI/ISO C does not specify how integer division
is rounded (to zero or to -infinity).

--
http://rrt.sc3d.org/
Reality is what refuses to disappear when you stop believing in it (Dick)
Reply | Threaded
Open this post in threaded view
|

Re: Integer division in Lua with numbers as longs

David Jones-2

On Apr 18, 2006, at 01:26, Reuben Thomas wrote:

> On Tue, 17 Apr 2006, John D. Ramsdell wrote:
>
>>> 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.
>>
>> I see this topic was recently discussed in another thread I happened
>> not to be following.  I concluded that integer division should work as
>> it does in Python, and changed the patch to define division as
>> follows.
>>
>> #define luai_numdiv(a,b) ((a)<0==(b)<0||(a)%(b)==0?(a)/(b):(a)/(b)-1)
>
> This is not reliable: ANSI/ISO C does not specify how integer division
> is rounded (to zero or to -infinity).

The 1999 C standard does.  And it specifies round towards zero (which
in my opinion is completely broken).

Previous versions of the C standard allowed the implementation to
select either style of rounding.

For personal reasons I'd love to know about any platform where someone
has observed C's integer division rounding towards -inf.

drj

Reply | Threaded
Open this post in threaded view
|

Re: Integer division in Lua with numbers as longs

John D. Ramsdell-2
In reply to this post by John D. Ramsdell-2
> On Apr 18, 2006, at 01:26, Reuben Thomas wrote:
>
> > On Tue, 17 Apr 2006, John D. Ramsdell wrote:
> >
> >> #define luai_numdiv(a,b) ((a)<0==(b)<0||(a)%(b)==0?(a)/(b):(a)/(b)-1)
> >
> > This is not reliable: ANSI/ISO C does not specify how integer division
> > is rounded (to zero or to -infinity).
>
> The 1999 C standard does.  And it specifies round towards zero (which
> in my opinion is completely broken).
>
> Previous versions of the C standard allowed the implementation to
> select either style of rounding.
>
> For personal reasons I'd love to know about any platform where someone
> has observed C's integer division rounding towards -inf.

You are correct, ANSI C does not specify how integer division is
rounded.  Do you think these definitions will fix the problem?

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

Thank you both for alerting me to this problem.

John