TExcelTAD
Home › Algebra › The Order of Operations in Algebra

The Order of Operations in Algebra

The order of operations decides what an expression means: brackets first, then powers, then multiplication and division, then addition and subtraction. With letters in the expression the rules are the same as in arithmetic, but a few cases (negative signs with powers, implied multiplication and fraction bars) trip up both students and programmers. This lesson covers them and compares the rules with Python's operator precedence.

The rules

Rank Operation Notes
1 Grouping: ( ), [ ], fraction bars,   \sqrt{\;} a fraction bar groups its whole top and whole bottom
2 Exponents evaluated right to left: 232=292^{3^2} = 2^9
3 Multiplication and division same rank, left to right
4 Addition and subtraction same rank, left to right

The arithmetic version is in Order of Operations. Here are the algebra-specific points.

Evaluating an expression with variables

Substitute first, using brackets around each value, then follow the order:

3x2−2xy+ywith x=−2, y=5=3(−2)2−2(−2)(5)+5=3⋅4−(−20)+5power first: (−2)2=4=12+20+5=37\begin{aligned} &3x^2 - 2xy + y \quad \text{with } x = -2,\ y = 5 \\ &= 3(-2)^2 - 2(-2)(5) + 5 \\ &= 3 \cdot 4 - (-20) + 5 && \text{power first: } (-2)^2 = 4 \\ &= 12 + 20 + 5 \\ &= 37 \end{aligned}

Without the brackets you might write 3⋅−223 \cdot -2^2, which is a different number.

The negative-power trap

A minus sign in front of a power is not part of the base unless brackets say so:

−x2 with x=3  →  −(32)=−9(−x)2 with x=3  →  (−3)2=9−32  →  −9(−3)2  →  9\begin{aligned} -x^2 \text{ with } x = 3 &\;\to\; -(3^2) = -9 \\ (-x)^2 \text{ with } x = 3 &\;\to\; (-3)^2 = 9 \\ -3^2 &\;\to\; -9 \\ (-3)^2 &\;\to\; 9 \end{aligned}

Python follows the same convention, because ** binds tighter than the unary minus:

python · try it yourself
x = 3
print(-x**2)        # -(x**2)
print((-x)**2)
print(-3**2, (-3)**2)
print(2**3**2)      # right to left: 2**(3**2) = 2**9
-9
9
-9 9
512

Note: Spreadsheets break this rule: in Excel, =-3^2 gives 9, because Excel applies the negation first. Code that translates formulas between spreadsheets and programming languages has to handle this.

Implied multiplication and division

In algebra, 2x2x means 2⋅x2 \cdot x, and ab/cdab/cd usually means (a⋅b)/(c⋅d)(a \cdot b)/(c \cdot d). In a program you must write the multiplication sign and the brackets yourself, and a/b*c means (a/b)⋅c(a/b) \cdot c, because division and multiplication go left to right.

text
written:  6/2x   with x = 3
intended: 6/(2x) = 6/6 = 1        (most textbooks)
in code:  6/2*x  = 3*3 = 9        (left to right)

Avoid the ambiguity altogether: write 6/(2x)6/(2x) or (6/2)x(6/2)x.

Fraction bars and square roots group

A long fraction bar acts as brackets around its numerator and denominator, and a square root sign covers everything under it:

x+6x−1is NOTx+6x−1b2−4acis NOTb2−4ac\begin{aligned} \frac{x + 6}{x - 1} &\quad \text{is NOT} \quad x + \frac{6}{x} - 1 \\ \sqrt{b^2 - 4ac} &\quad \text{is NOT} \quad \sqrt{b^2} - 4ac \end{aligned}

Translating these into code needs explicit brackets. Here is the quadratic formula's discriminant, done wrong and right:

python · try it yourself
import math

a, b, c = 1, -3, -10
right = math.sqrt(b**2 - 4*a*c)
wrong = math.sqrt(b**2) - 4*a*c
print("right:", right)
print("wrong:", wrong)

x = 4
print((x + 6) / (x - 1), x + 6 / x - 1)
right: 7.0
wrong: 43.0
3.3333333333333335 4.5

Python's precedence table

Higher → lower Operators
exponent **
unary -x, +x
multiplicative *, /, //, %
additive +, -
comparison <, <=, ==, !=, >=, >
boolean not, and, or

Because comparisons come after arithmetic, 2*x + 1 > 7 means (2*x + 1) > 7, exactly as in algebra. Bitwise operators such as & and ^ sit between these ranks and catch people out: in Python ^ is XOR, not a power, so 2^3 is 1.

In software

Precedence bugs are among the most common translation errors from a formula to code: a missing pair of brackets around a denominator, -x**2 where (-x)**2 was meant, or a / b * c where a / (b * c) was meant. When in doubt, add brackets; they cost nothing and make the intent visible to the next reader.

Exercises

Exercise 1: Evaluate

Evaluate 2a3−4ab+b22a^3 - 4ab + b^2 for a=−1a = -1, b=3b = 3.

Answer

19

Solution
2(−1)3−4(−1)(3)+32=2(−1)+12+9=−2+12+9=19\begin{aligned} &2(-1)^3 - 4(-1)(3) + 3^2 \\ &= 2(-1) + 12 + 9 \\ &= -2 + 12 + 9 \\ &= 19 \end{aligned}

Exercise 2: Signs and powers

For x=−4x = -4, evaluate (a) −x2-x^2, (b) (−x)2(-x)^2, (c) x2x^2, (d) −x3-x^3.

Answer

(a) −16, (b) 16, (c) 16, (d) 64

Solution
(a) −(x2)=−((−4)2)=−16(b) (−x)2=(4)2=16(c) x2=(−4)2=16(d) −(x3)=−(−64)=64\begin{aligned} &\text{(a) } -(x^2) = -((-4)^2) = -16 \\ &\text{(b) } (-x)^2 = (4)^2 = 16 \\ &\text{(c) } x^2 = (-4)^2 = 16 \\ &\text{(d) } -(x^3) = -(-64) = 64 \end{aligned}
python · try it yourself
x = -4
print(-x**2, (-x)**2, x**2, -x**3)
-16 16 16 64

Exercise 3: Fix the code

The formula is m=y2−y1x2−x1m = \frac{y_2 - y_1}{x_2 - x_1}. A student wrote m = y2 - y1 / x2 - x1. With (x1,y1)=(1,2)(x_1, y_1) = (1, 2) and (x2,y2)=(5,10)(x_2, y_2) = (5, 10), what does the wrong line give, and what is mm?

Answer

The wrong line gives 8.6; m=2m = 2.

Solution

Without brackets, the division happens first: 10−2/5−1=10−0.4−1=8.610 - 2/5 - 1 = 10 - 0.4 - 1 = 8.6. With brackets: (10−2)/(5−1)=8/4=2(10 - 2)/(5 - 1) = 8/4 = 2.

python · try it yourself
x1, y1, x2, y2 = 1, 2, 5, 10
print(y2 - y1 / x2 - x1, (y2 - y1) / (x2 - x1))
8.6 2.0

Exercise 4: Right to left

What are 2**2**3 and (2**2)**3 in Python?

Answer

256 and 64

Solution

** groups from the right: 2(23)=28=2562^{(2^3)} = 2^8 = 256, while (22)3=43=64(2^2)^3 = 4^3 = 64.

Test yourself

What is −x2-x^2 when x=5x = 5?

  1. 25
  2. −25
  3. 10
  4. −10

In Python, what does a / b * c mean?

  1. a / (b * c)
  2. (a / b) * c
  3. It is a syntax error
  4. a * c / b only for integers

Which Python line correctly computes x+1x−1\frac{x + 1}{x - 1}?

  1. x + 1 / x - 1
  2. (x + 1) / x - 1
  3. (x + 1) / (x - 1)
  4. x + 1 / (x - 1)