[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Integer division in Lua with numbers as longs
- From: ramsdell@... (John D. Ramsdell)
- Date: 18 Apr 2006 12:54:03 -0400
> 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