• Subject: Re: Modulo operator produces incorrect results with negative operand(s).
• From: chris beck <beck.ct@...>
• Date: Sat, 18 Feb 2017 12:51:09 -0500

Thanks to who posted in this thread, I did learn something from it. Particularly, I never considered that modulus with a negative divisor might be useful in a real program.

An equivalent way of stating lua's definition of modulus:

a % b returns the number between 0 and b which is congruent to a modulo b. If b is positive, this number is zero or positive, and if b is negative this number is zero or negative.

In particular, it's true that `a % c == b % c` if and only if a is congruent to b mod c, no matter if any of the numbers are negative.

And, lua's definition leads to a natural definition of "div" in the div-mod equation, as pointed out by others.

To me these look like pretty compelling advantages. So for whatever little it's worth, lua's way of doing it has my personal seal of approval. :)

Also it appears the "C" way of doing things is available from `math.mod` library function.

Best Regards,
Chris Beck

On Thu, Feb 9, 2017 at 7:27 AM, Roberto Ierusalimschy wrote:
> 2017-02-09 15:02 GMT+02:00 Martin <eden_martin_fuhrspam@gmx.de>:
>
> > Sadly (as I understand from similar discussions before) Lua
> > implementation is heavily linked with ANSI C and Roberto generally not
> > willing to introduce changes in logic that is different from ANSI C.
>
> Roberto might like to speak for himself on this, but IMHO the above
> comment oversimplifies his position.

As Viacheslav pointed out, the comment does not make sense here, as the
discussion started exactly because Lua *is* different from ANSI C in
this particular logic corner. Anyway...

First, the fact that the Lua implementation is heavily linked
with ANSI C does not imply at all that the Lua specification is