RPN Quick Start

Most people are familiar with standard arithmetic notation which looks something like this:

1 + 3 + 6

In this example, 1 is added to 3 to give 4 which is then added to 6 which results in the answer 10. Things get a bit more complicated when different operations are included:

1 + 3 * 6

Without a standard order of operations, this expression could be interpreted in two different ways. You could evaluate "1 + 3" first to give 4 which is multiplied by 6 to result in 24. Or, you could evaluate "3 * 6" first to give 18 which is then added to 1 to result in 19. Depending on what you start with, you get different answers. For this reason, multiplication is always performed before addition (or subtraction) so that is the standard order of operations in mathematics. So, 19 is the right answer here.

To make things explicit, we can use brackets:

(1 + 3) * 6

In this case, 1 + 3 is evaluated first because it is in brackets so the correct answer is 24. This should all be familiar from elementary and high school mathematics classes. But, most people have not encountered reverse Polish notation.

Here is an example:

1 3 + 6 +

This is the equivalent version of "1 + 3 + 6" in RPN. In order to understand this notation, you must understand the concept of a stack of numbers. There are two basic operations with a stack. You can push a number onto the bottom of the stack or you can pop the most recently pushed number off of the stack. This may sound very limiting but with these two basic operations, you can do quite a bit of manipulation.

Now to read "1 3 + 6 +", you get:

  1. The number 1 is pushed onto the stack.
  2. The number 3 is pushed onto the stack so that 3 is now at the bottom of the stack and 1 is next to the bottom.
  3. The operation + is executed which pops two numbers off the stack, adds them together and pushes the result back onto the stack. In this case, 3 and then 1 are popped off the stack, they are added together to get 4 and then 4 is pushed onto the stack. At this point 4 is at the bottom of the stack.
  4. The number 6 is pushed onto the stack so that 6 is now at the bottom of the stack and 4 is next to the bottom.
  5. The operation + is executed which pops 6 and 4 off the stack, evaluates 4 + 6 to get 10 and pushes 10 into the stack.

Because 10 is on the stack at the end of evaluating the expression, 10 is the answer which is consistent with "1 + 3 + 6".

Now let's look at "1 + 3 * 6". In this case, this would be written as the following in RPN:

1 3 6 * +

To see how this works, we can read through it like this:

  1. The number 1 is pushed onto the stack.
  2. The number 3 is pushed onto the stack so that 3 is now at the bottom of the stack and 1 is next to the bottom.
  3. The number 6 is pushed onto the stack so that 6 is now at the bottom of the stack, 3 is next to the bottom and 1 is above that.
  4. The operation * is executed which pops 6 and 3 off the stack, evaluates 3 * 6 to get 18 and pushes 18 onto the stack. So, 18 is now at the bottom of the stack and 1 is next to the bottom.
  5. The operation + is executed which pops 18 and 1 off the stack, evaluates 1 + 18 to get 19 and pushes 19 onto the stack.

At the end of this, 19 is left on the stack which is consistent with the answer expected for "1 + 3 * 6". The other interesting example is "(1 + 3) * 6" from above. In RPN, that looks like:

1 3 + 6 *

Note that brackets are not required in RPN to get the order of operations you want. This is how it gets evaluated:

  1. The number 1 is pushed onto the stack.
  2. The number 3 is pushed onto the stack so that 3 is now at the bottom of the stack and 1 is next to the bottom.
  3. The operation + is executed which pops two numbers off the stack, adds them together and pushes the result back onto the stack. In this case, 3 and then 1 are popped off the stack, they are added together to get 4 and then 4 is pushed onto the stack. At this point 4 is at the bottom of the stack.
  4. The number 6 is pushed onto the stack so that 6 is now at the bottom of the stack and 4 is next to the bottom.
  5. The operation * is executed which pops 6 and 4 off the stack, evaluates 4 * 6 to get 24 and pushes 24 into the stack.

As before, this evaluates to the correct answer. In all of the examples so far, the only operations we have seen are addition and multiplication. But with subtraction and division, the order of the numbers is very important. The expression "3 - 1" is very different from "1 - 3". In RPN, the order is the same as you are used to. So, "3 - 1" in standard notation is "3 1 -" in RPN.

Also, all of the examples seen so far are "binary" operations meaning they take two numbers and return one number. For example, an addition takes two numbers, adds them and then produces a result. There are also unary operations which take a single number, performs some operation on that number and produces a result. An example of this are the trig functions like sin. So, the expression "sin(5)" in standard notation would look like "5 sin" in RPN. A unary operation like sin in RPN pops a single number off the stack, performs its operation (ie calculates the sin of 5) and the pushes the result onto the stack. This can then be used to evaluate something more complicated like "sin(5 - 3)" which would look like "5 3 - sin" in RPN.

At this point, you may be getting more comfortable reading something in RPN, but how do you take a standard notation expression and convert it into RPN. It is actually quite easy. In order to understand how to do this, let's convert "3 * 8 - (2 + 3)" into RPN.

First, read the expression left to right. The first thing we see is the number 3 so we should push 3 into the stack. At this point, the RPN version of this expression looks like "3". The next thing we see is the multiply operation. But in RPN, we need to skip past this operation for now and find the other number which is being multiplied. So, we look at the number 8 and we push that onto the stack. The RPN expression looks like "3 8".

The next thing is the subtract operation. Because of the order of operations, the multiply should be evaluated before the subtract, so we need to perform the multiply at this point. The RPN expression now looks like "3 8 *". We have seen the subtract operation but we need to find the other number we are using in the subtract. So, we continue before we do the subtract.

The next thing we see is the open bracket, which we ignore for now. After that is the 2 so we push that onto the stack resulting in an RPN expression which looks like "3 8 * 2". We see a addition operation next. The question we need to ask at this point is should the subtract or the addition be performed first. Because of the brackets, the addition should be performed first. So, we need to get the other number being added which follows the + sign and is a 3. We push 3 onto the stack resulting in an RPN expression which looks like "3 8 * 2 3 +".

The final thing we need to do is the last operation which is the subtract. The final RPN expression is:

3 8 * 2 3 + -

RPN may seem unnatural at first but most people will very quickly figure out how to work with it. The benefit of RPN over standard notation for a calculator is the stack. The stack represents a scratch pad which can grow or shrink as you need it. And you don't need to use brackets to try and get the expression you want. Once you are comfortable, you will be able to very quickly look at a formula in standard notation and enter it in RPN.

To put this in the context of Halcyon Calc, the last RPN expression above can be calculated in Halcyon Calc by pressing these buttons:

3 Enter 8 Enter * 2 Enter 3 Enter + -

The "Enter" key is used to push a number onto the stack. That takes 11 key-presses. In order to save key-presses, you don't need to press Enter when you execute an operation. Regardless of whether the operation is a binary or unary operation, the number which you have typed but not yet pushed is automatically push onto the stack before the operation is executed. So, you can simplify the above by pressing:

3 Enter 8 * 2 Enter 3 + -

This brings the count down to 9 key-presses, saving 2 Enters. But, either works of course.

The rest of the documentation for Halcyon Calc assumes familiarity with RPN so you may want to experiment with the calculator a bit. Try to calculate some specific equations. Start with some simple ones and progress to something more complicated, but still just using the standard add, subtract, multiply and divide operations which are clearly visible on the virtual keypad. Many more operations are available but how to access and use them will be described elsewhere. Once you feel comfortable with RPN, dive into the rest of the docs to explore the real power of Halcyon Calc.