Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > mod of a negative number

Reply
Thread Tools

mod of a negative number

 
 
gk
Guest
Posts: n/a
 
      11-05-2006
Int a = -5; Int b = -2;
System.out.println(a % b); // -1

how this is working ?

 
Reply With Quote
 
 
 
 
Lars Enderin
Guest
Posts: n/a
 
      11-05-2006
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.
 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      11-05-2006
gk wrote:
> 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/s...oc.html#239829

Patricia
 
Reply With Quote
 
gk
Guest
Posts: n/a
 
      11-05-2006

Lars Enderin wrote:
> 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.


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.

 
Reply With Quote
 
Richard F.L.R.Snashall
Guest
Posts: n/a
 
      11-05-2006
Patricia Shanahan wrote:
> gk wrote:
>> Int a = -5; Int b = -2;

> 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.


A nitpick question: Is the division ( (/(3) ) also 2 in Java?
If so, why is this be called "rounding"?
 
Reply With Quote
 
Lee Weiner
Guest
Posts: n/a
 
      11-05-2006
In article <(E-Mail Removed)>, "Richard F.L.R.Snashall" <(E-Mail Removed)> wrote:
>Patricia Shanahan wrote:
>> gk wrote:
>>> Int a = -5; Int b = -2;

>> 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.

>
>A nitpick question: Is the division ( (/(3) ) also 2 in Java?
>If so, why is this be called "rounding"?


It is not called rounding. It is called integer division.

 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      11-05-2006
Richard F.L.R.Snashall wrote:
> Patricia Shanahan wrote:
>> gk wrote:
>>> Int a = -5; Int b = -2;

>> 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.

>
> A nitpick question: Is the division ( (/(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
 
Reply With Quote
 
gk
Guest
Posts: n/a
 
      11-05-2006

Patricia Shanahan wrote:
> gk wrote:
> > 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/s...oc.html#239829
>
> Patricia


nice explanation!

 
Reply With Quote
 
kstahmer kstahmer is offline
Junior Member
Join Date: May 2010
Posts: 1
 
      05-13-2010
Quote:
Originally Posted by Patricia Shanahan
gk wrote:
> 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,

Patricia
Illuminating post - thanks.

The JLS example was also illuminating:

5 % 3 produces 2
5 % (-3) produces 2
(-5) % 3 produces -2
(-5) % (-3) produces -2

This got me thinking...

Suppose a and b are integers.

We have two special cases:
  1. If b == 0, then a % b is NaN (JLS specification).
  2. If nonzero b divides a, then a % b == 0 (In particular, every nonzero b divides 0, so 0 % b == 0).
Which leaves us with nonzero b does not divide a.

Then

a % b > 0 if a > 0

and

a % b < 0 if a < 0.

Hence, if nonzero b does not divide a, then the sign of a % b equals the sign of a.

You can see this formally by noting:

(a / b) * b + a % b == a

implies

a % b == a - (a / b) * b

and if nonzero b does not divide a, then

Math.abs((a / b) * b) < Math.abs(a),

due integer division rounding towards zero.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why doesn't `from pkg import mod' work after `del pkg.mod'? ryles Python 3 07-26-2009 04:13 PM
Adding a positive number and a negative number EK Python 0 01-30-2009 04:13 AM
difference between import from mod.func() and x=mod.func() Hari Sekhon Python 0 06-20-2006 08:07 AM
Negative setup and Negative hold prem_eda VHDL 5 10-11-2004 12:14 PM
append_features(mod) -- mod.kind_of? makes absolutely no sense T. Onoma Ruby 9 12-15-2003 03:34 AM



Advertisments