Modulo in the Order of Operations
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 = 1because21 = 4 × 5 + 1 -
23 mod 10 = 3because23 = 2 × 10 + 3 -
3 mod 10 = 3because3 = 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 % 4will resolve to2, because we have2 × 3 = 6and6 % 4 = 2. -
3 % 4 × 2will resolve to6, because we have3 % 4 = 3and3 × 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 that 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.
🙋 Feel free to check our addition calculator, division calculator, multiplication calculator, and exponent calculator to help you with that!
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.
FAQs
- What does the Modulo in the Order of Operations do?
- We explain how modulo fits into the order of operations, in particular into the PEMDAS scheme. Use the calculator above for instant results in your browser.
- Is the Modulo in the Order of Operations free to use?
- Yes. All Try To Calculator tools are free and do not require an account.
- Are my inputs stored or sent to a server?
- No. Calculations run locally in your browser. We do not collect the numbers you enter or the results shown.
- Can I use the Modulo in the Order of Operations for professional decisions?
- This tool is for education and quick estimates. For medical, legal, tax, or financial decisions, verify results with a qualified professional.
- Where can I find related calculators?
- Browse more Math tools on Try To Calculator at /math, or use the related calculators section on this page.
Related calculators
Remainder
Use the remainder calculator to find the quotient and remainder of division.
Math
Slope
Calculate the slope of a line from two given points using the slope calculator. Discover the slope formula and learn how to find a slope with two points.
Math
Average
Calculate the average (mean) with our user-friendly tool. Discover how to find averages or perform quick calculations effortlessly.
Math
Circumference
Use this free circumference calculator to find the area, circumference and diameter of a circle.
Math