Reputation: 11
Quick question about reverse polish notation.
Why is 2*3/(2-1)+5*(4-1)?: (original)
23*21-/541-*+
rather than 23*21-/5+41-*?
I am just confusing myself. Personally I'd have adding extra brackets to the original question to make it clear where the 5 is added. If its not there what order do I assume it goes in?
Thanks
Upvotes: 0
Views: 987
Reputation: 246
If we assume a conventional order of operations, then any multiplications get computed before any additions. So, when you have
y+x*z
, x*z
gets computed first, according to usual order of operations. More explicitly, y+x*z
means (y+(x*z))
.
Thus, 2*3/(2-1)+5*(4-1)
means (((2*3)/(2-1))+(5*(4-1)))
If you were to explicitly state up front that you stipulated your order of operations as additions happening before multiplication, then if you wrote 4+5*6
you would mean ((4+5)*6)
. If you did that, then you could state the distributive law as x*y+z=(x*y)+(x*z)
. What would expressions mean when you omit operations?
Consider xy&z
, where &
is binary, and the binary operation for xy
gets omitted. If the omitted binary operation is *
, and &
is +
, then this would mean that the expressed operation &
would happen before the suppressed multiplication operation.
Usually, omitted operations get assumed to happen first. So, if addition had binding priority over multiplication, then it probably would make sense for an expression like xy
to mean x+y
instead of the more usual x*y
.
In principle, there seems nothing wrong with letting additions happen before multiplications, so long as you state that you want to do that up front and stick to that convention and its implications in whatever you write.
That all said, except for communicating with people who don't understand RPN or PN, I simply don't see why you would write in infix notation once you understand RPN and PN.
Upvotes: 1
Reputation:
It's because multiplication has higher precedence than addition. When you don't have the braces, 5(only) is first multiplied with (4-1) and added to rest of the expression. When you haven't used braces, it is evaluated according to order of precedence only.
Upvotes: 1