Out of Order
February 8, 2022 10:42 pm
Social media is filled with posts trying to catch people out on their ignorance (or forgetfulness) of the arithmetic order of operations. Everyone learns this at some point in their education, but most people rarely have to think about it in daily life. Even so, you would think that after years of seeing these same problems pop up again and again, everyone would remember what the order of operations were.
Usually, these problems are set up to confuse people on the order of multiplication and division in relation to addition and subtraction. The most recent problem I saw of this type was this:
1 + 2 x 3 – 4 + 5 = ?
I saw two wrong answers with the most frequency. If you go straight left to right, you get an answer of 10. A number of others I saw answered -2. The only way I can think they got this is by doing multiplication first, then addition, then subtraction. (In actuality, addition and subtraction have the same precedence.)
The answer to this problem is 8: multiply 2 x 3 first to get 6 and then proceed with the rest of the operations left to right:
1 + 6 – 4 + 5 = 8
This is all well and good, but there is another way of writing this problem (actually, there are more, but I’m only going to talk about one) that makes order of operations totally unnecessary. And it has to do with Nanyan Numerals. But first, let’s define the issue.
All (or nearly all) education in math is begun using infix notation: that is, the operator comes between the two operands. When we see 2 x 3, we know that we’re supposed to multiply the number on the left by the number on the right. This has the advantage of being easy to understand, especially when we see longer sequences. Sequences like 2 x 3 x 5 x 7 are easily understood moving left to right. We even say “2 times 3,” so this notation makes sense lexically.
The trouble comes in when we mix operands—worse still when you want to perform operations in a particular order. For that, most everyone is aware of the existence of parentheses. Few people seem to forget (at least) that you must do parentheses first. Infix notation would be useless without them. For example, say you’re trying to perform an operation like this:
(1 + 2) x (3 + 4)
In this case, we want to add 1 and 2 and multiply it by the sum of 3 and 4. If you were to try to write this out without parentheses, how would you do it? If you were to forgo the order of operations, there is no way to perform this function: you must keep track of the two sums separately so you can multiply them. Infix notation is simple to read, but it creates a number of issues that must be solved with (special notation).
But there is another way, and that way (among others) is postfix notation.
If you’ve looked at explanation of Nanyan Numerals, then you will already have confronted the idea of postfix notation. To put it simply, the operand is placed after the two operands. Infix 2 x 3 becomes postfix 2 3 x. That may seem strange to see, but it’s really very simple.
To start, imagine a line at which the numbers will queue up. It’s empty right now, but as we go through the sequence, we’ll fill it up. As we go left to right, put any numbers we find on this line in the order we found them. First we find a 2 and then a 3, so our line looks like this:
Remember, these are not digits: they’re separate numbers.
Now when we come to an operator, we go backwards starting at the right and we remove the first two numbers we come across—first 3 and then 2. Then we perform the operation (multiplication in this case) and we get 6. We then put this product at the end of our line. Since we removed the only numbers in the list, the list now only contains a 6.
And that’s our answer.
This list is actually a form called a stack. In a stack, the last element to be added is the first element to be removed. Think of it like a stack of paper: the last sheet of paper you add to the stack is the first sheet you take off. The advantages of this method become evident with longer sequences.
Take the problem above: (1 + 2) x (3 + 4). There was no way in infix notation to write this sequence without using parentheses. However, in postfix, we can write this problem like so:
1 2 + 3 4 + x
Thanks to the commutative property, the answer will be the same. Here’s now we arrive at the answer.
First, following left to right, we push the two numbers on the stack.
Then when we come to an operator, we pop the last two numbers, add them, and push that result to the stack
Then, we find more numbers, and we push those to the stack in order, which leaves us with
3 3 4
Now we come to another operator (+) so we pop the last two operands, add those together and push the result to the stack. Now we have
Moving along, we hit another operator (that’s two in a row), and just our luck, we have two operands on the stack. Multiply those together, push to the stack, and when get our answer:
Simple enough? Let’s make it harder.
Making It Harder
What would you do with this:
24 3 – 3 /
It’s not so difficult with addition and multiplication, but with subtraction and division, the order of the operands matters. So what do we do?
First, start same as before: push the numbers to the stack until we come to an operator.
Now we have a subtraction operator. Pop the two numbers off the stack and we have a 3 and a 24. Which do we subtract from which? In infix, this would be 24 – 3: the first number off the stack is the subtrahend (3) and the second number off the stack is the minuend (24). If it’s easier to remember, think of them being written in the same order in infix: 24 – 3 becomes 24 3 – in postfix.
Now push our result (21) to the stack
Continuing along, we push the 3 to the stack.
Now we come to another operator—a division operator (/). What order do we do the division? Well, the first number popped off (3) is the divisor and the second number (21) is the dividend. Again, think infix: we would write it 21 / 3 so in postfix it is 21 3 /. The quotient is 7, so push that to the stack and we have our answer.
Now with all that said, let’s go back to the original problem and see how we would write it in postfix notation.
The Original Problem
How would we write the original problem in postfix notation? Well, there are a number of ways (just as there were many ways to write it in infix notation), but since the digits have a nice progression to them, I’m going to keep that, leaving me with 1 2 3 4 5, but where do the operators go? The simplest way is to put them like so:
1 2 3 x + 4 – 5 +
That should get us to the original answer of 8. Here’s how:
Push the numbers to the stack until we hit an operator:
1 2 3
We hit a multiplication operator, so we pop off the last two numbers (3 and 2), multiply them together (6) and push the result on the stack, leaving us with
Continuing on, we come immediately to another operator (+). Pop the last two, add them, push to stack. Now all we have is
Next, we have a number so push that to the stack
Subtraction operator next. The order of these operands means we subtract 4 from 7 and we get
Now push the next number
The next item is an operator (+) so we add 3 and 5 and push to the stack to get our answer.
See? Unambiguous. No order of operations or parentheses needed. It’s a bit harder to understand at first, but in the end, it’s not so bad.
Incidentally, if you wanted to generate sequences to produce those aformentioned wrong answers in postfix notation, here they are:
1 2 + 3 x 4 – 5 + = 10
1 2 3 x + 4 5 + – = -2