Modulus division when first number is smaller than second number

12,584

Solution 1

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.

Solution 2

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

Solution 3

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"

Solution 4

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)

Share:
12,584
Jessica M.
Author by

Jessica M.

Student in California

Updated on June 07, 2022

Comments

  • Jessica M.
    Jessica M. about 2 years

    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?

  • Jessica M.
    Jessica M. about 11 years
    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.
  • misberner
    misberner about 11 years
    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)
  • Jessica M.
    Jessica M. about 11 years
    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?
  • Ravi Trivedi
    Ravi Trivedi about 11 years
    It is also called Modulo operator !
  • misberner
    misberner about 11 years
    @Ravi: This is not true, there is a difference between Modulus and Remainder (cf. for example blogs.msdn.com/b/ericlippert/archive/2011/12/05/…). 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.
  • Ravi Trivedi
    Ravi Trivedi about 11 years
    @misberner, In java, it is definitely referred as modulo. What you are saying is true only when it comes to the world of mathematics.
  • misberner
    misberner about 11 years
    @Ravi you are definitely wrong (if you take Oracle as the reference), check docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html
  • Ravi Trivedi
    Ravi Trivedi about 11 years
    @misberner, humm, so its not an official term. Thanks for that ref.
  • misberner
    misberner about 11 years
    @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.
  • Vinay Shukla
    Vinay Shukla over 9 years
    What will happen if it is 40%160
  • Jean-Bernard Pellerin
    Jean-Bernard Pellerin over 9 years
    @yanivx same thing, 0 remainder 40, so answer is 40
  • misberner
    misberner about 9 years
    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, docs.oracle.com/javase/7/docs/technotes/guides/language/… , section "Internal Invariants": "... as the % operator is not a true modulus operator, but computes the remainder ..."
  • MaxZoom
    MaxZoom about 9 years
    @misberner Lets agree on one thing: there is no modulus operator in Java only remainder operator %
  • Java
    Java about 4 years
    The result is the first nr. To example 3567 mod 9886789 = 3567
  • Vladislav Sorokin
    Vladislav Sorokin almost 4 years
    "While a < b, the result of a % b will be "a"" should be the first answer to this question, thanks!