Order of operations

From Free net encyclopedia

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).

Contents

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 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.

Mathematical Precedence

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:

  •  !A + !B =======> (!A) + (!B)
  • ++A + !B =======> (++A) + (!B)
  • A * B + C =======> (A * B) + C
  • A AND B OR C =======> (A AND B) OR C

Examples

1. Evaluate subexpressions contained within parentheses, starting with the innermost expressions. (Brackets [ ] are used here to indicate what is evaluated next.)
<math>(4+10/2)/9=(4+[10/2])/9=[4+5]/9=1 \,</math>
2. Evaluate exponential powers; for iterated powers, start from the right:
<math>2^{3^2}=2^{[3^2]}=[2^9]=512 \,</math>
3. Evaluate multiplications and divisions, starting from the left:
<math>8/2\times3=[8/2]\times3=[4\times3]=12 \,</math>
4. Evaluate additions and subtractions, starting from the left:
<math>7-2-4+1=[7-2]-4+1=[5-4]+1=[1+1]=2 \,</math>

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 Australia and Canada, an acronym BEDMAS is often used as a mnemonic for Brackets, Exponents, Division, Multiplication, Addition, and Subtraction.

In the UK and New Zealand, the acronym BODMAS is used for Brackets, Orders, Division, Multiplication, Addition, Subtraction. 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

<math>10 - 3 + 2 \,</math>

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.

More examples

  • Given:
<math>3-(5-(7+1))^2\times(-5)+3 \,</math>
  • Evaluate the innermost subexpression (7 + 1):
<math>3-(5-8)^2\times(-5)+3 \,</math>
  • Evaluate the subexpression within the remaining parentheses (5 − 8):
<math>3-(-3)^2\times(-5)+3 \,</math>
  • Evaluate the power of (−3)2:
<math>3-9\times(-5)+3 \,</math>
  • Evaluate the multiplication 9 × (−5):
<math>3-(-45)+3 \,</math>
  • Evaluate the subtraction 3 − (−45):
<math>48 + 3 \,</math>
  • Evaluate the addition 48 + 3:
<math>48 + 3 = 51 \,</math>

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.

Special cases

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

<math>a/b/c</math>

the expression is evaluated from left to right, as

<math>((a/b)/c)</math>.

(Recall that exponents are an exception, always evaluated from right to left.)

See also

External links

de:Operatorrangfolge it:Ordine delle operazioni pt:Ordem de operações tr:İşlem sırası