lua-users home
lua-l archive

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


"Patrick Donnelly" <batrick.donnelly@gmail.com> writes:

> I'm curious why during Lua's development it was decided that the
> modulus operator be implemented
>
> in Lua:
>
> -7 % 5 = 3

In my book, the only sensible definition.

> where in C:
>
> -7 % 5 = -2

Not at all: you'll find that in C, there merely is the definition

a%b = a - (a/b)*b;

and it is not defined whether (-7)/5 is actually -1 or -2.

> All mathematicians I've spoken to agree that the modulus is defined
> by division:
>
> a = b * q + r
>
> such that q is an integer and r is a natural number.
>
> Naturally -2 is part of the congruence class 3 bar (mod 5) (can't make
> the symbol in plain text).
>
> I would say Lua has done it "correctly", but I'm afraid some
> mathematicians would disagree and say:
>
> 0 <= abs(r) < abs(b)

Huh?  This relation is satisfied by Lua.  Where is the disagreement?

> So, was there a reason why? Personally I'm pretty happy it was
> defined the way it was.

Well, it goes nicely with math.floor(a/b).

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum