 The Museum of HP Calculators

## What is RPN?

In the 1920's, Jan Lukasiewicz developed a formal logic system which allowed mathematical expressions to be specified without parentheses by placing the operators before (prefix notation) or after (postfix notation) the operands. For example, the (infix notation) expression

```(4 + 5) × 6
```

could be expressed in prefix notation as

```× 6 + 4 5    or    × + 4 5 6
```

and could be expressed in postfix notation as

```4 5 + 6 ×    or    6 4 5 + ×
```

Prefix notation also came to be known as Polish Notation in honor of Lukasiewicz. HP adjusted the postfix notation for a calculator keyboard, added a stack to hold the operands and functions to reorder the stack. HP dubbed the result Reverse Polish Notation (RPN) also in honor of Lukasiewicz.

## Why Did/Does HP Use RPN?

In the years that followed, computer scientists realized that RPN or postfix notation was very efficient for computer math. As a postfix expression is scanned from left to right, operands are simply placed into a last-in, first-out (LIFO) stack and operators may be immediately applied to the operands at the bottom of the stack. By contrast, expressions with parentheses and precedence (infix notation) require that operators be delayed until some later point. Thus, the compilers on most modern computers converted statements to RPN for execution. (In fact, some computer manufacturers designed their computers around postfix notation.)

At the time that the HP-35 was introduced, other pocket calculators typically used a partial algebraic model. That meant they could evaluate trivial expressions like 4+5 but couldn't handle anything that involved parentheses or algebraic precedence. The technology of the time didn't allow for full algebraic compilers in pocket calculators.

RPN allowed HP to produce a pocket calculator that could evaluate arbitrary expressions using the available technology. For many, learning a new style of entry was a small price to pay to be able to evaluate arbitrary expressions on a calculator. Once the technology to produce algebraic compilers could fit into a pocket calculator, most RPN users had decided that RPN was more efficient and consistent for the user as well as for the calculator. Also, because subexpressions are evaluated as they are entered, entry errors are more obvious with RPN. On an algebraic calculator, omitting an opening parenthesis, may not lead to a calculation error until much later when an entire subexpression is evaluated.

Another advantage to RPN is consistency between machines. Early algebraic models had differing limits of the complexity of the expressions they could evaluate. For example, TI catalogs from the late 70's listed how many levels of parentheses and pending operations each model could handle. Even today if you begin to use an algebraic calculator, you need to determine just "how algebraic" it really is. For example, some switch to RPN for unary operators (ie 5 SIN rather than SIN(5) or even SIN 5 =), some are still missing parentheses and/or precedence and many can't directly enter an expression like:

```   4+5
—————
6+7
```

even though they claim to allow expressions to be entered as they are written.

## Learning RPN

If you've recently acquired your first RPN calculator and it didn't come with a manual, this section will get you started. The majority of HP calculators have the version of RPN that is described here.

Special notes for a few models:

HP-9100 and HP-9810
These calculators use a 3-level RPN that is a little different. The most important differences are that the result of two operand functions are left in the Y register and there is never an automatic stack lift. See the RPN versions page and the HP-9100 pages for more information on these models.
HP-28 and HP-48
These models have RPL which is more fully described here. The most important difference is that pressing ENTER only places the number in the lowest level of the stack rather than X and Y as on RPN calculators. You can also see the RPN versions page for more information on these models.

Do you remember how you originally learned to do math? Most of us were taught to write down the numbers we wanted to add and then add them like:

```    25
12
————
37
```

RPN works the same way. Take your new calculator and key in 25. Press the ENTER key to tell the calculator that you are finished keying this number. Now key in 12 and tell the calculator to add it to the previous number by pressing the + key. The result of 37 will immediately be displayed. Subtraction, multiplication and division all work the same way but with the −, ×, and ÷ keys substituted for the + key. Try it!

This also works for more than two numbers. To multiply the numbers 5, 6 and 7 together press 5 ENTER 6 × 7 × and read the result. Note that you didn't press ENTER after the 2nd and 3rd numbers because the operation key makes it clear that you are finished keying these numbers.

Many functions require only one number. On an RPN calculator, you still enter the number and then press the operation key and see the result. (Many calculators that claim to be algebraic use the same method since it takes less keystrokes than real algebraic syntax.) For example, to compute the sine of 10 press 1 0 SIN and read the result. To compute e5 press 5 ex.

Just remember that RPN calculators perform mathematical operations immediately when you press the operation keys so the number(s) must be entered first. There are no "pending operations" or precedence in RPN calculators. When multiple numbers must be entered in sequence, separate them with the ENTER key.

You now know how to use your calculator in the most basic way. The beauty of RPN is that this model extends to arbitrarily complex expressions without parentheses and precedence rules. To understand this, you'll want to know more about the stack.

RPN uses a stack of registers. Models described here have 4 registers labeled X, Y, Z, and T and show only the X register in the display. As you press the number keys, the number is entered into the X register. When you press ENTER, the number is both completed in the X register and copied to the Y register. The previous contents are moved "up" (Z to T, Y to Z and X to Y) to make room for the new number. See the example below:

 Initial Stack After 5 After ENTER Press: T 4 T 3 T 2 Z 3 Z 2 Z 1 Y 2 Y 1 Y 5 X 1 X 5 X 5 5 ENTER

When the 5 key was pressed, the stack lifted. When enter was pressed, the value in X was copied into Y. Don't worry about the two values pushed out of the T register (4 and then 3) - a four-level stack is sufficient even for very complex expressions. The stack was shown preloaded with the numbers 1-4 just to show you how the stack behaves.

Now to add 8 to the 5 already entered, see the example below:

 Stack from above After 8 After + Press: T 2 T 2 T 2 Z 1 Z 1 Z 2 Y 5 Y 5 Y 1 X 5 X 8 X 13 8 +

When the 8 was pressed, it overwrote the 5 in the X register and the + key caused the X and Y registers to be added with the result being placed in X. The rest of the stack dropped to fill the hole caused by two numbers being added to one sum.

Notice that the value of T was copied down to Z. T can be used as a handy constant register. (Last X described below can also be used as a constant register.) For example, if you wanted to compute the growth of \$100 deposited in a bank account earning 4%, you could press 1.04 ENTER ENTER ENTER which would fill the stack with 1.04. Then press 100 × to see the amount after the first year. Each additional press of × shows the balance after another year. This can continue indefinitely because the 1.04 in the T register is copied down each time × is pressed. The stack always contains the balance in X and 1.04 in the other three registers.

Another convenient stack trick is to use the × key to square a number. For example, to square 25, just press 25 ENTER ×. This works because after the ENTER both the X and Y registers contain 25.

In the calculations above, you used the X and Y registers of the stack without even thinking about it. Because the stack lifts and drops as needed, you can evaluate much more complicated with the same ease. For example, to evaluate:

```   4+5
—————
6+7
```

You would press 4 ENTER 5 + 6 ENTER 7 + ÷

Here's how it works:

 Initial Stack After 4 After ENTER Press: T 0 T 0 T 0 Z 0 Z 0 Z 0 Y 0 Y 0 Y 4 X 0 X 4 X 4 4 ENTER 5

 After 5 After + After 6 Press: T 0 T 0 T 0 Z 0 Z 0 Z 0 Y 4 Y 0 Y 9 X 5 X 9 X 6 + 6 ENTER

 After ENTER After 7 After + After ÷ Press: T 0 T 0 T 0 T 0 Z 9 Z 9 Z 0 Z 0 Y 6 Y 6 Y 9 Y 0 X 6 X 7 X 13 X 0.69 7 + ÷

Note that you evaluated the expression in the same order you would have by hand.

Pressing most calculator functions leave the stack left in a state where it will automatically lift. The ENTER key (and CLx which clears the X register) leave the stack in state where it won't automatically lift when the next number is entered. In this case, when the next number is entered it will replace the X register. This may sound complicated but it's really simple and intuitive. The ENTER key copies the X register to Y so there is no reason for the stack to automatically lift when you key the next number. You also wouldn't want the stack to lift after a CLx because that would just insert a zero into the stack. You rarely need to think about this - the calculator just does the right thing.

You can easily evaluate more complicated expressions than the one shown above. Just start with the innermost set of parentheses and work outwards as you would to solve the expression with a pencil and paper. For example to evaluate ([(4+5)(2+3)+6]/(8+7))9 press: 4 ENTER 5 + 2 ENTER 3 + × 6 + 8 ENTER 7 + ÷ 9 yx and read a result of 60716.99. If that's confusing, here it is broken down into steps:

```4 ENTER 5 +    Add 4 and 5 - one of the innermost terms
2 ENTER 3 +    Add 2 and 3 (another inner term)
×              4+5 is in Y and 2+3 is in X so multiply them
6 +            Add 6 to the result to complete (4+5)(2+3)+6
8 ENTER 7 +    Add 8 and 7 to compute the denominator
÷              Divide into the numerator already calculated
9 yx           Raise the previous result to the 9th power
```

This is the same order that you would have solved the expression by hand and the calculator will show the result of each subexpression which helps you catch errors. After a little more practice, RPN will become second nature and you may never want to use an algebraic calculator again.

## Stack Manipulation Keys

You can view the stack or change its order by pressing the R↓ to roll the stack contents. The example below shows an example of roll downs:

 Initial Stack After R↓ After 2nd R↓ Press: T 4 T 1 T 2 Z 3 Z 4 Z 1 Y 2 Y 3 Y 4 X 1 X 2 X 3 R↓ R↓

Pressing R↓ twice more would return the stack to its initial configuration. You can also exchange the contents of X and Y with the X⇔Y key and your calculator may also have a R↑ key. The CLx key will clear the contents of the X register. Some later models have an arrow key like ← which backspaces individual digits during entry or acts like a CLx at other times.

## Last X

Besides the registers above, most HP calculators have a LAST X register which preserves the number that was in X before the last numeric operation. Press the LAST X key to place a copy of the previous contents of X into the X register. You can use LAST X as a constant register or to recover from mistakes.