Errors computing modulo

  • Thread starter Thread starter Jon Skeet [C# MVP]
  • Start date Start date
J

Jon Skeet [C# MVP]

K Viltersten said:
I get arithemtical error computing this.

int modulo = -2 % 3;

The computer returns -2, as far i can
see. The correct value should be 1.

You've made the incorrect assumption that it's a modulus operator. It's
actually a remainder operator. There is no modulus operator in C#.

From the spec (C# 3, unified) section 7.7.3, integer remainder:

<quote>
The result of x % y is the value produced by x - (x / y) * y. If y is
zero, a System.DivideByZeroException is thrown.
</quote>

So, -2 % 3 = -2 - (-2/3)
= -2 - 0
= -2
 
I get arithemtical error computing this.

int modulo = -2 % 3;

The computer returns -2, as far i can
see. The correct value should be 1.
 
I get arithemtical error computing this.
You've made the incorrect assumption that
it's a modulus operator. It's actually a
remainder operator. There is no modulus
operator in C#.

Yes, i have, indeed.

Thank you.
 
Jon,

You've made the incorrect assumption that it's a modulus operator. It's
actually a remainder operator. There is no modulus operator in C#.

That's what MSDN calls the '%' operator though:

http://msdn2.microsoft.com/en-us/library/0w4e0fzs.aspx

K&R and Stroustrup use the same name, but prefer not to be specific about
behavior with negative operands ("machine dependent" resp. "implementation
dependent") :-)

Regards,
Gilles.
 
That's what MSDN calls the '%' operator though:

http://msdn2.microsoft.com/en-us/library/0w4e0fzs.aspx

That's unfortunate. The spec always calls it a remainder. Mind you, if
you overload the operator I believe it's called op_Modulus.

I *thought* that in maths terms a remainder could be negative, but
modulus never is. I could be wrong though.
K&R and Stroustrup use the same name, but prefer not to be specific about
behavior with negative operands ("machine dependent" resp. "implementation
dependent") :-)

That's even worse!
 
I *thought* that in maths terms a
remainder could be negative, but
modulus never is. I could be wrong
though.

Modulus is NEVER negative, as it by
definition belongs to a subset of the
set of natural numbers lower than the
moduler. E.g.:

let x be any integer
let y be any non-zero, natural number
then
x mod y is in [0 ; y-1] intersected
by the set of natural numbers
 
K Viltersten said:
I *thought* that in maths terms a
remainder could be negative, but
modulus never is. I could be wrong
though.

Modulus is NEVER negative, as it by
definition belongs to a subset of the
set of natural numbers lower than the
moduler. E.g.:

let x be any integer
let y be any non-zero, natural number
then
x mod y is in [0 ; y-1] intersected
by the set of natural numbers

Good, it's not just me then :) (Wikipedia is somewhat vague.)
 
Modulus is NEVER negative, as it by
definition belongs to a subset of the
set of natural numbers lower than the
moduler. E.g.:

let x be any integer
let y be any non-zero, natural number
then
x mod y is in [0 ; y-1] intersected
by the set of natural numbers

I don't know if it's an "official" mathematical definition but the
Fortran modulo operator can in fact return negative numbers, but only
if the divisor (y in your example) is negative. Modulo follows the
sign of the divisor while remainder follows the sign of the dividend.
 
Back
Top