Publishers of technology books, eBooks, and videos for creative people

Home > Articles

  • Print
  • + Share This
This chapter is from the book

Determining the Order of Operations

Numbers uses rules of precedence and associativity to determine the order in which it evaluates each part of an arithmetic formula. Precedence determines the priority of various operators when more than one operator is used in a formula. Operations with higher precedence are performed first. The formula

=2 + 3 × 4

is 14 rather than 20 because multiplication has higher precedence than addition. Numbers first calculates 3 × 4 and then adds 2.

Operators with lower precedence are less binding than those with higher precedence. Table 4.3 lists operator precedences from most to least binding. Operators in the same row of Table 4.3 have equal precedence.

Table 4.3 Order of Evaluation (Highest to Lowest)

Operator

Description

()

Calculations inside parentheses

-, +, %

Unary negation, unary identity, unary percent

^

Exponentiation

×,÷

Multiplication, division

+, -

Addition, subtraction

Associativity determines the order of evaluation in a formula when adjacent operators have equal precedence. Numbers uses left-to-right associativity for all operators, so

=6 ÷ 2 × 3

is 9 (not 1) because 6 ÷ 2 is evaluated first, and

=2 ^ 3 ^ 2

is 64 (not 512) because 2 ^ 3 is evaluated first.

You can use parentheses to override precedence and associativity rules. Expressions inside parentheses are evaluated before expressions outside them. Adding parentheses to the preceding examples, you get (2 + 3) × 4 is 20, 6 ÷ (2 × 3) is 1, and 2 ^ (3 ^ 2) is 512. It’s good practice to add parentheses (even when they’re unnecessary) to lengthy formulas to ensure your intended evaluation order and make formulas easier to read.

=5 ^ 2 × 4 ÷ 2

is equivalent to

=((5 ^ 2) × 4) ÷ 2

but the latter is clearer.

  • + Share This
  • 🔖 Save To Your Account