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.
--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy
> 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

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
K Viltersten - 19 Mar 2008 15:34 GMT
>> I get arithemtical error computing this.
>>
[quoted text clipped - 7 lines]
> remainder operator. There is no modulus
> operator in C#.
Yes, i have, indeed.
Thank you.
--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Gilles Kohl [MVP] - 19 Mar 2008 23:16 GMT
Jon,
>> I get arithemtical error computing this.
>>
[quoted text clipped - 5 lines]
>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.
Jon Skeet [C# MVP] - 19 Mar 2008 23:32 GMT
> >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
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!

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
K Viltersten - 20 Mar 2008 20:20 GMT
> 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
--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Jon Skeet [C# MVP] - 20 Mar 2008 21:29 GMT
> > I *thought* that in maths terms a
> > remainder could be negative, but
[quoted text clipped - 11 lines]
> 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.)

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Chris Nahr - 21 Mar 2008 08:35 GMT
>Modulus is NEVER negative, as it by
>definition belongs to a subset of the
[quoted text clipped - 6 lines]
> 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.
--
http://www.kynosarges.de