Ian Kelly wrote:
On Tue, Apr 1, 2008 at 11:40 AM, Geoffrey Spear <[EMAIL PROTECTED]> wrote:
There seems to be some disagreement in the literature (and apparently
in C machine-dependent differing behavior of the % operator) about how
to take the modulus of a negative number. There seem to be equally
good arguments that ((0-4) % 10) = 6 or 4.
I think that "evaluates the expression modulo 10" should be taken to
mean "evaluates the expression modulo 10 (using modular arithmetic)",
not "evaluates the expression and then evaluates the result using a
modulo 10 operation".
AFAIK, the modular arithmetic definition would consistently yield 6,
since the definition is based upon rings of integers.
The only problem with defining -4 % 10 as equal to 6 is that you then
have to have -4 / 10 as -1 which strikes many people as a bit odd. (You
have to satisfy (a / b) * b + a % b = a if there's to be any sense to
your definition at all.)
Once you allow negative denominators, there are three consistent ways of
defining modulus. See "The Euclidean definition of the functions div
and mod" by Raymond Boute (available on the web I'm sure; I can send you
a PDF if you want).
Michael.