The Museum of HP Calculators

HP Forum Archive 21

[ Return to Index | Top of Index ]

Rectangular to Polar, Polar to Rectangular – One more time
Message #1 Posted by Jeff O. on 6 Nov 2013, 11:32 a.m.

I realize that Marcus' post regarding returning multiple values to the stack was probably more proof-of-concept than implementation of a useful rectangular to polar conversion function, but I was intrigued, so I pursued it. My first thought was that Marcus’ program did the conversion assuming the imaginary component in stack-1 and the real component in stack-2, while I prefer to assume real in stack-1 and Imaginary in stack-2. Changing Marcus’ program to do that would be easy to do if SWAP were a command, but as discussed in Marcus’ thread, it is not. Using commands that appear to be available, I came up with the following (I used reserved complex variable Z1 instead of temp):

Rectangular to Polar:

EXPORT RtoP;
KEY K_Mul()
BEGIN
  "i * + i / CONJ 'Z1' |> ARG Z1 ABS"
END;

Dividing by i and then taking the conjugate swaps the components in the complex number. This seemed to work, so then I turned my attention to Polar to Rectangular conversion, with the magnitude assumed in stack-1 and the angle in stack-2. I figured I’d need two real variables, so I used reserved X and Y. Here is how I did it:

Polar to Rectangular:

EXPORT PtoR;
KEY K_Div()
BEGIN
  " 'X' |> X - + 'Y' |> SIN X * Y COS X * "
END;

The “'X' |>” stores the magnitude in X, then to get the angle from stack-2, I recalled the magnitude, subtracted from itself to get zero, then added zero to the angle to drop it to stack-1. Then I could store it in Y and proceed with multiplying the magnitude by the SIN and COS of the angle. Kind of clunky, but it seems to work.

To recap the usage, for rectangular to polar, with the imaginary component in stack-2 and the real component in stack-1, press shift-help (User), then x (multiply). This puts the command string in the command line, then press ENTER and the angle will be returned to stack-2 with the magnitude in stack-1. So it takes four keystrokes to convert from rectangular to polar. This compares to 12 keystrokes for the conversion using my separate MA and AN programs. (Alpha Alpha M A Enter shift + Alpha Alpha A N Enter)

For Polar to rectangular, with the angle in stack-2 and the magnitude in stack-1, press shift-help (User), then / (divide). This puts the command string in the command line, then press ENTER and the imaginary will be returned to stack-2 with the real in stack-1. Again, four keystrokes compared to 12 for conversion using separate IG and RL programs. (Alpha Alpha I G Enter shift + Alpha Alpha R L Enter)

Again, the above is mostly proof-of-concept, done primarily to see if it could be done, like climbing Mt. Everest (with a much, much lower chance of dying in the process.)


[ Return to Index | Top of Index ]

Go back to the main exhibit hall