This week's giveaway is in the JDBC forum.We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Devaka Cooray
• Knute Snortum
• Paul Clapham
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• Jeanne Boyarsky
• Bear Bibeault
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Ron McLeod
• Piet Souris
• Frits Walraven
Bartenders:
• Ganesh Patekar
• Tim Holloway
• salvin francis

# Modulus (%) not working as supposed to

Ranch Hand
Posts: 41
It's a short and simple question but it's baffling.
Why does 1%2 output 1?
In mathematical logic it should not. 1 divided by 2 is 0.5. Which means there is 0 remainder. You don't have any remainder when dividing 1 by 2. How can java output remainder of 1 then?

Sheriff
Posts: 6789
469

Suane Mane wrote:In mathematical logic it should not. 1 divided by 2 is 0.5.

But you are not dividing, you use modulus operation in actual question.

What would be 3 % 2 according to you?

Saloon Keeper
Posts: 10251
216
Or even, what's 4%2 then?

Master Rancher
Posts: 4087
47
• 2
0.5 is not a whole number, so 1 divided by 2 is 0 remainder 1.
So the modulus is 1.

Suane Mane
Ranch Hand
Posts: 41

Liutauras Vilda wrote:
But you are not dividing, you use modulus operation in actual question.

What would be 3 % 2 according to you?

In order to find the remainder you need to divide. So you are dividing even if the result required isn't the result of the division. 3%2 would have a remainder 1, yes.

Dave Tolls wrote:0.5 is not a whole number, so 1 divided by 2 is 0 remainder 1.
So the modulus is 1.

I think I get it... I think. So the division going on has to be int division. I did think of that and tried to look at it from that perspective but still couldn't get where the 1 was from because I thought 1/2 would be 0 and then you get no remainder. I need to brush up on what remainder actually means I guess. Got confusing with smaller dividend.
Thank you!

Liutauras Vilda
Sheriff
Posts: 6789
469

Suane Mane wrote:In order to find the remainder you need to divide. So you are dividing even if the result required isn't the result of the division. 3%2 would have a remainder 1, yes.

In which case you'd get 1.5, and not 1, same as in your initial logic.

I think Dave gave an info, that the division is based on how many whole numbers fit. 3 % 2 as in this case, how many 2's fit to 3, so you divide 3 / 2, you get 1 and remainder 1, so you are correct on that one. And now if you get back to your initial example 1 % 2, how many 2's fit to 1? 0. You don't take fractions.

Suane Mane
Ranch Hand
Posts: 41

Liutauras Vilda wrote:I think Dave gave an info, that the division is based on how many whole numbers fit. 3 % 2 as in this case, how many 2's fit to 3, so you divide 3 / 2, you get 1 and remainder 1, so you are correct on that one. And now if you get back to your initial example 1 % 2, how many 2's fit to 1? 0. You don't take fractions.

Yeah, I know there's no fraction in int division. I knew that from the start and still didn't help me understand remainder in any way whatsoever. How many 2s fit in 1 = 0. Just that further than that, I took it as being completely remainder 0. It was confusing.

Saloon Keeper
Posts: 3256
128
It is a well known theorem in algebra. Let a and b be two non negative integers, then there are integers 0 <= k and 0 <= c < a such that

b = k * a + c.

We have that b / a = k, and b % a = c.

For instance, 14 = 3 * 4 + 2, so 14 / 4 = 3 and 14 % 4 = 2.

For negative values, things are a little different (but not much) Try it out on your pc what you get in that case.

Marshal
Posts: 64496
225

Piet Souris wrote:. . . For negative values, things are a little different  . . .

there are two different conventions:-The two use different rounding conventions for integer division. If you can't work out from trial and error which convention Java® uses, as PS suggested, all the gory details are explained in the Java® Language Specification. Note the operator is usually called “remainder” in English and “moduus” in other European languages.

lowercase baba
Posts: 12746
51
Not to beat a dead horse, but by your logic, modulus would never return anything but zero.  12 divided by five would be 2.4, so 0 is the remainder.

15 divided by 8 is 1.875, so zero is the remainder...

etc.

Modus, by definition, is the leftover after integer division.  so 2 goes into 1 exactly zero times, with one left over. just like 5 goes into 12 two times, with two left over...
Staff note (Liutauras Vilda):

Note: this post referring to OP and not the Campbell Ritchie

Campbell Ritchie
Marshal
Posts: 64496
225

fred rosenberger wrote:. . . but by your logic, modulus would never return anything but zero.  . . .

I hae had the same argument from my former supervisor, who said that floating‑point numbers don't have a remainder.

Suane Mane
Ranch Hand
Posts: 41
• 1

fred rosenberger wrote:Not to beat a dead horse, but by your logic, modulus would never return anything but zero.  12 divided by five would be 2.4, so 0 is the remainder.

15 divided by 8 is 1.875, so zero is the remainder...

etc.

Modus, by definition, is the leftover after integer division.  so 2 goes into 1 exactly zero times, with one left over. just like 5 goes into 12 two times, with two left over...

No no like I keep saying, it was the fact the dividend was smaller than the divisor which confused me. Your examples are easy for me to make sense of, but anything with smaller dividend will result in '0.something' and since it's int division, result is just 0. For some reason the result being 0 just confused me about how to process the remainder. Nothing to do with the case of float or not. Just the fact result is 0, whether it's 0.n or just 0.

And I understood it from Dave's explanation. I'm not arguing anything. Not sure where you're getting at.
I just had some confusion about the logic of modulus in cases as such with lower value dividend and higher value divisor. But it's cleared now. I'm not arguing my logic was good. On the contrary, was trying to work out what's wrong.
I get it now - any int division with smaller dividend will yield a remainder equal to the value of the dividend itself.

Campbell Ritchie
Marshal
Posts: 64496
225

Suane Mane wrote:. . . any int division with smaller dividend will yield a remainder equal to the value of the dividend itself.

You need to check that; as I said earlier it differs from language to language, but in the case of Java®, the language specification strictly defines the result.
I see I have made quite a lot of spelling errors in this discussion.

 Let me tell you a story about a man named Jed. He made this tiny ad: how do I do my own kindle-like thing - without amazon https://coderanch.com/t/711421/engineering/kindle-amazon