19

I apologize if this is a simple question but I'm having trouble grasping the concept of modulus division when the first number is smaller than the second number. For example when 1 % 4 my book says the remainder is 1. I don't understand how 1 is the remainder of 1 % 4.
1 / 4 is 0.25. Am I thinking about modulus division incorrectly?

Jean-Bernard Pellerin
  • 12,556
  • 10
  • 57
  • 79
Jessica M.
  • 1,451
  • 12
  • 39
  • 54

6 Answers6

9

First, in Java, % is the remainder (not modulo) operator, which has slightly different semantics. That said, you need to think in terms of integer-only division, as if there were no fractional values. Think of it as storing items that cannot be divided: you can store zero items of size 4 in a storage of overall capacity one. Your remaining capacity after storing the maximum number of items is one. Similarly, 13%5 is 3, as you can fit 2 complete items of size 5 in a storage of size 13, and the remaining capacity is 13 - 2*5 = 3.

misberner
  • 3,488
  • 20
  • 17
  • 2
    i understand remainder division when the first number is bigger than the second. So i get how 13 % 5 is 3. What I don't understand is what the answer would be if when 5 % 13. – Jessica M. May 01 '13 at 01:58
  • If your item is bigger than your capacity, you can never fit it into your storage. Therefore the result is ALWAYS your overall capacity, which remains unused (i.e., the first number - you probably meant `5 % 13`) – misberner May 01 '13 at 02:00
  • Yes i did mean 5 % 13 and not the other way around. So if I get what you're saying the answer to 5 % 13 is 5? Is that correct? – Jessica M. May 01 '13 at 02:02
  • 2
    It is also called Modulo operator ! – Ravi Trivedi May 01 '13 at 02:03
  • 2
    @Ravi: This is not true, there is a difference between Modulus and Remainder (cf. for example http://blogs.msdn.com/b/ericlippert/archive/2011/12/05/what-s-the-difference-remainder-vs-modulus.aspx). It affects whether the sign of the result follows the dividend or the divisor in case of negative values. Javas % operator implements the remainder semantics. – misberner May 01 '13 at 02:07
  • @misberner, In java, it is definitely referred as modulo. What you are saying is true only when it comes to the world of mathematics. – Ravi Trivedi May 01 '13 at 02:12
  • 3
    @Ravi you are definitely wrong (if you take Oracle as the reference), check http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html – misberner May 01 '13 at 02:14
  • 1
    @misberner, humm, so its not an official term. Thanks for that ref. – Ravi Trivedi May 01 '13 at 02:22
  • @Ravi you're welcome, I only discovered it recently for myself when I stumbled upon a mismatch between constraint solver and Java semantics when dealing with negative values (and discovered that the constraint solver supported both MOD and REM operators). In C/C++ % as far as I know is the modulus operator, but you can easily check for yourself that the behavior between C/C++ and Java versions of % differ when negative operands are involved. – misberner May 01 '13 at 02:24
  • I don't feel qualified to say that they *must not* be interchanged, but the distinction in the official docs is enough reason to believe that they *should not* be interchanged. See, for example, http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html , section "Internal Invariants": "... as the % operator is not a true modulus operator, but computes the remainder ..." – misberner Jun 08 '15 at 21:32
  • @misberner Lets agree on one thing: there is no modulus operator in Java only remainder operator `%` – MaxZoom Jun 08 '15 at 22:20
  • The result is the first nr. To example 3567 mod 9886789 = 3567 – Java bee May 29 '20 at 13:45
7

If you divide 1 by 4, you get 0 with a remainder of 1. That's all the modulus is, the remainder after division.

Jean-Bernard Pellerin
  • 12,556
  • 10
  • 57
  • 79
5

I am going to add a more practical example to what "Jean-Bernard Pellerin" already said.

It is correct that if you divide 1 by 4 you get 0 but, Why when you do 1 % 4 you have 1 as result?

Basically it is because this:

n = a / b (integer), and
m = a % b = a - ( b * n )

So,

 a    b    n = a/b  b * n  m = a%b
 1    4      0        0      1    
 2    4      0        0      2
 3    4      0        0      3
 4    4      1        0      0
 5    4      1        4      1

Conclusion: While a < b, the result of a % b will be "a"

Jorge Casariego
  • 21,948
  • 6
  • 90
  • 97
2

Another way to think of it as a representation of your number in multiples of another number. I.e, a = n*b + r, where b>r>=0. In this sense your case gives 1 = 0*4 + 1. (edit: talking about positive numbers only)

sashkello
  • 17,306
  • 24
  • 81
  • 109
0

I think you are confused between %(Remainder) and /(Division) operators.

When you say %, you need to keep dividing the dividend until you get the remainder 0 or possible end. And what you get in the end is called Remainder.

When you say /, you divide the dividend until the divisor becomes 1. And the end product you get is called Quotient

Ravi Trivedi
  • 2,340
  • 2
  • 14
  • 20
0

Another nice method to clear things up, In modulus, if the first number is > the second number, subtract the second number from the first until the first number is less than the second.

17 % 5 = ?
17 - 5 = 12
12 % 5 = ?
12 - 5 = 7
7 % 5 = ?
7 - 5 = 2
2 % 5  = 2

Therefore 17 % 5, 12 % 5, 7 % 5 all give the answer of 2. This is because 2 / 5 = 0 (when working with integers) with 2 as a remainder.

XcodeNOOB
  • 2,135
  • 4
  • 23
  • 29