DEV Community

Hargunbeer Singh
Hargunbeer Singh

Posted on

Remainder VS Modulus

Introduction

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.

Overview

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:
Clock 1
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.

The Negative Number Game

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:
Modulus clock representation
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 -7.


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:
Modulus negative dividend clock
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 2


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.

Which is Which

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 % symbol differently. For example, JavaScript, C++, and C# consider % as remainder operator whereas programming languages like Ruby consider % as modulus.

Where can this knowledge be used

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.

Top comments (1)

Collapse
 
stoiandan profile image
Stoian Dan

Awesome, really helpful!
The way I more easily understand it, is with remainder, you stop at the first multiple that's below your value so -4 % 3 (as reminder), "how many 3s can we fit in -4" well -4 + 3 = -1, and since we can't fit another 3 in -1, we stop we thus get -1 with a reminder of -1, because -1 - (-1) = 0
In modulus, you go to the first multiple above your unsigned value, which would be 6, then subtract the signed value, so 6 - 4 = 2