0

Most computer algorithms turn $a+b*(c+d)+e*f$ into $a\, b\, c\, d + * + e f * + $.

But this performs an addition before a multiplication.

If you follow order of operations, shouldn't the expression actually be:

$$a\; b\; c\; d\; +\; *\; e\; f\; *\; +\; +\;$$

Alex Pozo
  • 1,290
Ben Alan
  • 195
  • Why so? If we start from left to right we put number in the stack: $a,b,c,d$ until we get the first binary operand which is $+$ and thus we apply it to the top two numbers in the stack removing them and putting on top the result. The new stack will be$a,b,(c+d)$. Now we find $$ and we apply it to the two top elements to get: $a,b(c+d)$. – Mauro ALLEGRANZA Oct 20 '22 at 11:57
  • The next is $+$ and the result will be a stack with a single element: $a+[b*(c+d)]$. Up to now, no error. – Mauro ALLEGRANZA Oct 20 '22 at 11:57
  • Going on we have $e$ and $f$ and next the binary $$; thus we perform multiplication and we get the stack: $a+[b∗(c+d)],(ef)$. The last step will be their sum. – Mauro ALLEGRANZA Oct 20 '22 at 11:59
  • Computers build postfix expressions with a stack. But does the official definition outside of that algorithm require building it with a stack? You will notice both expressions yield the same answer. – Ben Alan Oct 20 '22 at 12:00

1 Answers1

2

In infix notation, the expression $x + y + z$ is technically ambiguous because it could refer to either $(x + y) + z$ or to $x + (y + z)$. Of course, we do this precisely because we like the ambiguity as these two expressions yield the same result and we don’t want to think about how to place the parentheses all the time.

When translating them to postfix notation, you need to make a choice, though. The first option leads to $x~ y + z~ +$ and the second to $x~y~ z + +$.

Note that this is precisely your example with $$\begin{align*} x &= a\\ y &= b \times (c + d) = b~c~d + \times \\ z &= e \times f = e~f~ \times \end{align*}$$

So the order of operations (multiplication before addition) really does not have much to do with it.

Eike Schulte
  • 3,232
  • So, as far as PEMDAS is concerned, (which we all know is bogus), a postfix expression will either have to perform a multiplication before an addition, or it will have to do a right->left addition operation? – Ben Alan Oct 20 '22 at 18:48
  • PEMDAS is not a general rule about maths but a convention on how to read infix expressions that mix multiplication and addition. While it is often taught as “do multiplications first” what it really means is that you should imagine implicit parentheses around the multiplications when multiplications and additions occur together. (I.e. $a +b \times c = a + (b\times c)$ and not $=(a+b) \times c$. It is not bogus: It is very useful to reduce the number of parentheses in infix expressions and it is important to know the convention because it is applied pretty much universally. – Eike Schulte Oct 20 '22 at 21:19
  • (The P in PEMDAS is a bit stupid as it basically says to keep parentheses where they already exist. Well, duh! I like the German “Punktrechnung vor Strichrechnung” better, which only mentions “operations written with dots” (i.e. multiplication written as $\cdot$ and division written as $:$) and “operations written with dashes” (i.e. addition and subtraction).) – Eike Schulte Oct 20 '22 at 21:23
  • In postfix notation, there is no ambiguity about the order of operations. So once you translated an expression to it, PEMDAS does not apply anymore. – Eike Schulte Oct 20 '22 at 21:25
  • So my question is, even though both postfix expressions are the same, is there one standard way to convert infix to postfix, or are both equally valid equivalents to the original infix? – Ben Alan Oct 21 '22 at 00:22
  • Both are equally valid. When a computer does it, it needs to make a choice, and computers will (usually) interpret $x+y+z$ as $(x+y)+z$, thus giving $x~y+z~+$ in postfix notation. That doesn’t make the other answer any less correct, though, unless your goal is to replicate the computer’s result. – Eike Schulte Oct 21 '22 at 05:42