# Reverse Polish Notation

For example :

a + b - c = abc-+


Why is this correct ?

xy - z+ = xyz--


as I understand, it has to be :

xy - z+ = xyz+-

• $xyz--=x(yz-)-=x-(y-z)=x-y+z=[(xy)-]+z=xy-z+$. – Nishant Oct 8 '14 at 4:11
• Why would you want to learn reverse polish notation. Did you lose a bet? – Pedro Tamaroff Oct 8 '14 at 4:23
• @PedroTamaroff no I'm studying for an exam and RPN is a part of the subjects – f855a864 Oct 8 '14 at 4:25

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.

$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.

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))].