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