# Modulo operations

posted 6 years ago

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

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.

posted 6 years ago

If you use Long_division...

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 - a free SCJP / OCPJP exam simulator

What would SCJP exam questions look like? -- OCPJP Online Training -- Home -- Twitter -- How to Ask a Question

Nicola Garofalo

Ranch Hand

Posts: 308

posted 6 years ago

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)

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

posted 6 years ago

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.

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.

When you do things right, people won't be sure you've done anything at all.

Mike Simmons

Ranch Hand

Posts: 3090

14

posted 6 years ago

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

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.

posted 6 years ago

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?

So saying that the modulo is "the remainder" is not correct?

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 hasalwaysbeen 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?

When you do things right, people won't be sure you've done anything at all.

Mike Simmons

Ranch Hand

Posts: 3090

14

posted 6 years ago

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.

Yes - what's left over from the

Obviously, I still disagree.

It

0 mod 15 = 0 % 15 = 0. Which is the remainder.

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.

posted 6 years ago

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.

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.

When you do things right, people won't be sure you've done anything at all.

posted 6 years ago
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

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.

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:

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

Posts: 49466

64

posted 6 years ago

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

Posts: 49466

64