Just wondering if anyone has touted the idea of adding the (other common) alternative definition of modulo, which is generally a lot more number theory friendly. Specifically the definition which always gives a non-negative result to "
n mod d" whenever
d is positive, regardless of whether
n is positive or negative.
Obviously I'm not suggesting that
mod be redefined, that would break too much code, but maybe a separate operator, for example called "
modulo", be added.
Don't misunderstand me and think I'm just making some kind of "Pascal is Wrong" jibe. Actually I like the way Pascal's
div works (truncating towards zero rather than the alternative of "flooring" towards negative infinity). And of course once you have defined how
div works there is no real choice for how
mod should work, if the two are to be self consistent.
And by self consistent, I mean the most important property of divide with remainder. Namely, that with
Q = n div d and
R = n mod d, that :
n = Q*d + RSo if I like the way
div works, and
mod is self consistent, then you may well ask what am I complaining about? Well basically it's the following property of
modulo that is important in number theory, but is not satisfied by the Pascal definition of
mod.
In number theory, modulo forms an equivalence class. So for example in modulo base 12 (think for example the hour hand on a clock) then the following are all equivalent :
{ .... -32, -20, -8, 4, 16, 28, 40, ... }And the normal number theory definition of the modulo operator returns the least positive (or zero) member of that equivalence class, namely
4 in this case.
This leads to a really important property of modulo, that :
n modulo d = (n + k*d) modulo d, for any integer
k (positive or negative).
In words this simply means that we can add or subtract any multiple of our modulo base (any multiple of 12 to our hour hand in the above example) and the final result is unchanged.
Unfortunately, Pascal's mod does not satisfy the above property.
For example:
16 mod 12 = 4however:
(16 - 3*12) mod 12 = -8Anyway, just wondering what people's opinions are on having the alternate modulo available (as a separate key word such as "modulo")?
I'm getting a bit sick of typing
(((n mod d) + d) mod d) just to achieve
(n mod d) when I don't know the polarity of
n.