Post Reply 
(42S/DM42/Free42) Repeating decimals
01-23-2018, 01:41 AM
Post: #1
(42S/DM42/Free42) Repeating decimals
There was a recent thread on calculating repeating decimals. Joe Horn had a simple algorithm to do it, so I coded it for the DM42. It works on Free42 as well - not sure how well it would work on an actual 42S.

There are two functions, IDIV2 and RDEC. IDIV2 is a standalone function that replicates the function from the 50g (I think). Input is the numerator on Y and the denominator on X. The results are:

T: input numerator (a)
Z: input denominator (b)
Y: integer part of a/b
X: remainder of a/b

RDEC uses IDIV2 to implement Joe's algorithm and display the result of Y/X in the alpha register using the notation x.yyyy_rrrr... where 'rrr' is the repeating part of the decimal.

It's not always perfect - try 11632/864. Doesn't catch the first period of the repeating decimal.

I also use most of the R00 to R09. Sorry for my sloppiness there. I should clean these up to make them more logical (or use named variables) but I didn't.

R00: temp for calculating
R01: b
R02: a
R04: number of factors of 2 in b
R05: number of factors of 5 in b
R07: temp for calculating
R08: The remainder that signifies the end of the repeating part (96 in Joe's example)
R09: the max of R04 and R05 - used as a decrementing counter

Input is numerator (a) in Y and denominator (b) in X. Output is:

A: repeating decimal
Z: a
Y: b
X: a/b

Code:

00 { 186-Byte Prgm }
01▸LBL "IDIV2"
02 X<>Y
03 STO ST T
04 X<>Y
05 STO ST Z
06 ÷
07 IP
08 STO ST T
09 CLX
10 RCL ST Z
11 R↓
12 MOD
13 R↓
14 R↓
15 CLX
16 LASTX
17 R↓
18 R↓
19 END
20▸LBL "RDEC"
21 STO 01
22 STO 00
23 X<>Y
24 STO 02
25 X<>Y
26 0
27 STO 04
28 STO 05
29 R↓
30▸LBL 01
31 RCL 00
32 STO 07
33 2
34 ÷
35 STO 00
36 FP
37 X≠0?
38 GTO 05
39 1
40 STO+ 04
41 GTO 01
42▸LBL 05
43 RCL 07
44 5
45 ÷
46 STO 07
47 FP
48 X≠0?
49 GTO 10
50 1
51 STO+ 05
52 GTO 05
53▸LBL 10
54 RCL 04
55 RCL 05
56 X≤Y?
57 X<>Y
58 STO 09
59 RCL 02
60 RCL 01
61 CLA
62 XEQ "IDIV2"
63 X<>Y
64 AIP
65 ├"."
66 X<>Y
67 RCL 09
68 X=0?
69 GTO 40
70 R↓
71▸LBL 20
72 10
73 ×
74 RCL 01
75 XEQ "IDIV2"
76 X<>Y
77 AIP
78 X<>Y
79 DSE 09
80 GTO 20
81 GTO 45
82▸LBL 40
83 R↓
84▸LBL 45
85 ├"_"
86 STO 08
87▸LBL 30
88 10
89 ×
90 RCL 01
91 XEQ "IDIV2"
92 X<>Y
93 AIP
94 X<>Y
95 RCL 08
96 X=Y?
97 GTO 99
98 R↓
99 GTO 30
100▸LBL 99
101 ├"…"
102 RCL 02
103 RCL 01
104 RCL 02
105 RCL 01
106 ÷
107 AVIEW
108 END
Find all posts by this user
Quote this message in a reply
01-23-2018, 07:01 AM
Post: #2
RE: (42S/DM42/Free42) Repeating decimals
I tried on a real hp42s, it works like a charm. It is quite fast and usable.
Thank you.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 3 Guest(s)