Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

mod of a negative number

Thread view: 
gk - 05 Nov 2006 15:21 GMT
Int a = -5; Int  b  = -2;
System.out.println(a % b); // -1

how  this is working ?
Lars Enderin - 05 Nov 2006 15:28 GMT
gk skrev:
> Int a = -5; Int  b  = -2;
> System.out.println(a % b); // -1
>
> how  this is working ?

(How does this work?)
According to the definition of the % operator -1 is the rest after
taking out a multiple (-4) of -2 from -5.
gk - 05 Nov 2006 15:51 GMT
> gk skrev:
> > Int a = -5; Int  b  = -2;
[quoted text clipped - 5 lines]
> According to the definition of the % operator -1 is the rest after
> taking out a multiple (-4) of -2 from -5.

true.....but -2<-1 , so we can divide it  further theoretically ...is
not it ? hmm...but if we keep on dividing its going to be an infinite
loop.
Patricia Shanahan - 05 Nov 2006 15:39 GMT
> Int a = -5; Int  b  = -2;
> System.out.println(a % b); // -1
>
> how  this is working ?

Strictly speaking, there is no "mod" operator in Java. % is defined to
be "remainder". It is the same as modulo for positive operands.

For integer operands, % is designed to maintain the identity:
(a/b)*b+(a%b) is equal to a.

Java integer division rounds towards zero, so -5/-2 is 2.

( (-5)/(-2) ) * (-2) + (-1) is -5.

See the JLS,
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#239829

Patricia
Richard F.L.R.Snashall - 05 Nov 2006 16:05 GMT
>> Int a = -5; Int  b  = -2;
> For integer operands, % is designed to maintain the identity:
[quoted text clipped - 3 lines]
>
> ( (-5)/(-2) ) * (-2) + (-1) is -5.

A nitpick question: Is the division ( (8)/(3) ) also 2 in Java?
If so, why is this be called "rounding"?
Lee Weiner - 05 Nov 2006 17:12 GMT
>>> Int a = -5; Int  b  = -2;
>> For integer operands, % is designed to maintain the identity:
[quoted text clipped - 6 lines]
>A nitpick question: Is the division ( (8)/(3) ) also 2 in Java?
>If so, why is this be called "rounding"?

It is not called rounding.  It is called integer division.
Patricia Shanahan - 05 Nov 2006 17:46 GMT
>>> Int a = -5; Int  b  = -2;
>> For integer operands, % is designed to maintain the identity:
[quoted text clipped - 6 lines]
> A nitpick question: Is the division ( (8)/(3) ) also 2 in Java?
> If so, why is this be called "rounding"?

I usually use "rounding" in the sense in which it is used in e.g. the
IEEE 754 standard, to mean modifying the infinitely precise result of a
calculation to fit in the destination's format.

In that usage, it includes rounding directed rounding, such as rounding
towards zero, as well as the various flavors of round to nearest. Some
people use "truncation" when the rounding is towards either zero or
negative infinity.

Patricia
gk - 05 Nov 2006 20:13 GMT
> > Int a = -5; Int  b  = -2;
> > System.out.println(a % b); // -1
[quoted text clipped - 15 lines]
>
> Patricia

nice explanation!


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.