lua-users home
lua-l archive

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


In message <33560f2d1b9b8804a11f7c632851919f@ricilake.net> you wrote:

> Is there a good reason why % is implemented in terms of floor rather 
> than fmod? The following seems curiously inconsistent to me:
> 
> Lua 5.1 (work6)  Copyright (C) 1994-2005 Tecgraf, PUC-Rio
>  > =-3 % 2
> 1
>  > =math.mod(-3, 2)
> -1

This is an old old story, a matter of contention between mathematicians
who want mod defined properly to be useful for modular arithmetic and,
I guess engineers and (some) computer scientists who want ... well I am
not sure I know what they want.
If you want to use positive integers as representatives of congruence
classes for modular arithmetic, and I think you have to be seriously
weird to want otherwise, so that x%k is the congruence class of x
modulo k, then (-3)%2 is 1 because -3 = 2*(-2) + 1, and so 1 represents
the congruence class of -3 modulo 2. The math.mod definition is one of
those classic mistakes made by some non-mathematical riff-raff (anyone
for inter-faculty paint-ball?);.
 
-- 
Gavin Wraith (gavin@wra1th.plus.com)
Home page: http://www.wra1th.plus.com/