Order of operations
From Wikipedia, the free encyclopedia
In arithmetic and algebra, when a number or expression is both preceded and followed by a binary operation, a rule is required for which operation should be applied first. From the earliest use of mathematical notation, multiplication took precedence over addition, whichever side of a number it appeared on. Thus 3 + 4 • 5 = 5 • 4 + 3 = 23. When exponents were first introduced, in the 16th and 17th centuries, exponents took precedence over both addition and multiplication, and could only be placed 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, a vinculum (an overline or underline) was originally used. Today we use parentheses. Thus, if we want to force addition to precede multiplication, we write (3 + 4) • 5 = 35.
Rules for handling other operations are covered in detail below.
Students often have difficulty understanding the order of operations. This problem is exacerbated by the fact that many of the textbooks in use in the United States state the rules in a way that is either unclear or incorrect.[citation needed]
Contents |
[edit] The standard order of operations
The order of operations is expressed in the following chart.
-
-
- exponents and roots
- multiplication and division
- addition and subtraction
-
In the absence of parentheses, do all exponents and roots first. Stacked exponents must be done from the top down. Root symbols have a bar (called vinculum) over the radicand which acts as a symbol of grouping. After taking all exponents and roots, then do all multiplication and division. Finally, do all addition and subtraction.
It is helpful to treat division as multiplication by the reciprocal and subtraction as addition of the opposite. thus 3/4 = 3 ÷ 4 = 3 • ¼ and -4 + 3 is the sum of negative four and positive three.
If an expression involves parentheses, then do the arithmetic inside the innermost pair of parentheses first, and work outward. Sometimes, the distributive law can be used to remove parentheses.
[edit] Mathematical precedence
Most programming languages conform to mathematical order of operations. The order in C-style languages 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 |
Examples:
- !A + !B ========> (!A) + (!B)
- ++A + !B ========> (++A) + (!B)
- A * B + C =======> (A * B) + C
- A AND B OR C ====> (A AND B) OR C
[edit] Examples from arithmetic
- 1. Evaluate subexpressions contained within parentheses, starting with the innermost expressions. (Brackets [ ] are used here to indicate what is evaluated next.)
- 2. Evaluate exponential powers; for iterated powers, start from the right:
- 3. Evaluate multiplications and divisions, starting from the left:
- 4. Evaluate additions and subtractions, starting from the left:
- 5. Evaluate negation on the same level as subtraction, starting from the left:
In the United States, the acronym PEMDAS (for Parentheses, Exponentiation, Multiplication/Division, Addition/Subtraction) is used instead, sometimes expressed as the mnemonic "Please Excuse My Dear Aunt Sally" or "Please Excuse My Deadly Angry Samurai."
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. Since multiplication and division are of the same rank, this is sometimes written as BOMDAS, BIDMAS or BIMDAS where I stands for Indices.
Warning: 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
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.
[edit] More examples
- Given:
- Evaluate the innermost subexpression (7 + 1):
- Evaluate the subexpression within the remaining parentheses (5 − 8):
- Evaluate the power of (−3)2:
- Evaluate the multiplication 9 × (−5):
- Evaluate the subtraction 3 − (−45):
- Evaluate the addition 48 + 3:
[edit] Proper use of parentheses and other grouping symbols
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:
- 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 trig function, put the argument of the function, typically shown in bold and/or italics, in parentheses:
- y = sin(x+1)
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:
- 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 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:
- y = { 2 / [ 3 / ( 4 / 5 ) ] }
is more readable than:
- y = ( 2 / ( 3 / ( 4 / 5 ) ) )
Note that certain applications, like computer programming, will restrict you to certain grouping symbols.
[edit] Special cases
In the case of a factorial in an expression, it is 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
-
- a / b / c
the expression is evaluated from left to right and is said to associate to the left. It is therefore equal to
-
- (a / b) / c.
With index notation for exponentiation, however, exponents are evaluated from right to left.
[edit] Calculators
Different calculators follow different orders of operations. Cheaper caculators 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
Microsoft's calc.exe program uses the former in its standard view and the later in its scientific view.
Calculators may associate exponents to the left or to the right depending on the model. For example, the expression on the TI-92 and TI-30XII (both Texas Instruments calculators) associates two different ways:
The TI-92 associates to the right, that is
whereas, the TI-30XII associates to the left, that is
[edit] See also
- Common operator notation (for a more formal description)
- associativity
- commutativity
- distributivity
[edit] External links
- Order of operations on PlanetMath
- For a rationale behind the use of the order of operations, see Text Savvy.
- For a programming language (C++) order of operations / precedence chart, see C++ Operator Precedence Chart