Omni Calculator logo

Modulo in the Order of Operations

Created by Anna Szczepanek, PhD
Reviewed by Rijk de Wet
Last updated: Jun 05, 2023


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 75 similar arithmetic calculators ➗
Absolute changeAbsolute valueAdding and subtracting fractions… 72 more
People also viewed…

Flat vs. round Earth

Omni's not-flat Earth calculator helps you perform three experiments that prove the world is round.

Floor function

With the floor function calculator you can discover how this function works and why it is useful.

Harmonic mean

Find the harmonic mean of up to 30 values with this harmonic mean calculator.

Titration

Use our titration calculator to determine the molarity of your solution.
Copyright by Omni Calculator sp. z o.o.
Privacy, Cookies & Terms of Service