Xth root of Y

Contributions to this software library are always welcome. Please ensure that you post program listings rather than .raw files. They give a reasonable idea of what your program does without having to load them into a DM42 and you can also include comments in your code. Check out the following link for a decoder/encoder: https://technical.swissmicros.com/decoders/dm42/

You can then copy/paste the listing and post it in "code" tags.
Post Reply
whuyse
Posts: 198
Joined: Thu Dec 21, 2017 1:23 pm

Xth root of Y

Post by whuyse »

In: Y: y; X: x
Out: x-th root of y

Apart from the FUNC 21 instruction, 42S-compatible.

Code: Select all

00 { 43-Byte Prgm }
01▸LBL "X√Y"
02 FUNC 21
03 X<>Y
04 REAL?
05 GTO 01
06 COMPLEX
07 X=0?
08 X≤Y?
09 GTO 03
10 COMPLEX
11 GTO 02
12▸LBL 01
13 X>0?
14 GTO 00
15▸LBL 02
16 +/-
17 XEQ 00
18 +/-
19 RTN
20▸LBL 03
21 COMPLEX
22▸LBL 00
23 X<>Y
24 1/X
25 Y^X
26 END
Cheers, Werner
41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE, DM15L
whuyse
Posts: 198
Joined: Thu Dec 21, 2017 1:23 pm

Re: Xth root of Y

Post by whuyse »

Shorter, but less suited for the real 42S as it uses the whole stack:

Code: Select all

00 { 35-Byte Prgm }
01▸LBL "X√Y"
02 FUNC 21
03 1/X
04 RCL ST Y
05 REAL?
06 0
07 CPX?
08 COMPLEX
09 X=0?
10 X≤Y?
11 GTO 00
12 R^
13 +/-
14 R^
15 Y^X
16 +/-
17 RTN
18▸LBL 00
19 R↓
20 R↓
21 Y^X
22 END
Cheers, Werner
41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE, DM15L
whuyse
Posts: 198
Joined: Thu Dec 21, 2017 1:23 pm

Re: Xth root of Y

Post by whuyse »

Silly me.. of course I need to include a check to see if X is odd as well:

Code: Select all

00 { 46-Byte Prgm }
01▸LBL "X√Y"
02 RCL+ 21
03 1/X
04 LASTX
05 2
06 MOD
07 1
08 X≠Y?
09 GTO 00
10 R↓
11 R↓
12 RCL ST Y
13 REAL?
14 0
15 CPX?
16 COMPLEX
17 X=0?
18 X≤Y?
19 GTO 00
20 R^
21 +/-
22 R^
23 Y^X
24 +/-
25 RTN
26▸LBL 00
27 R↓
28 R↓
29 Y^X
30 END
Cheeers, Werner
41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE, DM15L
Thomas Okken
Posts: 1100
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Xth root of Y

Post by Thomas Okken »

It looks like the FUNC 21 turned into RCL+ 21 somehow?
whuyse
Posts: 198
Joined: Thu Dec 21, 2017 1:23 pm

Re: Xth root of Y

Post by whuyse »

Oops.
I use that to get the byte count right in the DM42 decoder, which doesn't (yet) recognize FUNC etc, and forgot to change it back..
I also noticed I need to check a few more cases to be foolproof..
Werner
41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE, DM15L
Post Reply