In arithmetic and algebra, certain rules are used for the order in which the operations in expressions are to be evaluated. These precedence rules (which are mere notational conventions, not mathematical facts) are also used in many programming languages and by most modern calculators. In computing, the standard algebraic notation is known as infix notation. This article assumes the reader is familiar with addition, subtraction, multiplication, division, exponents, and roots (such as square roots, cube roots, and so on).
The order of operations is expressed in the following chart.
In the absence of parentheses, do all the exponents and roots first. Stacked exponents must be done from right to left. Root symbols have a bar over the radicand which acts as a symbol of grouping. Then do all the multiplication and division, from left to right. Finally, do all of the addition and subtraction, from left to right. If there are parentheses, in arithmetic do the expression inside the innermost parentheses first, and work outward. In algebra, the distributive law can sometimes be used to remove parentheses. The chart which gives the order of operations can help in remembering that roots and exponents distribute over multiplication and division, while multiplication and division distribute over addition and subtraction.
Order of operations is only important when an expression has a symbol for an operation both to its left and to its right. (Roots are really a special case, since the root symbol applies to everything under the bar across the top, which is part of the root symbol and also a symbol of grouping.) Exponents take precedence over all other operations, including the unary operation of taking the opposite. Thus, to evaluate -x2 we first square x and then take the opposite of the result. If x is 3, the evaluation is -9. Exponents, however, are always written to the right of their base, never to the left. Thus the expression x2y means the square of x multiplied by y. Multiplication and division signs can be written either to the left or the right of an expression, and the same is true of addition and subtraction signs. A multiplication on the left followed by a division on the right can be done in any order. For example, 8×4/2 = 32/2 = 16 is the same as 8×4/2 = 8×2 = 16. The reverse is not true, however. In 8/4×2, the division must be carried out before the multiplication, or a wrong answer results. Multiplication and division always take precedence over addition and subtraction. An addition sign on the left followed by a subtraction sign on the right can be done in any order. For example, 8+4-2 = 12-2 = 10 is the same as 8+4-2 = 8+2 = 10. The reverse is not true, however. In 8-4+2 the subtraction must be carried out first, or a wrong answer results.
Most programming languages conform to mathematical order of operations. The order is as follows:
| 1 | () * -> . :: ++ -- | Grouping | |
| 2 | ! ~ ++ -- - + * & | Logical negation | |
| 3 | * / % | Multiplication, division, modulus | |
| 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 | = += -= *= /= %= &= ^= <<= >>= | Assignment operators |
The way this works is that the symbols are evaluated first in order. This can be thought of as a way to know how to add parentheses around an expression so you know what it means. Add the parentheses to group similar things, in the order from the top of the table to the bottom.
Examples:
The expression: 2 + 3 × 4 is evaluated to 14, and not 20, because multiplication precedes addition. If the intention is to perform the addition first, parentheses must be used: (2 + 3) × 4 = 20.
In Canada, an acronym BEDMAS is often used as a mnemonic for Brackets, Exponents, Division, Multiplication, Addition, and Subtraction.
In the UK, Australia and New Zealand, the acronym BODMAS is commonly used for Brackets, Orders, Division, Multiplication, Addition, Subtraction, or also, Brackets, Of, Division, Multiplication, Addition, Subtraction.* ('of' meaning multiply in this context) Since multiplication and division are of the same rank, this is sometimes written as BOMDAS, BIDMAS or BIMDAS where I stands for Indices.
In the US, the acronym PEMDAS (for Parentheses, Exponentiation, Multiplication/Division, Addition/Subtraction) is used instead, sometimes expressed as the mnemonic "Please Excuse My Dear Aunt Sally".
Warning: Although parentheses precede exponentiation, multiplication and division are of equal precedence, and addition and subtraction are of equal precedence. Using any of the above rules in the order addition first, subtraction afterward would give the wrong answer to
If the addition were done before the subtraction, it would give the wrong answer 5 rather than the correct answer 9. The correct answer is found by performing all additions and subtractions left-to-right.
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 rules 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:
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:
3) Whenever there is a trig function, put the argument of the function, typically shown in bold and/or italics, in parentheses:
4) The rule for trig functions also applies to any other function, such as square root. That is, the argument of the function should be contained in parentheses:
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:
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:
While a printed text may have:
6) Whenever anything can be interpreted multiple ways, put the part to be done first in parentheses, to make it clear.
7) You may alternate use of the different grouping symbols (parentheses, brackets, and braces) to make it more readable. For example:
is more readable than:
Note that certain applications, like computer programming, will restrict you to certain grouping symbols.
In the case of a factorial in an expression, it's evaluated before exponents and roots, unless grouping symbols dictate otherwise. When new operations are defined, they are generally presumed to take precedence over other operations, unless overridden by grouping symbols. In the case where repeated operators of the same type are used, such as in
the expression is evaluated from left to right, as
(Recall that exponents are an exception, always evaluated from right to left.)
Operatorprioritet | Operatorrangfolge | Ordine delle operazioni | Rekenregel | Ordem de operações | İşlem sırası
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Order of operations".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world