Modulus division when first number is smaller than second number

Jessica M. picture Jessica M. · May 1, 2013 · Viewed 11.8k times · Source

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?

Answer

misberner picture misberner · May 1, 2013

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.