• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Modulus (%) not working as supposed to  RSS feed

 
Ranch Hand
Posts: 37
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 6697
464
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 10136
214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or even, what's 4%2 then?
 
Master Rancher
Posts: 4042
47
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 37
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Marshal
Posts: 6697
464
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 37
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Master Rancher
Posts: 3189
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 64166
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 12738
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 64166
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 37
IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 64166
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Beware the other head of science - it bites! Nibble on this message:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!