Modulo in the Order of Operations

Created by Anna Szczepanek, PhD
Reviewed by Rijk de Wet
Last updated: Nov 21, 2022

Have you ever wondered how modulo fits into the order of operations? You're in the right place — in this article, we explain everything you need to know about PEMDAS and modulo. Not sure what PEMDAS is? Keep reading!

What is the modulo operator?

The modulo operator returns the remainder of the division of one number by some other number. Remember, we're dealing with integer numbers here. In mathematical notation, if a and n are two integers, we can always find b and r such that

a = b * n + r

where the remainder r satisfies 0 ≤ r < n. Then a mod n = r.

Alternatively, we can say that a mod n = r if and only if n divides a−r without remainder.

For instance:

  • 21 mod 5 = 1 because 21 = 4 * 5 + 1

  • 23 mod 10 = 3 because 23 = 2 * 10 + 3

  • 3 mod 10 = 3 because 3 = 0 * 10 + 3

Is modulo considered the same as division?

Modulo is related to division, but is not exactly the same. Let's see:

7 / 2 = 3.5

is different from

7 mod 2 = 1.

There's also the integer division operation (often denoted by the double slash //, especially in programming):

7 // 2 = 3.

The relation between integer division and modulo can be expressed by the following relationship:

7 = 3 * 2 + 1.

As you can see, the integer division 7 // 2 answers the question "How many times does 2 fit into 7?" and 7 mod 2 answers the question "What is the remainder when we divide 7 by 2?"

Where is modulo in the order of operations?

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. When there's modulus and multiplication or division, then the operations are performed from left to right.

For instance:

  • 2 * 3 % 4 will resolve to 2, because we have 2 * 3 = 6 and 6 % 4 = 2.

  • 3 % 4 * 2 will resolve to 6, because we have 3 % 4 = 3 and 3 * 2 = 6.

However, in mathematics, we would sometimes give precedence to the modulo operator before multiplication and addition. This is because mod n is considered as indicating the environment in which we perform the calculations — in proper mathematical terms, we call this the ring. Hence, many mathematicians would agree the expression 3 mod 4 * 2 implies that we are performing computations modulo 8, so the result would be 3.

As you can see, it's not always obvious where modulo is situated in the order of operations. If you're in doubt when dealing with a new programming language, just consult the documentation or perform a quick check asking your computer (or calculator) to evaluate a few examples. This might require writing a little code, but your problem will be solved immediately. In math, to avoid confusion, use parentheses. When facing a confusing expression, check the context or ask your colleagues for clarification.

Where is modulo in PEMDAS?

PEMDAS is an acronym that stands for Parentheses, Exponents, Multiplication / Division, Addition / Subtraction. It encodes the order of precedence when performing arithmetic operations.

Some countries use the acronym BEDMAS, standing for Brackets, Exponents, Division/Multiplication, Addition/Subtraction. Other variations are also in use.

Note that in the two versions we evoked, the order of multiplication and division seems to be reversed. This is not the case! Remember that multiplication and division have equal precedence: they occupy the same place, and you perform them from left to right to get the correct answer. The same rule holds for addition and subtraction.

So, where is the modulo operator in the PEMDAS scheme? As you can see, it does not appear there. One reason is that kids learn about PEMDAS several years before they learn about modulo. Another reason is that there are different conventions about where mod should fit into the order of precedence, and it is highly context-dependent. One sure thing is that most programming languages put modulo at the same level as multiplication and division.

Anna Szczepanek, PhD
x mod y = r
x (dividend)
y (divisor)
r (remainder)
Check out 62 similar arithmetic calculators ➗
Absolute changeAbsolute valueAddition… 59 more
People also viewed…

Adding fractions

Estimate the sum of two, three, four, or five fractions using the adding fractions calculator.

Complex number

The complex number calculator helps you to find the outcome of basic operations of complex numbers. It is possible to choose between complex numbers' algebraic and polar forms.

Helium balloons

Wondering how many helium balloons it would take to lift you up in the air? Try this helium balloons calculator! 🎈

Podcasts

Do you feel like you could be doing something more productive or educational while on a bus? Or while cleaning the house? Well, why don't you dive into the rich world of podcasts! With this podcast calculator, we'll work out just how many great interviews or fascinating stories you can go through by reclaiming your 'dead time'!
Copyright by Omni Calculator sp. z o.o.
Privacy policy & cookies
main background