wood burning stoves*
The moose likes Beginning Java and the fly likes Modulo operations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Modulo operations" Watch "Modulo operations" New topic
Author

Modulo operations

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

If the modulo expression means to take the remainder of division,

why does:


My assumption was that it would be false because of a remainder of 15.



I liked this applet: http://www.openprocessing.org/visuals/?visualID=3066


When you do things right, people won't be sure you've done anything at all.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3060
    
  38

Janeice DelVecchio wrote:My assumption was that it would be false because of a remainder of 15.


If you use Long_division...


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
If the dividend is a zero and the divisor is finite, the result equals the dividend.

It's a language specification


Bye,
Nicola
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11255
    
  16

When you divide by 15, you can never HAVE a remainder of 15...you can have anything from 0 to 14.

Generally speaking, Y mod X will only give you values from 0 to X-1 (assuming Y is a non-negative integer value)


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 150

If 0 / 15 == 0 (rem 15),

then multiply both side by 15

15 (0/15) == 15 (0)

0 == 0 + 15(remainder)

Thus,
0 != 15

My silly interpretation.

star chaser..
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

So the language specification states that the remainder when dividing 0 by 15 is 0?

I mean, if you do long division, you do have a remainder of 15 (unlike Devaka's beautiful picture shows).

I'm confused why this is and who thought it would be a good plan to say they'll make the expression evaluate differently if it is with a 0 divisor.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3012
    
  10
Janeice DelVecchio wrote:So the language specification states that the remainder when dividing 0 by 15 is 0?

I mean, if you do long division, you do have a remainder of 15 (unlike Devaka's beautiful picture shows).

I'm confused why this is and who thought it would be a good plan to say they'll make the expression evaluate differently if it is with a 0 divisor.

Is 0 the divisor or the dividend? Up until now, I thought we were talking about 0/15 rather than 15/0. For 0/15, 0 is the dividend, and 15 is the divisor. The quotient is 0, and the result is 0.

As a general rule, the dividend should equal the divisor times quotient, plus the remainder. For example if we divide 14 by 3, we get a quotient of 4 with a remainder of 2. If we multiply divisor 3 by quotient 3, we get 12, and if we add the remainder 2, we get 14, which is the dividend.

How does this apply to 0/15? Can we agree the quotient here is 0? If you say the remainder is 15, then when we calculate (divisor * quotient) + remainder we get (15 * 0) + 15 = 15 - that's supposed to add up to the dividend. Except that the dividend was 0. So how could the remainder have been 15? That doesn't work.

Instead, the remainder is 0. Now (divisor * quotient) + remainder = (15 * 0) + 0 = 0, which was the dividend. As we expect. All is right in the world again.

So, defining 0/n to have a remainder of 0 is not just a Java specification thing. It's the way remainder has always been defined, as far as I know.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

Mike Simmons wrote:
So, defining 0/n to have a remainder of 0 is not just a Java specification thing. It's the way remainder has always been defined, as far as I know.


What? I'm not a math major and I'm incredibly confused by your whole post.

If you have no pies and you try to divide them to feed 15 hungry friends, you have no friends fed with a remainder of 15 hungry friends.

A remainder is how much is left over in the dividend after dividing into the divisor, right?
Fifteen goes into zero no times with a remainder of fifteen.


So saying that the modulo is "the remainder" is not correct?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3012
    
  10
Janeice DelVecchio wrote:If you have no pies and you try to divide them to feed 15 hungry friends, you have no friends fed with a remainder of 15 hungry friends.

You have 15 hungry friends, but they aren't the remainder. When you divide 0 pies among 15 friends, they each get 0 pies (the quotient) and you have 0 pies left over (the remainder). The remainder in this problem should be the number of pies left over, not the number of friends.

Janeice DelVecchio wrote:A remainder is how much is left over in the dividend after dividing into the divisor, right?

Yes - what's left over from the dividend. Which was zero pies. The 15 friends were never part of what was being divided up - they were who it was divided up amongst.
Janeice DelVecchio wrote:Fifteen goes into zero no times with a remainder of fifteen.

Obviously, I still disagree.
Janeice DelVecchio wrote:So saying that the modulo is "the remainder" is not correct?

It is correct, at least for positive numbers. (There may be a subtle definition issue for negative numbers; don't remember and don't care at the moment.)

0 mod 15 = 0 % 15 = 0. Which is the remainder.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

This is a very interesting concept. I never considered the remainder being what's left of the top number or the bottom number...
I thought it was what "couldn't fit" and is thereby left over... and I never really thought (since third or fourth grade) about how the number was derived.

I suppose I never thought about it critically. It makes sense.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11255
    
  16

if you have 20 dollar bills and you want to divide them equally among 3 people (let's ignore pennies for this example), you would say

20 dollars divided by 3 people = 6 dollars per person, with 2 dollars left over.

in this example, 20 is the dividend, 3 the divisor, 6 the quotient, 2 the remainder.

Note that the remainder and the dividend have the same 'units' - dollars.

In other words:
  • dividend - number of things to portion out
  • divisor - how many portions you need
  • quotient - how many are in each portion - this is what you get with integer division
  • remainder - how many of the thing are 'left over' when everyone gets an equal share - this is what you get with the mod operator


  • In you example, you have 0 dollars (dividend) to divide among 15 people (divisor). The remainder must be the same units as the dividend.

    so, 0 / 15 means everyone gets 0 dollars, and then you have 0 dollars left over.

    If you had 3 dollars, everyone would still get 0 (since you have to be fair and give everyone the same), and now you have 3 dollars left over.







    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38467
        
      23
    There are several different ways to calculate a remainder; I believe the % operator in Java is "Euclidean", ie remainder + quotient * divisor gives the original dividend back. There is a thing called IEEERemainder, which is used by the BigDecimal class; that is slightly different.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38467
        
      23
    I was mistaken about BigDecimal; the IEEEremainder method is in Math.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Modulo operations