The Beauty of ModulusOct 22, 2017
Modulus really is a beautiful little operator. So overlooked, but so useful, that it's managed to find quite a special place in my heart. At it's heart, it's a simple concept - the modulus of two numbers is the remainder of integer division between them. That is, if you have
10 % 3, you divide 10 by 3 to get 3 with a remainder of 1 - that 1 is the result of the modulus operation.
Modulus is represented in most programming languages by either
mod - for the sake of this article, I'll be using the simpler and more common
% form. I'll be using Ruby code to write my examples, but this applies to any programming language at all.
Let's take a look at some of the beautiful tricks you can do with modulus, shall we?
This is actually probably the best known use for modulus - it can be used to easily check if one number is divisible by another.
if n % 2 == 0 puts "n is even" else puts "n is odd" end
This is, for the most part, self explanatory - the remainder of dividing two numbers will only be 0 if the first number is divisible by the second. Simple!
One of the beautiful properties of modulus is that if you make a loop that increments a number and then sets it to itself modulus a maximum, the number will loop between 0 and the maximum. This is a bit clunky to think about in words, so let's write some code:
n = 0 while true n += 1 n = n % 5 puts n end
This will output the following:
0 1 2 3 4 0 1 2 3 4 ...
As you can see, the number now loops between 0 and 4. This is because every time
n hits 5,
n % 5 becomes 0. On the other hand, any time
n is between 0 and 4,
n % 5 is
n isn't divisible by 5.
This becomes very useful for things like game logic (think Snake's looping coordinate system, where going offscreen will lead you out from the other side) and dealing with things like angles (
deg = deg % 360 means you never have to worry about treating numbers above 360 as the corresponding degree less than 360) or time (
minutes % 60 anyone?).
This is one of the more niche uses for modulus, but it's still quite useful to know - modulus can be used to convert numbers from one numerical base to another. For example, let's write a function that converts decimal (base 10) integers to trinary (base 3):
def b10tob3(n) out = "" while n >= 3 out = (n % 3).to_s + out n = n / 3 end out = n.to_s + out if n > 0 return out end
Running a number through this function will return a string containing the same number in base 3 (feel free to boot up an
irb session and double check for yourself). But how does it work?
If you strip out the boilerplate, it essentially comes down to three simple operations, repeated until the number is completed:
nby the desired base (3)
- Prepend the result to the output string
- Integer divide
nby the desired base and update
This makes the process remarkably simple, and quite easy to understand as well. It can even be adapted to start from other bases than 10 with a little clever math.
(Thanks to ncmathsadist on StackExchange for the really great explanation on this one)
I hope I've been able to show you a little bit of the magic that is the modulus operator. It's an extremely useful tool for various mathematical and logical operations, and has myriad uses in simplifying and condensing programming logic. Here's to the remainder (ba dum tss) of your programming careers =)