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?
If the numerator is smaller than the denominator, then the remainder is equal to the numerator. 3 % 10 =3 //If smaller number i.e. 3 is divided by bigger number i.e. 10, then the numerator becomes the remainder.
Most programming languages adopt the convention that the modulo operator (denoted by % rather than mod ) occupies the same place in the order of operations as multiplication and division. Hence, it comes AFTER the operations in parentheses, but BEFORE addition and subtraction.
Answer. No, the result of a modulo operation can never be larger than the divisor, assuming all positive values being used in the operation. If the division is done correctly, then the divisor was fitted into the dividend as many times as possible.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With