## Wednesday, September 21, 2011

### RPN Basics

RPN Basics
(updated 9/25/2011)

This is a basic tutorial of reverse polish notation (RPN).  RPN is an operating system that some calculators use, primarily those manufactured by Hewlett Packard.  RPN removes the need to enter parenthesis during long calculations and allows for immediate feedback during calculations; you will not need to enter a long operation before getting feedback - thus eliminating errors.  A lot of times, the number of keystrokes required to make a calculation is reduced using RPN compared to algebraic systems.

Typically, a RPN calculator uses a stack with four registers, named X, Y, Z, and T.  Each register is stacked on top of another.  A four-register stack diagram looks like this:

T
---
Z
---
Y
---
X [DISPLAY]

Most displays will only show the contents of the X register.

What is required of the user to execute a desired operation depends on the number of arguments (for our purpose, numbers) the function requires.  Most scientific calculator functions require one or two arguments.

One-argument functions operate on whatever is in the display, or the X register.  For one-argument functions, simply execute the desired operation.  One-argument functions include all the trigonometric functions (sine, cosine, tangent), logarithms, exponential (e^), reciprocal, square root, and factorial (x!).  The change sign operation fits under the category of one-number operations because it simply multiplies the number by -1.  The change sign operation is often labeled either CHS (HP 12C, HP 15C) or +/- (HP 35S).

Two-argument functions operate on the contents on the Y and X registers.  Common two-argument functions include the arithmetic operators (+, -, x, ÷), powers (y^x), combination and permutations, percent and percent change (Δ%).  To use a two-argument function, enter the first number (y), then press ENTER.  ENTER terminates the entry and gets the calculator ready to receive another number.  Next, enter the second number (x).  A second ENTER is not required because executing the operation terminates the second entry.  In summary, to operate a two-argument function:

1.  Enter the first (y) argument,
2.  Press ENTER to terminate the first entry.
3.  Enter the second (x) argument,
4.  Execute the desired function.

When you link more than one operation, it is known as a chain calculation.  A simple example is adding a list four numbers.   Another example is adding two groups of numbers and then multiplying the two sums together.

In chain calculations, whatever in the display becomes the first argument of the operation.  All that is needed is to enter the second argument (number), and then the required function.  For chain calculations:

1.  Enter the next required argument
2.  Execute the desired function, no ENTER is required

A more detailed explanation of the stack can be found in manuals of the HP 12C, 15C, and 35S calculators.     HP Website

The scope of this blog is just to give a very basic tutorial of RPN.  A lot of examples are provided to illustrate how to use the functions on an RPN calculator.

Calculators with RPN

Hewlett Packard:
Scientific: 15C (including Limited Edition), 35S, 48 Series, 32Sii, 41C, 50g+, and many others
Financial: 12C (all editions), 30b

iPod Apps:
GO-Sci 25, GO-Sci 21, just to name a couple.

You can look for RPN calculators online, many are available for the iPod, iPad, and Android operating mobile devices.

This tutorial is going to be a "do by example" tutorial.  Keystrokes are shown in blue.  All calculations on this blog are rounded to 4 decimal places.

* Note: This works for most models.  In these examples, you may need to press a shift key to access an operation depending on the calculator.  Since this tutorial covers a variety of calculators, the shift keys are omitted.  Please check your manual.

Examples:  Calculating with RPN

#1:  5 + 8

Keystrokes:
5 [ENTER]       Display: 5.0000

8 [ + ]                Display: 13.0000

Result: 13

#2: 10 - 6
As in any calculation involving subtraction, the order is important.

Keystrokes:
10 [ENTER]      Display: 10.0000

6 [ - ]                  Display: 4.0000

Result: 4

# 3:  6 x 2.95 + 2 x 1.28

Keystrokes:
6 [ENTER]       Display: 6.0000

2.95 [ x ]            Display: 17.7000

2 [ENTER]       Display:  2.0000

1.28 [ x ]            Display: 2.5600

[ + ]                    Display: 20.2600

Result: 20.26

# 4:  200 ÷ (3^2.5 - 1)

Keystrokes:
200 [ENTER]       Display:  200.0000

3 [ENTER]           Display: 3.0000

2.5 [y^x]               Display: 15.5885

1 [ - ]                     Display: 14.5885

[ ÷ ]                       Display: 13.7095

Result: 13.7095

# 5:  1/2 + 3/7 - √(25/64)

√ is the symbol for square root

Keystrokes (or one possible set of keystrokes):
2 [1/x]                   Display: 0.5000
3 [ENTER]           Display: 3.0000

7 [ ÷ ]                    Display: 0.4286

[ + ]                       Display: 0.9286
25 [ENTER]         Display: 25.0000

64 [ ÷ ]                  Display: 0.3906
[ √ ]                       Display: 0.6250

[ - ]                        Display: 0.3036

Result: 0.3036

# 6:  e^-3

Keystrokes:
3 [CHS] (or [+/-])      Display: -3
[e^x]                          Display: 0.0498

Result: 0.0498

# 7:  √(3^2 + 4^2)

Keystrokes:

If a square operation [x^2] is available:
3 [x^2]                    Display: 9.0000

4 [x^2]                    Display: 16.0000

[ + ]                         Display: 25.0000

[ √ ]                         Display: 5.0000

If a [x^2] is not available:
3 [ENTER] 2 [y^x]      Display: 9.0000

4 [ENTER] 2 [y^x]      Display: 16.0000

[ + ]                               Display: 25.0000

[ √ ]                               Display: 5.0000

Result: 5

# 8: Find the percent change between 19.99 (old) and 34.99 (new)

%CHG = Δ% =  [new - old] ÷ old x 100%

Keystrokes:

If a percent change function [Δ%] is available:
19.99 [ENTER]             Display: 19.9900

34.99 [Δ%]                    Display: 75.0375

If  [Δ%] is not available:
34.99 [ENTER] 19.99 [-]    Display: 15.0000

19.99 [÷]                              Display: 0.7504
100 [x]                                 Display: 75.0375

Result: The percent change is an increase of 75.0375%

Register Operations

Two common register operations are Swap and Roll Down.

Swap: This operation swaps the contents on the X and Y registers.  The key is typically labeled [x<>y].  The swap function is useful when arguments need to be switched before performing subtraction, division, and taking powers.

#9:  2 - (-5 x 3)

Keystrokes:
5 [CHS] (or [+/-])       Display: -5
[ENTER] 3 [ x ]         Display: -15.0000
Display:  2
[x<>y]                        Display: -15.0000
[ - ]                             Display: 17.0000

Result: 17

Roll Down:  This operation pushes down the contents of the register one level.

In a four stack scheme:
Whatever was in the T register goes to the Z register
Whatever was in the Z register goes to the Y register
Whatever was in the Y register goes to the X register
Whatever was in the X register goes to the T register

The key often labeled R with a down arrow next to it.  [R↓]

The Constant Pi (π)

The Pi key (or keystroke sequence) puts π on the X register (display) and lifts everything else one level.  On a four-register stack, whatever was held in the T register is lost.

#10: Find the area of a circle with a radius of 2.35 inches.

Keystrokes:
[π]                             Display: 3.1416

2.35 [x^2] [x]           Display: 17.3494

Result: 17.3494 square inches

Alternatively:  [ π ] 2.35 [ENTER] 2 [y^x] [ x ]

#11:  How many 5-card hands can be dealt out of a standard deck of 52 playing cards?

Combination = n! ÷ (k! x (n - k)!)

This function has several labels: Cy,x (HP 15C), COMB (HP 42S, HP 50g+), or nCr (most calculators)
The factorial function has several labels, typically x! or n!.

Keystrokes:

If a combination function is available:
52 [ENTER] 5 [nCr]

If a combination function is not available:
5 [x!]                                  Display: 120.0000

52 [ENTER]                      Display: 52.0000

5 [ - ]                                  Display: 47.0000

[x!]                                     Display: 2.5862     59   (2.5682 x 10^59)

[ x ] [1/x]                            Display: 3.2222    -62   (3.2222 x 10^-62)

52 [x!]                                Display: 8.0658      67  (8.0658 x 10^67)

[ x ]                                    Display: 2,598,960.000

Result: 2,598,960 possible 5-card hands

#12:  Find the sine of 30°

Keystrokes:
If necessary, set the calculator to degrees mode
30 [SIN]

Result: 0.5000

#13:  You have purchased a calculator for \$99.99 and present a coupon for 15% for the purchase price.  Assume sales tax is 8.75%.  What is the final amount due?

In RPN calculators, the percent function [ % ] returns Y * X%.  The contents of the Y stack remain unchanged.

Keystrokes:
99.99 [ENTER] 15 [ % ]      Display: 14.9985   (99.99 x 15%)
[ - ]                                         Display: 84.9915
8.75 [ % ]                              Display: 7.4368     (84.9915 x 8.75%)
[ + ]                                        Display: 92.4283

Result: 92.4283 (The final bill is \$92.43)

#14:  You deposit \$1,000 in a bank account earning 3.5% interest for 5 years.  How much money will you have after 5 years?

FV = PV x (1 + i%)^n

Where FV is the future value, PV is the present value, i is the periodic interest rate, and n is the number of periods.  We are looking for FV with PV = 1,000, i = 3.5, and n = 5.

Keystrokes:
1000 [ENTER] 1 [ENTER] 3.5 [ % ]       Display: 0.0350

[ + ]                                                             Display: 1.0350

5 [ y^x ]                                                      Display: 1.1877

[ x ]                                                             Display: 1,187.6863

Result: 1,187.6863  (\$1,187.68)

#15: On a right triangle, find the angle x in degrees:

/|
/  |
/    |
15  /      |
/        |
/x        |
--------
10

(my attempt at a right triangle, hopefully you get the picture)

x = arccos (10/15) = cos^-1 (10/15)

Keystrokes:

Set the calculator in Degrees mode if necessary.
10 [ENTER] 15 [ ÷ ]               Display: 0.6667
[COS^-1]                                Display: 48.1897

Result: The angle is 48.1897°

I hope you find this tutorial on RPN helpful.

Eddie

Many thanks to Xavier A. and Dieter on the MoHPC (The Museum of HP Calculators) Forum.

### Numworks: Allowing Repeated Calculations in Python

Numworks: Allowing Repeated Calculations in Python Introduction Say we want the user to repeat a calculation or a routine for as lo...