|Re: HP-35s ->P and ->R conversions|
Message #14 Posted by Paul Dale on 8 Aug 2007, 7:44 p.m.,
in response to message #4 by Nenad (Croatia)
[EDIT: I posted an improved version immediately below this message, don't use this code if you want the flag independence - Pauli]
Rectangular Polar conversions aren't something I use but, nonetheless, I thought I'd have a go at this little programming exercise.
My program preserves the Z and T stack registers, it honours the current trigonometric mode and handles the degenerate cases. It doesn't do the right thing with LASTx but we can't have everything. At least not yet. It seems to produce the same values as my 15c but I've not tested it extensively.
First up the commented programmer friendly listing:
1 # Convert radius, theta -> x, y
2 LBL R # r, t, ?, ?
4 Rv # ?, ?, r, t
5 eqn [REGZ*SIN(REGT),REGZ*COS(REGT)] # [x,y], ?, ?, r
6 unpack: [1,0] # ?, ?, [x, y], [1,0]
8 * # ., ?, ?, x
9 EQN LASTx*[0,1]
12 # Convert x, y -> radius, theta
13 LBL P # x, y, ?, ?
15 GTO degen
17 Rv # ?, ?, x, y
18 eqn [ATAN(REGT/REGZ),SQRT(SQ(REGT)+SQ(REGZ))]
19 GTO unpack
21 # Degenerate cases
22 degen: Rv # y, ?, ?, 0
24 GTO d2 # 0, ?, ?, 0
29 ASIN # t = +/- PI/2, y, ?, ?
31 Rv # ?, ?, t, y
32 eqn [REGZ,ABS(REGT)] # , ?, ?, t
33 GTO unpack
34 d2: R^ # 0, 0, ?, ?
Of course, that isn't a lot of use when entering it into a calculator so here is a calculator friendly version after running though my little assembler:
R001 LBL R
R004 eqn [REGZ*SIN(REGT),REGZ*COS(REGT)]
R008 EQN LASTx*[0,1]
P001 LBL P
P003 GTO P008
P006 eqn [ATAN(REGT/REGZ),SQRT(SQ(REGT)+SQ(REGZ))]
P007 GTO R005
P010 GTO P020
P018 eqn [REGZ,ABS(REGT)]
P019 GTO R005
R^ is roll up
Rv is roll down
eqn marks the beginning of an equation and shouldn't be entered.
Checksums and sizes are:
R 74 BE75
P 123 8999
I hope somebody finds this useful.
Edited: 8 Aug 2007, 11:35 p.m. after one or more responses were posted