Postfix notation

in #math5 years ago

Most people don't think about how to write sums. It is clear that that the plus sign should go between the two numbers you want to add. This makes a lot of sense but when you think about it there is no hard reason to put it between the numbers rather than after them. Then, 2+5 would become 2 5 + with the result remaining 7. This is known as postfix notation as compared to the normal infix notation. Of course it would work the same with subtraction, multiplication and division, e.g. 5 2 - = 5-2.

If we want to add more than two numbers things get a little more complicated. For example we can figure out the value of 2 5 3 + + step by step. Looking at the first + we realize it can only be meant to add up the 5 and the 3 so we get 2 8 + which is 10. So the strategy is always to look for two numbers followed by an operation and calculate that first. We could have also written 2 5 + 3 +, this time adding 2 and 5 first, resulting in 7 3 + which is also 10.

Some people insist that postfix is better than infix and we will now look at a reason why. For an expression like 2 + 5 * 3 it is common to do the multiplication first so it would become 2+15 and finally 17. If we wanted to do the addition first we would have to use parentheses: (2+5) * 3 = 7 * 3 = 21.
Now look at the postfix expression 2 5 3 * +. As before it will first multiply 5 by 3 and then add 2, yielding 17. If we want to add 2 and 5 first here, we do not use parentheses but rather rearrange the expression to 3 2 5 + *. Following the same logic, this will do just what we wanted, add 2 to 5 and then multiply by 3, the result being 21.
This is the true magic of postfix notation. You never need parentheses to clarify the order of operations and neither do you need arbitrary rules like multiplication before addition. On the other hand it is less intuitive and takes quite some time to get used to.

Just as well we might write the operator before the two numbers, resulting in a prefix expression. This is largely the same as postfix notation but it corresponds well with the idea of an operator basically being a function that takes two inputs x,y and gives only one result f(x,y) = x+y.

Finally, I want to give you two postfix expressions you can try to figure out yourself:
i) 2 5 3 4 + - +
ii) 6 2 5 + 4 - * 1 -

Sort:  


You just planted 0.06 tree(s)!


Thanks to @jifka

We have planted already
4666.06 trees
out of 1,000,000


Let's save and restore Abongphen Highland Forest
in Cameroonian village Kedjom-Keku!
Plant trees with @treeplanter and get paid for it!
My Steem Power = 32907.93
Thanks a lot!
@martin.mikes coordinator of @kedjom-keku
treeplantermessage_ok.png

Coin Marketplace

STEEM 0.29
TRX 0.13
JST 0.033
BTC 63133.02
ETH 3043.69
USDT 1.00
SBD 3.63