On Tue, 5 Feb 2002, f. bou wrote:
> In Ruby version 1.6.6
> is -9 % 2 = 5 why. I thought it should be 4 or -4.
> Is it a bug?

you mean -9 % 5 gives 1; it does not give -4 because / and % together obey
the relation (a/b)*b + (a%b) = a where a and b are integers.

a/b on integers is division rounded _downwards_, so -9 / 5 gives -2, not
-1, so (a/b)*b gives -10, not -5, and so the correction factor (the
modulo) has to be 1, not -4.

it also can be said that in Ruby (and Python), the result of a%b has the
same sign as b. In Perl (and Java and C), a%b is the same sign as a,
because division is rounded _towards zero_ instead.

________________________________________________________________
Mathieu Bouchard                   http://hostname.2y.net/~matju