Re: HP35s >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, ?, ?
3 Rv
4 Rv # ?, ?, r, t
5 eqn [REGZ*SIN(REGT),REGZ*COS(REGT)] # [x,y], ?, ?, r
6 unpack: [1,0] # ?, ?, [x, y], [1,0]
7 x<>y
8 * # ., ?, ?, x
9 EQN LASTx*[0,1]
10 RTN
11
12 # Convert x, y > radius, theta
13 LBL P # x, y, ?, ?
14 x=0?
15 GTO degen
16 Rv
17 Rv # ?, ?, x, y
18 eqn [ATAN(REGT/REGZ),SQRT(SQ(REGT)+SQ(REGZ))]
19 GTO unpack
20
21 # Degenerate cases
22 degen: Rv # y, ?, ?, 0
23 x=0?
24 GTO d2 # 0, ?, ?, 0
25 x>0?
26 1
27 x<0?
28 1
29 ASIN # t = +/ PI/2, y, ?, ?
30 Rv
31 Rv # ?, ?, t, y
32 eqn [REGZ,ABS(REGT)] # [], ?, ?, t
33 GTO unpack
34 d2: R^ # 0, 0, ?, ?
35 RTN
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
R002 Rv
R003 Rv
R004 eqn [REGZ*SIN(REGT),REGZ*COS(REGT)]
R005* [1,0]
R006 x<>y
R007 *
R008 EQN LASTx*[0,1]
R009 RTN
P001 LBL P
P002 x=0?
P003 GTO P008
P004 Rv
P005 Rv
P006 eqn [ATAN(REGT/REGZ),SQRT(SQ(REGT)+SQ(REGZ))]
P007 GTO R005
P008* Rv
P009 x=0?
P010 GTO P020
P011 x>0?
P012 1
P013 x<0?
P014 1
P015 ASIN
P016 Rv
P017 Rv
P018 eqn [REGZ,ABS(REGT)]
P019 GTO R005
P020* R^
P021 RTN
where:
R^ is roll up
Rv is roll down
eqn marks the beginning of an equation and shouldn't be entered.
Checksums and sizes are:
LN= checksum
R 74 BE75
P 123 8999
I hope somebody finds this useful.
 Pauli
Edited: 8 Aug 2007, 11:35 p.m. after one or more responses were posted
