What modulo does
The modulo operator % gives the remainder of a division, the part that does not fit evenly. If you divide 15 by 4, the divisor fits 3 times (that is 12), and 3 is left over. So 15 % 4 is 3.
println(10 % 2)
println(15 % 4)
println(20 % 3)
0
3
2
It works with variables in the same way:
var a = 2
var b = 4
println(b % a)
println(11 % b)
0
3
A remainder of 0 means the first number divides evenly by the second. That makes x % 2 == 0 the usual test for an even number.
Quotient and remainder together
In Scala, / on two Int values gives the quotient and % gives the remainder. They always fit together: the divisor times the quotient, plus the remainder, gives back the original number.
val quotient = 26 / 3
val remainder = 26 % 3
println(quotient)
println(remainder)
println(quotient * 3 + remainder)
8
2
26
Dividing 26 by 3 gives 8 groups of 3, which is 24, and 2 is left over. The last line confirms the identity.
Details specific to Scala
- Negative numbers. The result takes the sign of the left operand:
-7 % 2is-1, and7 % -2is1. If you need a non-negative remainder for a positive divisor, useMath.floorMod(-7, 2), which gives1. - Doubles.
%also works on decimal numbers:5.5 % 2is1.5. It does the same job, the part left after taking away whole multiples of the divisor. - Zero divisor.
10 % 0withIntvalues throwsjava.lang.ArithmeticException: / by zero. With aDoublezero it givesNaN. - Precedence.
%has the same rank as*and/, so it is applied before+and-, and from left to right among equals.
You may meet examples that use Math.floor to compute a quotient. In Scala this is not needed for integers, because / on Int values already gives the whole quotient. Math.floor returns a Double, so Math.floor(26 / 3) prints 8.0 rather than 8.
Two common uses
Even or odd. Every even number leaves remainder 0 when divided by 2, and every odd number leaves 1, so 10 % 2 == 0 is true and 7 % 2 == 0 is false. For an odd test on possibly negative numbers, compare with 0 instead: x % 2 != 0, because -7 % 2 is -1, and -7 % 2 == 1 is false.
Wrapping around. A value that cycles, such as the hours on a 24-hour clock, can be kept in range with %. Five hours after 22:00 is (22 + 5) % 24, which is 3, so 03:00.
The exercise
Andy has amount dollars and each product costs price dollars. He buys as many products as he can. The money left after that is whatever does not divide evenly by the price, which is the remainder. For 70 dollars and a price of 15, he buys 4 products, which cost 60, and 10 dollars remain:
val amount = 70
val price = 15
println(amount % price)
The output is:
10
Both values are inferred as Int, which is why % gives an exact whole-number remainder. The number of products itself would be amount / price, which is 4.
Common mistakes
- Using / instead of %.
amount / priceis the number of products, 4, not the money left. - Swapping the operands.
price % amountis 15 % 70, which is 15. The left operand is the number being divided. - Expecting a percentage. The symbol is the same in other contexts, but in Scala
%always means remainder.
Key takeaways
a % bis the remainder of dividingabyb.- For
Intvalues,a / banda % bare the quotient and the remainder. - A remainder of 0 means the division is exact.
