# Reverse Polish Notation

**URL:** <https://boards.straightdope.com/t/reverse-polish-notation/504991>\
**Category:** In My Humble Opinion\
**Created:** [August 1, 2009, 7:08pm UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991 "2009-08-01T19:08:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Johnny\_L.A](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/johnny_l.a/32/1084_2.png) [@Johnny\_L.A](https://boards.straightdope.com/u/Johnny_L.A)\
**Post date:** [August 1, 2009, 7:08pm UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991/1 "2009-08-01T19:08:17Z")

</div>

I’ve never used an HP calculator, so I’ve never used Reverse Polish Notation. In my job I write programs using a sequential language I’ve been using for a dozen years, so I’m quite used to the normal algebraic layout where the operator follows the operand and using parentheses where required (or not required, for clarity). I’ve heard that RPN is more ‘logical’ and less prone to error than the usual method.

Here’s an example.

1 + 2 x 3 = ?

By the order of operations, 2 and 3 would be multiplied and 1 added afterward. 2 x 3 = 6, plus 1 = 7. But what if the desired answer is 9? Add parentheses:

(1 + 2) x 3 = 9

I think most calculators know the order of operations, and so they would come up with 7 if you entered 1 + 2 x 3 [equals]. So if someone doesn’t know the OoO and assumes the equation is worked in written order they’ll get a ‘wrong’ answer. (i.e., they assume that 1 and 2 will be added first, and then multiplied by 3.)

AIUI — again, never having used it – to get 9 one would enter this in RPN:

3 [enter] 2 [enter] 1 + x

If you wanted the OoO answer of 7, one would enter:

1 [enter] 2 [enter] 3 x +

So to get the right answer per the OoO you enter the numbers sequentially (as in the first, possibly ambiguous, equation, and then enter the operators in reverse order. Is that correct? It seems much less intuitive to me, especially since I use parentheses to ensure that I’m doing things in the order I want to do them.

So RPN types: What is the appeal of RPN? Is it really useful, given that operational order can be specified simply by using parentheses? Is processing speed an issue? ISTM that the limiting factor is primarily how fast you can enter the numbers and symbols, rather than the speed of the processor. Is it a case of ‘It’s a geek thing. You wouldn’t understand.’? Can you give examples of why RPN is ‘better’ than the normal way?

---

<div class="post-metadata">

**Author:** ![Nava](https://avatars.discourse-cdn.com/v4/letter/n/da6949/32.png) [@Nava](https://boards.straightdope.com/u/Nava)\
**Post date:** [August 1, 2009, 7:17pm UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991/2 "2009-08-01T19:17:16Z")

</div>

> [@Johnny\_L.A](#):
>
> So RPN types: What is the appeal of RPN?

What my teachers told us is that it requires less keystrokes. Since calculators would die from mechanical failure rather than from anything else, less mechanical wear’n’tear was important. I don’t know about you, but I have killed a lot more keyboards than computers.

The cost of my HP was equivalent to 3 weekends’ pocket money for my richest classmates; to 25% of my yearly discretionary budget (transportation, entertainment, class materials). You bet we wanted it to last.

And the way my calculator works to get 7 is  
3 (enter) 2 \* 1 +

To get 9,  
2 (enter) 1 + 3 \*

Your version has more keystrokes.

---

<div class="post-metadata">

**Author:** ![Knorf](https://avatars.discourse-cdn.com/v4/letter/k/58956e/32.png) [@Knorf](https://boards.straightdope.com/u/Knorf)\
**Post date:** [August 1, 2009, 7:21pm UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991/3 "2009-08-01T19:21:11Z")

</div>

I find RPN much easier and faster to use, even for ordinary calculations (such as MPG, balancing a checkbook, computing students’ final grades.) I treasure my now ancient HP 11C, which has proven to be incredibly durable.

---

<div class="post-metadata">

**Author:** ![Hunter\_Hawk](https://avatars.discourse-cdn.com/v4/letter/h/50afbb/32.png) [@Hunter\_Hawk](https://boards.straightdope.com/u/Hunter_Hawk)\
**Post date:** [August 1, 2009, 7:27pm UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991/4 "2009-08-01T19:27:24Z")

</div>

On my 28S, I also have the option of entering ‘1+2x3’ [EVAL] and end up with 7, or ‘(1+2)x3’ [EVAL] and end up with 9.

But yeah, I’ll second **Knorf** –RPN is easier and faster, and I generally find that it makes structuring the sequences of calculations go more smoothly.

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [August 1, 2009, 8:13pm UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991/5 "2009-08-01T20:13:57Z")

</div>

From a programming perspective, implementing an RPN parser is a lot easier than making one for arbitrary expressions. You just push numbers onto a stack, and pop them off when you get an operator. There are whole programming languages organized around this idea, like Forth.

---

<div class="post-metadata">

**Author:** ![Dewey\_Finn](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/dewey_finn/32/4222_2.png) [@Dewey\_Finn](https://boards.straightdope.com/u/Dewey_Finn)\
**Post date:** [August 1, 2009, 8:45pm UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991/6 "2009-08-01T20:45:42Z")

</div>

Am I the only one who actually participated in competitions between RPN and algebraic notation calculators back in college? (Note that these were informal things we did in a dorm room. What can I say? It was a geeky school.)

---

<div class="post-metadata">

**Author:** ![groo](https://avatars.discourse-cdn.com/v4/letter/g/ecd19e/32.png) [@groo](https://boards.straightdope.com/u/groo)\
**Post date:** [August 1, 2009, 9:08pm UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991/7 "2009-08-01T21:08:49Z")

</div>

Programmer I am head visualize stack in and rules RPN \<enter\>

---

<div class="post-metadata">

**Author:** ![Johnny\_L.A](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/johnny_l.a/32/1084_2.png) [@Johnny\_L.A](https://boards.straightdope.com/u/Johnny_L.A)\
**Post date:** [August 2, 2009, 3:43am UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991/8 "2009-08-02T03:43:00Z")

</div>

I’d like to learn more about RPN.

---

<div class="post-metadata">

**Author:** ![Lightray](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lightray/32/391_2.png) [@Lightray](https://boards.straightdope.com/u/Lightray)\
**Post date:** [August 2, 2009, 4:40am UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991/9 "2009-08-02T04:40:55Z")

</div>

> [@Knorf](#):
>
> I treasure my now ancient HP 11C, which has proven to be incredibly durable.

Oh, yes. The batteries are held in by tape now, but it’s still functioning after 22 years. Younger engineers scoff, but if they touch my HP, there will be Hell To Pay.

---

<div class="post-metadata">

**Author:** ![hajario](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/hajario/32/171_2.png) [@hajario](https://boards.straightdope.com/u/hajario)\
**Post date:** [August 2, 2009, 5:06am UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991/10 "2009-08-02T05:06:58Z")

</div>

I am an engineer and I prefer RPN. It’s faster and more logical.

---

<div class="post-metadata">

**Author:** ![Johnny\_L.A](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/johnny_l.a/32/1084_2.png) [@Johnny\_L.A](https://boards.straightdope.com/u/Johnny_L.A)\
**Post date:** [August 2, 2009, 5:59pm UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991/11 "2009-08-02T17:59:21Z")

</div>

> [@Johnny\_L.A](#):
>
> I’d like to learn more about RPN.

As I implied in the OP, I have no reason to use RPN. But it just sounds geeky, and therefore interesting.

So if I want to learn RPN, what would be a good calculator to do it on? An HP 35s? I looked at the $12-cheaper HP 33S, but it’s funny-looking. How about an HP 50g? I already have a TI, so I don’t really need a graphing calculator.

---

<div class="post-metadata">

**Author:** ![chappachula](https://avatars.discourse-cdn.com/v4/letter/c/d2c977/32.png) [@chappachula](https://boards.straightdope.com/u/chappachula)\
**Post date:** [August 2, 2009, 6:20pm UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991/12 "2009-08-02T18:20:18Z")

</div>

hp’s were absolutely the best calculators in the world… in 1982.  
Back then, the most common calculators wereTexas Instruments, with tiny little buttons that barely moved when pressed. You couldn’t always tell by feel whether the key you just pressed had worked. The HP’s had big, bouncy keys with a clearly audible and “feelable” click, that felt good under your fingers.And when it cost $200 for a simple model, it was worth buying an HP.  
But the Reverse notation thing is just stupid, man…  
You have to think like Yoda:  
“3.14159…12.5…multiply them, I will”

---

<div class="post-metadata">

**Author:** ![Johnny\_L.A](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/johnny_l.a/32/1084_2.png) [@Johnny\_L.A](https://boards.straightdope.com/u/Johnny_L.A)\
**Post date:** [August 2, 2009, 6:23pm UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991/13 "2009-08-02T18:23:24Z")

</div>

> [@chappachula](#):
>
> But the Reverse notation thing is just stupid, man…  
> You have to think like Yoda:  
> “3.14159…12.5…multiply them, I will”

I can German speak.

---

<div class="post-metadata">

**Author:** ![chappachula](https://avatars.discourse-cdn.com/v4/letter/c/d2c977/32.png) [@chappachula](https://boards.straightdope.com/u/chappachula)\
**Post date:** [August 2, 2009, 6:28pm UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991/14 "2009-08-02T18:28:13Z")

</div>

> [@Johnny\_L.A](#):
>
> I can German speak.

yeah, but German is _designed_ to be written with the verb in the wrong place.  
Math formulas are written in a specific order, so why change it?  
Me, I like to press buttons in the same order that I write …

---

<div class="post-metadata">

**Author:** ![Thudlow\_Boink](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/thudlow_boink/32/320_2.png) [@Thudlow\_Boink](https://boards.straightdope.com/u/Thudlow_Boink)\
**Post date:** [August 2, 2009, 6:31pm UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991/15 "2009-08-02T18:31:22Z")

</div>

> [@Johnny\_L.A](#):
>
> I think most calculators know the order of operations, and so they would come up with 7 if you entered 1 + 2 x 3 [equals].

Scientific calculators do; basic four-function calculators don’t.

---

<div class="post-metadata">

**Author:** ![Nava](https://avatars.discourse-cdn.com/v4/letter/n/da6949/32.png) [@Nava](https://boards.straightdope.com/u/Nava)\
**Post date:** [August 2, 2009, 7:26pm UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991/16 "2009-08-02T19:26:17Z")

</div>

> [@Johnny\_L.A](#):
>
> So if I want to learn RPN, what would be a good calculator to do it on? An HP 35s? I looked at the $12-cheaper HP 33S, but it’s funny-looking. How about an HP 50g? I already have a TI, so I don’t really need a graphing calculator.

You don’t need a calculator to learn RPN. It’s just a different way to organize the operators which doesn’t require brackets or priorities.

The notations you thought were RPN are, in fact, RPN; you already know RPN. Your notations simply weren’t the most efficient. How many numbers you can place in stacks before you start adding operators depends only on how many stacks your machine or program has, but it’s more efficient if you follow a sequence of “numbers I want to operate first, operator, next numbers, operators, third group of numbers…” It’s also a simpler logic and it works for very long operations without needing a huge number of stacks.

The reason the calculators had more than two stacks (both my HP15C and my brother’s I-think-HP17 have four) is in order to be able to manage things like:

{(a+b)\*(c+d)}/(e+f)

Using your method (first all numbers, then all operators) wouldn’t work there.

---

<div class="post-metadata">

**Author:** ![Red\_Skeezix](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@Red\_Skeezix](https://boards.straightdope.com/u/Red_Skeezix)\
**Post date:** [August 2, 2009, 7:48pm UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991/17 "2009-08-02T19:48:26Z")

</div>

> [@Johnny\_L.A](#):
>
> So RPN types: What is the appeal of RPN? Is it really useful, given that operational order can be specified simply by using parentheses? Is processing speed an issue? ISTM that the limiting factor is primarily how fast you can enter the numbers and symbols, rather than the speed of the processor. Is it a case of ‘It’s a geek thing. You wouldn’t understand.’? Can you give examples of why RPN is ‘better’ than the normal way?

From the perspective of someone who has never used an HP calculator, only used this in programming.

1. Postfix notation removes a layer of computational complexity on the computer’s side. You don’t have to layer the parser by the order of operations if there is no such thing.

2. Less keystrokes, you never type parenthesis (they count as two keystrokes on QWERTY typewriter SHIFT+0). Less keystrokes translates to less errors.

3. No associative ambiguity. Think 1+2+3: Is it (1+2)+3 or 1+(2+3)? (This is important for examining intermediary results vs expected intermediary results.)

4. It’s more mechanical, there is two simple evaluation rules (post-tokenization,stack based):  
a. If($token is number) push $token.  
b. If($token is operator) push eval(pop,pop,$token).

5. A simple change to the second rule will allow you to see unambiguous intermediary results.  
If($token is operator)  
$result= eval(pop,pop,$token)  
display $result  
push $result

6. Generally forces users to eschew infix/algebraic shorthand notations. 6(5+3) which can lead to errors or unexpected consequences to new users.

---

<div class="post-metadata">

**Author:** ![Red\_Skeezix](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@Red\_Skeezix](https://boards.straightdope.com/u/Red_Skeezix)\
**Post date:** [August 2, 2009, 7:57pm UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991/18 "2009-08-02T19:57:27Z")

</div>

> [@Nava](#):
>
> The reason the calculators had more than two stacks (both my HP15C and my brother’s I-think-HP17 have four) is in order to be able to manage things like:
> 
> {(a+b)\*(c+d)}/(e+f)
> 
> Using your method (first all numbers, then all operators) wouldn’t work there.

Sorry to be contrarian. This only requires one stack…

a b + c d + \* e f + /

Your point however of all numbers then all operators still holds true though. To convert from infix to postfix requires you to list the evaluations in the order which you wish them to occur.

---

<div class="post-metadata">

**Author:** ![Johnny\_L.A](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/johnny_l.a/32/1084_2.png) [@Johnny\_L.A](https://boards.straightdope.com/u/Johnny_L.A)\
**Post date:** [August 2, 2009, 7:58pm UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991/19 "2009-08-02T19:58:47Z")

</div>

> [@Nava](#):
>
> You don’t need a calculator to learn RPN. It’s just a different way to organize the operators which doesn’t require brackets or priorities.
> 
> \<snip\>
> 
> {(a+b)\*(c+d)}/(e+f)

For me, it’s easier to learn things (especially things I don’t need to learn) if I can see the results, so I’d need a calculator.

How would your equation look in RPN?

EDIT: I see this has been answered.

---

<div class="post-metadata">

**Author:** ![The\_Hamster\_King](https://avatars.discourse-cdn.com/v4/letter/t/8edcca/32.png) [@The\_Hamster\_King](https://boards.straightdope.com/u/The_Hamster_King)\
**Post date:** [August 2, 2009, 8:08pm UTC](https://boards.straightdope.com/t/reverse-polish-notation/504991/20 "2009-08-02T20:08:47Z")

</div>

Normal in-fix notation required more than 50% more keystrokes in many cases. If you were doing lots of calculations, it added up.

Furthermore, I found that it was harder to mis-key in RPN. Part of the problem with parentheses was that the first one in a sequence didn’t change the display. So you really couldn’t be sure if the keystroke had taken or not. If you had two or three layers of parens the odds of making a mistake increased. With RPN if you miskeyed something the intermediate result usually would give you a clue that you’d done something wrong.

[Next page](https://boards.straightdope.com/t/reverse-polish-notation/504991.md?page=2)
