Reverse Polish Notation

5 minute read

Reverse Polish Notation is a curious way of writing arithmetic operations. Instead of writing the operations between the numbers they apply to (called infix notation), you put them after the numbers. So 2 + 2 is written 2 2 +.

That probably sounds just plain silly if you haven’t heard of it before, like this does:

xkcd 645: a hot dog sitting next to a hot dog bun, captioned “Reverse Polish Sausage”.

However, unlike RPS, RPN has several useful properties that mean it deserves to be more than just a curiosity. First, it’s simpler to create a calculation circuit using RPN than using infix notation, which explains its popularity in the 1960s and 1970s, when pocket calculators were still cool, difficult to design, and rather expensive.

Second, more relevantly nowadays when you can buy a four-function calculator for $5 or do the math on your phone, it’s possible to write any sequence of operations unambiguously without using any parentheses. With infix notation, if you write, say, 5 + 3 * 6, it is unclear if you’re supposed to do the multiplication or the addition first; if you do the addition first you get 48, but if you do the multiplication first you get 23. We’ve thus had to come up with the complex notion of “order of operations” to avoid mixing them up. Order of operations is so important to any math more complicated than algebra that if you do a lot of math, you probably think it’s self-evident that you do the multiplication first – but even when the order is clear in your head, it remains easy to mix something up if you’re not paying attention. And when you add parentheses, you can end up with some pretty messy-looking expressions which are challenging to type accurately into a calculator (consider (2 / (5 - (6 + 3) * 8)) / 3, which in RPN would be typed 2 5 6 3 + 8 * - / 3 /). Studies have suggested that while RPN is initially less intuitive for most people, RPN users make fewer calculation mistakes and complete calculations faster (presumably because no keystrokes are wasted on parentheses).

Aside from raw efficiency, though – who really cares that much about saving two seconds typing into a calculator? – RPN has a strange elegance to it once you get used to it. In some way it seems purer and more reflective of the act of calculating. If you’re going to add 24 and 68, you don’t take the number 24, then think about the abstract concept of addition for a while, then get to considering the 68 – you start with the numbers, and then you add them. If you were doing the calculation on paper, you would surely write both the numbers down before you started adding. Putting the numbers into the calculator first just makes sense.

RPN calculations use the notion of a stack, which is a computer-science concept that will be familiar from everyday life. You have access to one end of the stack (in everyday life and in algorithms, we’d usually call that the top, but in RPN we call it the bottom because it makes the most sense to show it at the bottom of a column of numbers); you can throw more things onto the stack anytime you want, but you can only work with the last item you put on. When you put papers on your desk, stack dishes in your sink, or get interrupted while you’re working and then get interrupted by something even more important before you can gradually work your way back down to the original thing you were working on, you’re dealing with a stack.

Having your numbers on a stack instead of squashing operators between them grants you the useful ability to manipulate your numbers after typing them into the calculator. For instance, ever type in two numbers and then realize they should have been in the other order? (For example, instead of 2 / 4, you wanted to do 4 / 2.) With an infix-notation calculator, you’re out of luck and you just have to type it again; with an RPN calculator you can hit the “exchange” button to swap the bottom two numbers. Similarly, you can duplicate the bottom number so you can calculate one figure with it but hold onto the value so you can use it to calculate a second one in a moment. With the best calculators, you can also undo and redo your operations, meaning that if you make a mistake or you need to go back and look again at what you just did, you can restore the stack right to where it was a moment ago. In my experience, this vastly reduces the potential for mistakes and makes working with numbers much more pleasant.

This weekend I published version 1.0.0 of my esc calculator program (pronounced /esk/). esc is a friendly, extensible text-user-interface RPN calculator. If you like RPN, Python, or working in text terminals, or you just need to mess with numbers enough that you’d like to find better ways to do it, check out esc!

Besides just being a solid RPN calculator, esc allows you to easily add additional operations. For instance, if you find that you often need to calculate the circumference of a circle, you can write a Python function (which is easy as pi for something this simple, really) to multiply the bottom value on the stack by 2π and add it as a button on the calculator. The whole thing takes only a couple of minutes once you’ve done it a few times. Programmable hardware calculators have existed for many years, but they tend to be tricky and slow to program; esc brings the power of these calculators to your computer and to Python, the third-most-popular programming language and one of the easiest to learn.

A screenshot of esc in use, taken from the esc documentation.

esc is available under the GNU GPL license, and if you have Python installed on your computer you can download and try it right now at your favorite command line:

$ pip install esc-calc
$ esc