Through the Looking Glass: Reverse Polish Notation: Less Popular, but Still Potent

The newer HP Palmtop User's Guides devote less space to "Reverse Polish Notation," but RPN can reduce the number of keystrokes for quicker calculations.

By Ed Keefe

Everyone knows how to use Reverse Polish Notation --right? I began to suspect otherwise when I noticed comments on CompuServe like: "I can make the letters `RPN' appear on the HP Calc screen, but then the calculator doesn't work right after that."

RPN is a variation on a parentheses-free mathematical logic known as "Polish Notation," developed by the Polish logician Jan Lukasiewicz (1878-1956). Over the years the most popular calculators in business and science have been HP calculators. These feature Reverse Polish Notation, especially the HP-12C business calculators and the HP-41 series. Texas Instruments was the other big player in calculators, and their machines used algebraic notation. You were either an RPN supporter, walking around with the motto ENTER > = (RPN is greater than Algebraic) emblazoned on T-shirts, or you were a TI algebraic "heretic."

Fortunately, the HP 95LX and 100LX tend to harmonize this rivalry by offering the choice of using either RPN or Algebraic notation in HP Calc. The users of the earlier HP calculators learned to use RPN on those machines and continue to use it on the 95LX and 100LX because they have found it to be more productive, using fewer keystrokes and, some would say, making fewer mistakes. Others tend to use Algebraic notation because they are familiar with it, don't understand Reverse Polish Notation, and don't want to take the time to learn it.

It appears that HP's perception of the importance of RPN has changed. I dug out my HP 95LX User's Guide and found that 17 pages were devoted to RPN, in Appendix D! Then I searched through the HP 100LX User's Guide and found only one page that paid tribute to RPN, the "first language" of Hewlett-Packard calculators!

Solver probably has something to do with the decline in popularity of RPN. The efficiency and accuracy of keystrokes is no longer an issue for programming complicated formulas. Solver lets you create in a few minutes an interactive, multiple variable equation that would have taken weeks to develop and debug on earlier calculators.

However, for intermediate calculations that are beyond simple arithmetic, frequently change, or produce a sequence of results (like accumulating interest), you can still take advantage of RPN's efficiency.

Using RPN on the Palmtop

To set the 95LX for RPN, go to HP Calc and press (MENU) Options Alg/ RPN. To set the 100LX for RPN, go to HP Calc and press (MENU) Options Calculator Modes RPN (F10).

Algebraic notation places the operators between relevant numbers or variables. With Algebraic set you would key in 1 + 2 = to get the answer 3. RPN specifies the operators after the numbers. With RPN set you would key in 1 (ENTER) 2 + to get the answer 3. The only mathematical operators that work differently in RPN are:

The two-number arithmetic operators (+) , (-) , (*) , (/) , and (^)

The percent function (%)

The LAST X function (LAST).

Here are some examples of RPN at work:

To calculate

Press Answer

53 + 137

53 (ENTER) 137 (+) 190.00

82-15

82 (ENTER) 15 (-) 67.00

12 x 5

12 (ENTER) 5 (*) 60.00

18 / 3

18 (ENTER) 3 (/) 6.00

 5

5 (ENTER) 2 (^) 25.00

 (14 + 3 + 2 11) / 2

14 (ENTER) 3 (+) 2 (+) 11 (-) 2 (/) 64.00

As you can see from the above examples, the numbers are separated by pressing (ENTER) or an arithmetic operator. The last example shows that you don't need to enter parentheses to group numbers. (Pressing (=) is the same as (ENTER) in the RPN mode.)

The "Stack" makes it work

When I got my first RPN calculator, an HP 25 Scientific, I wrestled with what I called "Perverse" Polish Notation for the first few weeks. The notion of a "stack" of numbers was new to me. Both HP Palmtop User's Guides refer to the stack as a four-level record of activities within the Calculator. Books on RPN compare the stack to a stack of dishes, in a spring-loaded dispenser. The operation of stack on a calculator with a single line display isn't obvious. However, the RPN stack can be made visible in either the Arithmetic and Math modes on the HP Palmtops. An example will help clarify these concept. These examples will work in all the applications of HP Calc. However, to see the stack and the registers, you need to switch to the Arithmetic or Math Applications.

First go to the Arithmetic function in HP Calc. If you haven't set your Palmtop to RPN, do so now (see the instructions above). You should see a screen similar to the one below:

Stacks and Registers Displayed in HP Calc: Graphic

 If the Stack and Registers are not visible, press (F2) (SHOW) on the 95LX. On the 100LX, press (MENU) Options Calculator Modes Show Stack, Show Registers. The registers and stacks shown above are empty. Clear yours on the 95LX by pressing: (MENU) Erase Registers; and (MENU) Erase Stack. On the 100LX press: (MENU) Clear Stack; and (MENU) Clear Registers.

The RPN stack positions are labeled Y, Z, and T. The X position of the stack is the Calc Line, lower right below the thick horizontal line.

EXAMPLE: Let's say you want to subtract two numbers, 20 15. Key in 20 and it appears on the Calc Line, the X position. Hit (ENTER) and key in 15. 20 is pushed to the Y position and 15 appears in the X position. Now press (-) to subtract. The calculator removes the two numbers off the stack, subtracts X (15) from Y (20) and puts the result (5) back on the stack in the X position.

HP's twist was to invert the stack: T at the top, Z next, Y next, and X at the bottom. One advantage of this method is that when you picture subtracting 15 from 20 on the stack, it's similar to the way you wrote down an arithmetic problem in grade school:

write 20

write 15

write -

 subtract and write 5

In grade school arithmetic, you never used an equals sign (=) to finish the problem. With an RPN calculator you don't need to press (=) to get the answer.

Once I understood the notion of the stack, RPN became easy. I forgot about entering algebraic formulas and just did the arithmetic like I did in grade school. I just keyed in two numbers and pressed the operator key (+, -, *, /, etc.), and beheld the answer. I didn't need an equal key to work a calculator, or parentheses either. In my mind, Enter was greater than Equal. The example below solves the same problem in RPN and Algebraic notation.

EXAMPLE: Add 3 and 5/8 inches to 10 and 7/8 inches and express the result as a decimal number.

In RPN Mode press:

5 (ENTER) 8 (/) 3 (+)

(3.63 is displayed)

 7 (ENTER) 8 (/) 1 0 (+)(+)

(14.50, final result displayed)

In Algebraic Mode press:

3 (+) 5 (/) 8 (+)

(3.63 is displayed)

(() 1 0 (+) 7 (/) 8 ())

(3.63+10.88 is displayed)

(=)

(14.50, final result displayed)

In the Algebraic mode on HP Calc you can press (ENTER) instead of ()) (=) and save one keystroke.

T is the Top of the Stack

The behavior of the T (Top) register in the RPN stack is worth noting. Whatever value is in the T register gets duplicated whenever the stack "drops." The stack drops as a result of a two number operation. For example if you press 1 (ENTER), 2 (ENTER), 3 (ENTER), 4 the stack looks like this:

Stack T = 1.00

Z = 2.00

Y = 3.00

___________________________________

(Calc line, X stack position) 4

Now if you press (+), X and Y are removed from the stack and added together. The result, 7, is placed in X (Calc line) stack position. The contents of Z drops to Y, and a copy of the contents of T drops to the Z position. The stack will look like this:

Stack T = 1.00

Z = 1.00

Y = 2.00

___________________________________

(Calc line, X stack position) 7

As mentioned, a copy of T drops to Z. This behavior can be used to insert a constant into a problem.

EXAMPLE: Suppose you deposit $2,000 in a savings account which pays a simple 5.25% annual interest. How much will you have at the end of the first year? second year? third year? sixth year? The answer, for each year, requires multiplying the previous year's result by 1.0525. To make this happen, enter the following keystrokes:

1.0525 (ENTER) (ENTER) (ENTER) 2000

Press (*) to get the answer for the first year: 2,105.00.

The interest rate, 1.0525, is copied from the T stack position to the Z position each time the stack drops. This insures that the Y position will always have 1.0525 in it.

Press (*) a second time and 2,105.00 is multiplied by 1.0525 and you get the two-year answer: 2,215.51. Press (*) a third time to get the 3-year answer: 2,331.83. Press (*) three more times in succession to get the 6-year answer: 2,718.71.

EXAMPLE: Here's how to do the same thing in Algebraic mode. First, press the following keystrokes:

1.0525 (ENTER) 2000

Then press (*) (LAST) (=) to get 2,105.00, the answer for the first year.

Solving the problem this way puts 1.0525 in the Stack 1 position, and lets you recall it with the LAST command, instead of having to re-enter it each time. You end up entering the same number of keystrokes as you did in RPN mode, to set things up and get the first year's answer. You must press three keys, (*) (LAST) (=), to get the answer to subsequent years.

In these short examples you can start to see how calculations can be done more easily once you're familiar with RPN and its stack. This is why those who are familiar with RPN don't want to go back to Algebraic notation.

A more complicated example where Solver does it better

Interactive RPN solutions used to take weeks to develop and debug. Now, with HP Calc's Solver anyone can do in a few minutes what formerly took weeks. The following example, finding the amount of payments in a lease, can be done more easily in Solver than in RPN. The formula is from the HP-41C User's Library Solutions: Lending, Saving and Leasing, page 39 (out of print).

PMT = (PV Resid*(1+i)^-n) /

((1 (1+i)^-(n-A))/i+A)

In this formula, PMT is the value of each payment; PV, Present Value, is the amount of the lease; Resid is the Residual value at the end of the lease; n is the total number of periods in the lease; i is the periodic interest rate which is the annual interest rate divided by the number of payment periods per year (usually 12, but could be 3, 4, 6, 24, 52, etc.); and A is the number of advance payments.

To create an interactive solution to the above problem, simply start Solver, activate the Solve Editor, (F2) on the 95LX, (F6) on the 100LX, and key in the following formula. (The lines wrapped below to fit format of The HP Palmtop Paper. You can enter the equation on one line.)

PMT=

(PV-Resid*(1+i)^(-n))/

((1-(1+i)^(-(n-A)))/i+A)

Quit the Solver Editor and press (F9), CALC, and behold, an interactive solution will appear in the blink of an eye. You can key in the values for any four of the variables and solve for the fifth one. This formula will allow you to use any number of payment periods per year, so you will have to calculate the i variable, before you enter it. For example if the you are using 13% annual interest you would divide .13 by 12 and enter the result in i.

Solving the same problem in RPN requires many more key strokes than using Solver. (See the sidebar on page 54.) However, it provides an excellent demonstration of how the stacks and registers are used. By going through this example step by step you will learn the basics of using RPN. Also, for those formulas where Solver is awkward to use you can record the RPN solution and save it as a System Macro that you can use again and again.

RPN makes Solver behave as expected

In the above problem, when you enter the value for "i," use the following keystrokes: in RPN, press 13 (ENTER) 1200 (/); in Algebraic, press 13 (/) 1200 (ENTER). Then enter the value into "i" by pressing (F5) or whatever F-key is associated with "i". When you have solved for "i" to get the annual rate in this example: in RPN press 1200 (*); in Algebraic mode press (*) 1200. Then press (ENTER). Notice the downside. In algebraic mode, the result is assigned to "i". This is probably not what you wanted to have happen. This doesn't happen when you use the RPN mode or if you use the (=) key in Algebraic mode.

While you're using Solver, you can see what's going on with the RPN stack. Just use (CTRL)-(A) and (CTRL)-(S) to switch between the Arithmetic and Solve windows. As a matter of fact, when RPN mode is active, the four stack registers and all the memory storage registers are available for your use in all of the calculator applications.

Summing Up

RPN remains a very efficient way to do arithmetic on HP calculators. However, with the HP Palmtop computers and the HP 19B, the question of which is better, "ENTER" or "EQUAL", (i.e. RPN or algebraic modes) is irrelevant. Both calculator modes are available.

If you already know how to use RPN, you'll still be able to use your skill. If you're new to RPN, you can choose to ignore it. HP Calc will still work quite well. Or you can spend a small amount of time learning to use RPN arithmetic. The rewards are worth the effort.

Advanced RPN example: Calculating Lease Payments