Remainder and Modulo are two mathematic operations that are highly used in programming and computing in general. They both are represented by
%, but they act differently, their differences are usually overlooked, but they are really important as now knowing the differences between the both might cause production bugs in an application.
Remainder and Modulo operations are a part of modular mathematics. Modular mathematics is usually used in clocks. The remainder is simply the number left after the division of two numbers, for example,
17 % 12 would be
5 as the number left after dividing 17 by 12 is 5. Its representation on a clock would look like this:
As shown in the diagram above, you first took a cyclic turn around the clock which accounted for dividing the dividend by the divisor 12. After a cyclic turn, you are left with 5 so you go forth 5 places, and 5 is the answer of
17 % 12. The Modulus operator would work the same way except that it would act differently when one of the operands is negative.
Remainder and Modulus operators work the same way until and unless they are operated upon negative operands. The remainder operator would operate the negative operands in the same way as it operates the positive operands, it would just not consider the negativity of the number. On the contrary, modulus would operate differently, and here's how.
As proved earlier using clock math diagrams,
17 % 12 would result in 5 as the remainder.
17 % -12 would also result in the result being
5 as after one oscillation around the clock, 5 would be the answer. What about
17 % -12 using a modulo operator? We would need to use a different type of clock to depict the calculations.
So in modular mathematics, a clock hand's clockwise turn always increments the number of hours(value). So for carrying out
17 % -12 we would need a clock that increments the negative number -12, and it would look like this:
In the above figure, the clock is turning clockwise and the number is being incremented, so after a whole oscillation around the clock, we would have 5 left as the remainder, but we are using modulus so we will move the clock hand 5 steps further which results in the result being
The dividend could also be a negative number with the divisor being positive. You would similarly solve that. Suppose
-14 mod 12, so when we represent this on a graph, it would look like this:
In the above figure, we move the clock hand counterclockwise because the dividend is negative. After a single oscillation around the clock we just have 2 counter-clockwise clock hand turns left so the answer of this operation is
10, had the modulus been remainder in this operation, the answer would have been
In other words, the remainder operator returns the number of turns of the clock hand left after $n$ number of complete oscillations. The remainder is a result of modular division. The modulus operator returns the position of the clock hand after moving all the turns left. While using the modulus operator, we have to use all the clock hand turns left and then return the final clock hand position.
A point to be noted is that the clock is only shown because the divisor in all the operations is 12, you would not be able to represent an operation with some other remainder on a clock with 12 positions.
You must be thinking about how to differentiate between the modulus and remainder operator, given that they have the same sign
% in code. The answer is: it depends. It depends on the programming language you are coding in. Different programming languages, consider the
% as remainder operator whereas programming languages like Ruby consider
% as modulus.
Details are important. The details discussed above might make you think that they have no use, but yes they do! Knowing these details might help you with that production bug you just pushed or these details might be asked in a job interview.