Order of operations
In mathematics and computer programming, order of operations (more formally precedence rule) is a rule used to unambiguously clarify which procedures should be performed first in a given mathematical expression.
For example, in mathematics and most computer languages multiplication is done first; in the expression 2 + 3 × 4, the correct answer is 14. Parentheses, which have their own rules, may be used to avoid confusion, thus the above expression may also be rendered 2 + (3 × 4).
From the earliest use of mathematical notation multiplication took precedence over addition, whichever side of a number it appeared on.[1] Thus 3 + 4 × 5 = 4 × 5 + 3 = 23. When exponents were first introduced, in the 16th and 17th centuries, exponents took precedence over both addition and multiplication, and could be placed only as a superscript to the right of their base. Thus 3 + 5 2 = 28 and 3 × 5 2 = 75. To change the order of operations, originally a vinculum (an overline or underline) was used. Today we use parentheses. Thus, to force addition to precede multiplication, write (2 + 3) × 4 = 20.
The standard order of operations
The standard order of operations, or precedence, is expressed in the following chart.
- terms inside brackets
- exponents and roots
- multiplication and division
- addition and subtraction
This means that if a number or other symbol, or an expression grouped by one or more symbols of grouping, is preceded by one operator and followed by another, the operator higher on the list should be applied first. The commutative and associative laws of addition and multiplication allow the operators +, −, *, and / to be applied in any order that obeys this rule. The root symbol, √, requires either parentheses around the radicand or a bar (called vinculum) over the radicand. Stacked exponents are applied from the top down.
It is helpful to treat division as multiplication by the reciprocal (multiplicative inverse) and subtraction as addition of the opposite (additive inverse). Thus 3/4 = 3 ÷ 4 = 3 • ¼ and 3 − 4 = 3 + (−4), that is, the sum of positive three and negative four.
Symbols of grouping can be used to override the usual order of operations. Grouped symbols can be treated as a single expression. Symbols of grouping can be removed using the associative and distributive laws.
Examples
A horizontal fractional line also acts as a symbol of grouping:
For ease in reading, other grouping symbols (such as curly braces {} or square brackets [] ) are often used along with the standard round parentheses, e.g.
Unfortunately, there exist differing conventions concerning the unary operator − (usually read "minus"). In written or printed mathematics, the expression −32 is interpreted to mean −(32) = −9, but in some applications and programming languages, notably the application Microsoft Office Excel and the programming language bc, unary operators have a higher priority than binary operators, that is, the unary minus (negation) has higher precedence than exponentiation, so in those languages −32 will be interpreted as (−3)2 = 9. [2]. In any case where there is a possibility that the notation might be misinterpreted, it is advisable to use parentheses to clarify which interpretation is intended.
Similarly, care must be exercised when using the slash ('/') symbol. The string of characters "1/2x" is interpreted by the above conventions as (1/2)x. The contrary interpretation should be written explicitly as 1/(2x). Again, the use of parentheses will clarify the meaning and should be used if there is any chance of misinterpretation.
Mnemonics
Mnemonics are often used to help students remember the rules, but the rules taught by the use of acronyms can be misleading. In the United States, the acronym PEMDAS or "Please Excuse My Dear Aunt Sally" is common. It stands for Parentheses, Exponentiation, Multiplication, Division, Addition, Subtraction. In other English speaking countries, Parentheses may be called Brackets, and Exponentiation may be called either Indices, Powers or Orders, and since multiplication and division are of equal precedence, M and D are often interchanged, leading to such acronyms as BEDMAS, BIDMAS, BIMDAS, BODMAS, BOMDAS , BIRDMAS and BPODMAS.
These mnemonics may be misleading, especially if the user is not aware that multiplication and division are of equal precedence, as are addition and subtraction. Using any of the above rules in the order "addition first, subtraction afterward" would give the wrong answer to
The correct answer is 9, which is best understood by thinking of the problem as the sum of positive ten, negative three, and positive two.
There is a new mnemonic featured in Danica McKellar's books Math Doesn't Suck[2] and Kiss My Math[3] that does address this very issue: "Pandas Eat: Mustard on Dumplings, and Apples with Spice." The intention being that Mustard and Dumplings is a "dinner course" and that Apples and Spice is a "dessert course." Then it becomes not a linear string of operations to do one after the other, but rather the "dinner course" operations are considered together and performed left to right, and then addition and subtraction are considered together, again performed again left to right.
In college mathematics, the rules of priority are (usually) taught correctly, and students are taught the commutative law, associative law, and distributive law, which replace the grade school "rules". The "left to right" rule is not a law of mathematics.
For example,
is much easier when worked from right to left, using the associative law of multiplication and treating division as multiplication by the reciprocal.
Proper use of parentheses and other grouping symbols
![]() | This section possibly contains original research. (February 2008) |
When restricted to using a straight text editor, parentheses (or more generally "grouping symbols") must be used generously to make up for the lack of graphics, like square root symbols. Here are some suggestions for doing so:
1) Whenever there is a fraction formed with a slash, put the numerator (the number on top of the fraction) in one set of parentheses, and the denominator (the number on the bottom of the fraction) in another set of parentheses. This is not required for fractions formed with underlines:
- y = (x+1)/(x+2)
2) Whenever there is an exponent using the caret (^) symbol, put the base in one set of parentheses, and the exponent in another set of parentheses:
- y = (x+1)^(x+2)
3) Whenever there is a trigonometric function, put the argument of the function, typically shown in bold and/or italics, in parentheses:
- y = sin(x+1)
4) The rule for trigonometric functions also applies to any other function written in "functional notation", such as "sqrt" for a square root. That is, the argument of the function should be contained in parentheses:
- y = sqrt(x+1)
5) An exception to the rules requiring parentheses applies when only one character is present. While correct either way, it is more readable if parentheses around a single character are omitted:
- y = (3)/(x) or y = 3/x
- y = (3)/(2x) or y = 3/(2x)
- y = (x)^(5) or y = x^5
- y = (2x)^(5) or y = (2x)^5
- y = (x)^(5z) or y = x^(5z)
Calculators generally require parentheses around the argument of any function. Printed or handwritten expressions sometimes omit the parentheses, provided the argument is a single character. Thus, a calculator or computer program requires:
- y = sqrt(2)
- y = tan(x)
while a printed text may have:
- y = sqrt 2
- y = tan x.
6) Whenever anything can be interpreted multiple ways, put the part to be computed first inside parentheses, to make it clear.
7) One may alternate use of the different grouping symbols (parentheses, brackets, and braces) to make expressions more readable. For example:
- y = { 2 / [ 3 / ( 4 / 5 ) ] }
is more readable than:
- y = ( 2 / ( 3 / ( 4 / 5 ) ) )
Note that certain applications, like computer programming, will restrict one to certain grouping symbols.
8) Another way of simplifying reading and/or parsing is to try to avoid the need of parentheses altogether. For example:
- y = 3/(2x) might be written as y = 3/2/x
and
- y = (2x)^5 might be written as y = 32x^5
A possible drawback of this method is that people not used to reading mathematic expressions might not have the same ability to quickly grasp what is written when it's done so in a less intuitive way.
Special cases
An exclamation mark indicates that one should compute the factorial of the term immediately to its left, before computing any of the lower-precedence operations, unless grouping symbols dictate otherwise. But 23! means (23)! = 8! = 40320 while 23! = 26 = 64; a factorial in an exponent applies to the exponent, while a factorial not in the exponent applies to the entire power.
If exponentiation is indicated by stacked symbols, the rule is to work from the top down, thus:
A function name usually applies to the monomial following the name, thus "sin xy" means sin (xy) but sin x + y means (sin x) + y. Calculators usually require parentheses, and parentheses should be used in complicated expressions to prevent misunderstanding.
Sometimes a dash or a heavy dot is used as a multiplication sign which has higher precedence than division. For example, J/kg K, J/kg-K, and J/kg · K are all equivalent.[citation needed]
Calculators
Different calculators follow different orders of operations. Most non-scientific calculators without a stack work left to right without any priority given to different operators, for example giving
while more sophisticated calculators will use a more standard priority, for example giving
The Microsoft Calculator program uses the former in its standard view and the latter in its scientific view.
The non-scientific calculator expects two operands and an operator. When the next operator is pressed, the expression is immediately evaluated and the answer becomes the left hand of the next operator. Advanced calculators allow entry of the whole expression, grouped as necessary, and only evaluates when the user uses the equals sign.
Calculators may associate exponents to the left or to the right depending on the model. For example, the expression a ^ b ^ c on the TI-92 and TI-30XII (both Texas Instruments calculators) associates two different ways:
The TI-92 associates to the right, that is
- a ^ b ^ c = a ^ (b ^ c) = a^(b ^ c) =
whereas, the TI-30XII associates to the left, that is
- a ^ b ^ c = (a ^ b) ^ c =
An expression like 1/2x is interpreted as 1/(2x) by TI-82, but as (1/2)x by TI-83. While the first interpretation may be expected by some users, only the latter is in agreement with the standard rules stated above.
Programming languages
Many programming languages use precedence levels that conform to the order commonly used in mathematics, though some, such as APL or Smalltalk, have no operator precedence rules (in APL evaluation is strictly right to left, in Smalltalk it's strictly left to right).
The logical bitwisenhkk operators in C (and all programming languages that borrowed precedence rules from C, for example, C++, Perl and PHP) have a precedence level that the author of the language considers to be unsatisfactory.[4] However, many programmers have become accustomed to this order. The relative precedence levels of operators found in many C-style languages are as follows:
1 | () [] -> . :: | Grouping, scope, array/member access |
2 | ! ~ - + * & sizeof type cast ++x --x | (most) unary operations, sizeof and type casts |
3 | * / % | Multiplication, division, modulo |
4 | + - | Addition and subtraction |
5 | << >> | Bitwise shift left and right |
6 | < <= > >= | Comparisons: less-than, ... |
7 | == != | Comparisons: equal and not equal |
8 | & | Bitwise AND |
9 | ^ | Bitwise exclusive OR |
10 | | | Bitwise inclusive (normal) OR |
11 | && | Logical AND |
12 | || | Logical OR |
13 | ?: | Conditional expression (ternary operator) |
14 | = += -= *= /= %= &= |= ^= <<= >>= | Assignment operators |
15 | , | Comma operator |
Examples:
- !A + !B (!A) + (!B)
- ++A + !B (++A) + (!B)
- A * B + C (A * B) + C
- A || B && C A || (B && C)
- ( A && B == C ) ( A && ( B == C ) )
References and notes
See also
- Common operator notation (for a more formal description)
- Operator associativity
- Associativity
- Commutativity
- Distributivity
- Operator overloading
- Operator precedence in C and C++
- PEMDAS (an acronym for the order of operations)