Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Errors computing modulo

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
K Viltersten - 19 Mar 2008 15:07 GMT
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
Jon Skeet [C# MVP] - 19 Mar 2008 15:13 GMT
> 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.