RPN Complex Number Calculator
Written by Bryce Ringwood   

 

RE   
 IM     

 


Calculator 0.1.249

Instructions

This calculator performs simple "complex number" operations. (If you see what I mean). If you have never used a "Reverse Polish Notation" calculator before, please take a look at the examples and instructions, otherwise you will find the answers to be very mysterious. (I loaned my HP45 to my daughter because she had lost her school calculator. When she came home from school she tried to make me eat it, she was so frustrated.)

This complements the article on complex numbers.

There are two advantages that RPN calculators have compared to others, first, and most important, you can see the intermediate results of your efforts at every stage. They are also computationally efficient - if you view the source code, you will see it is a small program 

The intention is that the calculator should perform in a similar sort of way that an HP-35(S) / 45 / 65 reverse "Polish" notation calculator operates, however, it works on complex numbers defined as a + ib - rectangular cordinates. The P2R button converts to rectangular cordinates and the R2P button converts to polar.Complex numbers are entered as "a" then i followed by "b". Negative numbers are entered by keying the CHS key during data entry.

N.B. You can't enter numbers from the keyboard, only the keypad can be used.!

Simple Example - Data Entry

let's divide  10 + 2i by 3 -4i and take the square root:

key 10 i 2 ENTER 3 i 4  CHS / =0.88, 1.84i.

Now press SQR See 1.208223449002111 and 0.7614485555298907 is displayed

Now press X^2 to get back to the original number.

You may see there's a loss of precision in the last decimal place(This seems to depend on the browser you are using). If you have a genuine HP 45 to hand - you will see that entering 2, taking the square root and squaring the result also gives 1.99999999999, not 2.

Memory storage is implemented as STO then 1 to store in memory 1. RCL 1 recalls from memory 1. Memory operators MEM + etc will probably not be implemented.

Implementation

The idea was to knock up a simple calculator in an hour or two in Javascript to complement the maths article on complex numbers. 

There are some differences from a HP calculator:-

  1. Oddly, the HP 35s will not perform square root operations on negative or complex numbers, whereas this calculator does so. The HP 35s will work out a complex number raised to the power of another complex number - so there is a workaround. (Be careful of what raising  Z  to some fractional power might mean).
  2. This calculator does not operate in "polar mode". You can, however, convert.
  3. This calculator does not have an "Enter Exponent" key. This may be provided later.
  4. In this release, the display and the x-register are one and the same. The display is therefore used for storage, which is not good and means there is no rounding, making the calculator harder to use.
  5. The display lines are a single line on the top fro Re and a single line on the bottom for Im.

A pocket calculator is much more convenient than a computer in the workshop, however this will work in a mobile phone browser.

Operations

To add two numbers, press number1 ENTER number2 +.

Example To Add 2 + 4i to 5 -8.5i, Key the following:

2 i 4 ENTER 5 i 8.5 chs + Result  7 - 4.5 i

To divide 9.2 - 7i by 12.6 + 2i, Key the following

9.2 i 7 chs ENTER 12.6 i 2 / Result 0.6261980830670926  -0.6549520766773162i

Note how numbers are entered. If you are entering the real part, change the sign (CHS) to make it a negative number. If you are entering the imaginary part, the CHS key will convert the number to a negative imaginary number. Note that the CHS key will change the sign of the entire number if you are not in data entry mode. This is consistent with the HP 35S.

The Operational Stack

The calculator has four special temporary memories called "the stack". These are called the x,y,z and t registers. Think of them like four dinner plates piled on top of one another. When the calculator is switched on, they are all empty.The number stack When you key in your first number and press the ENTER key, a waiter removes the top plate, and puts your number on the bottom plate. He then copies the contents of the bottom plate on to a clean dinner plate and makes that the new bottom plate.

Suppose we do the same again, keying in a new number. Once again the waiter goes through the same motions of removal, putting the new number on to the bottom plate and producing a new plate so the number at the bottom can be copied on to it. Notice the top plate is always discarded.

Go through the process again, and you will see all the plates are full. Alas - there are only four plates, so if you enter a fourth number, the first number you keyed in will be lost.

Now, suppose that instead of entering four numbers, you entered only one (making the bottom two plates full) and then key in a number followed by a "two number operation" - say addition. Now the waiter fills the contents of the bottom plate with the number you keyed in. He then adds the contents of the plate above (the y register) and throws away the plate. Next, he takes a new plate, copies the contents of the current top plate to it, and makes the new plate the top plate.

What if you want to add a new number to the current number? - You don't have to ENTER again. Our cunning waiter sees you entered another +, and removes the top plate. He takes a new plate with your new number on the bottom and makes that the bottom plate. He then adds the contents of the 2nd plate to the bottom plate and removes the 2nd plate. He copies the top plate contents to a new plate and makes that the top plate.

No matter how you try to explain it, it sounds complicated and you might say "why do it?" - well it makes possible chained  and mixed chain calculations.

You can view the contents of the stack (of pplates) with the "RDN" ("Roll Stack Down")key, and exchange the x and y registers (bottom two plates) with the "XintY" key. 

Chained Calculations

This calculator will operate as a "normal" calculator. Just  don't key in the "i" part of the number.

Example(from the HP45 manual); If you sell 12 items at R1.58 and 8 items at R2.67 eac and 16 items at R0.54 each, then the sale price is :

(12 X 1.58) + (8 X 2.67) + (16 X 0.54)

Key in:

12 ENTER 1.58 X                           18.96
8 ENTER 2.67 X   21.36
 +     40.32
16 ENTER 0.54 X          8.64
+      (This is the total sale price) 48.96

 

 

 

 

Mixed Chain Calculations

Example:( ((2 + 8i)+(9 - 7i)) X 8 + ((3 + 2i) - (9+8i))/(12+6i)))X( 8 - 4i)

 

2 i 8 ENTER   2  i 8
 9 i 7 chs +    11 i 1
8 x   88 i 8
3 i 2 ENTER   3 i 2
 9 i 8  - -6 i - 6
 12 i 6 /   -.6 i -.2
+   87.4 i 7.8
8 i 4 chs X            730.4 i -287.2

 

 

 

 

 

 

Note that results are displayed as you go.

Polar Conversions

Example:

1 i 1 R2P 1.4142135623730951 i 0.7853981633974483
4 X  5.656854249492381 i 3.141592653589793   (i.e. pi)
P2R       -5.656854249492381 i 6.927419656575484e-16 (i.e. zero)

                                                                     

Clearing a number

The CLX key operates differently, depending on whether you are entering data, or have already entered or calculated your number. In data entry mode, the CLX key clears the the number you are entering so you can start again. If you are entering a real number, it clears the real part. If you are entering an imaginary number it clears the imaginary part.

If you are not entering data - it sets the x register (bottom plate) to zero - so you can start with a clean plate.

Limitations

You may use this calculator for educational and hobby purposes. I am not responsible for any loss or damage as a result of your using this or any other program on this site. The code is believed to work correctly, if it doesn't -please let me know.

 
Joomla template by a4joomla