Reverse Polish Notation

859 Views Asked by At

For example :

a + b - c = abc-+ 

Why is this correct ?

xy - z+ = xyz--

as I understand, it has to be :

xy - z+ = xyz+-
3

There are 3 best solutions below

0
On

Because RPN performs the specified operation immediately with the most recent two numbers in the stack.

$$xy - z + = (x-y) + z$$

$$xyz-- = x-(y-z)$$

because in the second sequence, the $x$ gets pushed up higher into the stack once you enter the $y$ and the $z$. Then you execute the subtraction operation. Finally, the result of that subtraction is subtracted again from the first value entered ($x$) as the stack gets pushed down.

Algebraically, they give identical results because $x - (y-z) = x - y + z = (x-y) + z$.

Your proposed $xyz+-$ would give:

$$x - (y+z) = x - y - z$$

which is clearly not the same thing as the above.

0
On

$xy - z + = (x-y)+z$, (first $x$,$y$ on the stack, handle the minus, put the result on the stack, put $z$ on the stack, take their sum finally) while $xyz-- = x - (y - z) = x - y + z$ ($y$ and $z$ are highest on the stack when we meet the first minus) , so indeed the same.

0
On

xyz--=xy-zNN+, where "N" indicates the negative of a number. In other words, for any x, y, z, the difference of x and the difference of y and z, equals the sum of the difference of x and y and the negative of the negative of z. Since zNN=z, we thus obtain

xyz--=xy-z+. In infix notation this looks like this:

[x-(y-z)]=[(x-y)+(N(N(z))], which you might more ambiguously write:

[x-(y-z)]=[(x-y)+-(-(z))].