RPN Complex Number Calculator |
Written by Bryce Ringwood | ||||||||||||||||||||||||||||||||||
InstructionsThis 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 Entrylet'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. ImplementationThe 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:-
A pocket calculator is much more convenient than a computer in the workshop, however this will work in a mobile phone browser. OperationsTo 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 StackThe 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. 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 CalculationsThis 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:
Mixed Chain CalculationsExample:( ((2 + 8i)+(9 - 7i)) X 8 + ((3 + 2i) - (9+8i))/(12+6i)))X( 8 - 4i)
Note that results are displayed as you go. Polar ConversionsExample:
Clearing a numberThe 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. LimitationsYou 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. |