Arithmetic Operations on a Computer
Order of Operations
A computer can add, subtract, multiply, and divide numbers. The symbols for these operations are +, −, *, and / respectively. Some computer languages, such as those typically used in spreadsheets, also allow exponentiation. Its symbol is ^ . A mathematical expression can contain more than one operation, representing a series of computations; we evaluate an expression (that is, compute its result) by performing one operation at a time, with the result of each operation used in the next. In these cases it's important that we agree what order the operations are performed in, or else a single expression might yield different results.
Most computer languages use the same precedence of operations as mathematics. Exponentiation is done first in a string of operations, with multiplication and division next, and addition and subtraction last. This is also called the hierarchy of operations.
|Next highest||*, /|
For example, consider the algebraic expression:
Most people would recognize that this means to square w, multiply y and z, and add x, yz, and w2 together. If x = 1, y = 2, z = 3, and w = 4, for instance, the expression evaluates to
When operations have the same precedence, such as an addition and subtraction, or multiple subtractions, they are done from left to right. The left-to-right order is largely academic in some cases, such as multiple additions, because (since addition is commutative and associative) you get the same answer either way. But in some cases, the order makes a difference in the final result; so we understand that the order of operation within a level of the hierarchy is always left-to-right.
Example: Evaluate 5 + 3 − 4, 5 − 3 + 4, and 5 − 3 − 4.
Solution: In all three cases, we proceed left-to-right. In the first expression, we first perform the addition, so the result is 8 − 4 = 4.
In the second, we perform the subtraction first, giving us 2 + 4 = 6.
In the third, we perform the left subtraction (of 5 − 3) first, giving 2 − 4 = −2. Note that if we had done the other subtraction first, the final result would have been 6. Remember, unless the hierarchy says to do one of the operations first, always proceed left to right.
To specify a different order of operations, we use parentheses.
Example: Evaluate 10 − 6 / 2, (10 − 6) / 2, and 10 − (6 / 2).
Solution: In the first expression, do the division first, because it has higher precedence than subtraction. The result is 10 − 3 = 7 .
In the second expression, the parentheses force the subtraction to be done first: 4 / 2 = 2.
The third expression evaluates to 10 − 3 = 7 , the same as the first expression. The parentheses are not needed, but it is not incorrect to include them.
You may have learned the simple acronym PEMDAS, or a longer mnemonic sentence, to remember the order of operations. If so, you might want to mentally break that up as P-E-MD-AS; otherwise you're liable to try to always (M)ultiply before (D)ividing, when those two operations are at the same level and should be processed left-to-right. This is a very common source of error in these kinds of problems.
In-line ExpressionsOn a computer, there are limitations to the manner in which expressions can be entered. We must create in-line expressions that are just a sequence of characters (keystrokes at the keyboard). Some rules:
- No simple juxtaposition. In standard algebraic notation, xy means to multiply x and y together. For the computer, we must place an asterisk to indicate multiplication. For example: x * y
- Exponentiation: x2 is not a simple sequence of characters, and many programming environments don't support superscripts easily, or at all. Thus, we represent this operation with a caret: x ^ 2
- Fractions and divisions are written for the computer as in-line
expressions, not in built-up form:
, for example.
|Built-up algebraic expression||Creating the in-line expression equivalent|
|x + yz + w2||The in-line expression equivalent would be: x + y * z + w^2|
|The inline form is (x + y) / 3. Parentheses are required around the numerator. Otherwise, only y would be divided by 3!|
|Exponentiation has the highest precedence, and division and multiplication have the same precedence. For emphasis, we could place parentheses around y2 and 4/3 giving (4/3) * x * (y^2): but they are not needed. The expression 4/3 * x * y^2 is simpler and is equivalent.|
|Parentheses are needed around the numerator and around the denominator. Do not forget the multiplication symbol between 2 and y. The result is (x+3)/ (2*y−5)|
|In-line expression||Built-up algebraic expression|
|x − y / z + w||Since division has precedence over subtraction
and addition, only y is divided by z:
|(x − y) / z + w||x − y is divided by z.
w is not part of the fraction:
|(x − y) / (z + w)||w is part of the denominator in this
ExercisesIn the first group of exercises, evaluate (compute the value of) the expression written in in-line notation.
- 1 + 2 * 3
- (1 + 2) * 3
- (4 + 3) ^ 2
- 4 + 3 ^ 2
- 5 * 6 ^ 2
- (5 * 6) ^ 2
- 1 + 2 * 4 − 10 / 2
- (1 + 2) * 4 − 10 / 2
- (1 + 2) * (4 − 10) / 2
- 1 + (2 * 4) + (10 / 2)
- a2 − 2ab + b2
a a + 1
- P(1 + r)t
- P(1 +
a + b a − b
a + b a
- a +
b a − b
- a +
- b ^ 2 − 4 * a * c
- p * (1 + r) ^ t − p
- (b − a) / a * 100
- a / 2 − b / 3
- x + y / z + w
- (x + y) / z + w
- x + y / (z + w)
- (x + y) / (z + w)
Credits and licensing
This article is by Robert P. Webber, Scott McElfresh, and Don Blaheta, licensed under a Creative Commons BY-SA 3.0 license.
Version 2016-Jan-29 03:15