Post Reply 
Decimals to improper fractions program?
06-05-2018, 06:36 PM (This post was last modified: 06-06-2018 01:17 PM by Dieter.)
Post: #4
RE: Decimals to improper fractions program?
(06-05-2018 05:13 AM)Oddballs Wrote:  I am trying to program my HP35s and Free42 to take a decimal number and convert it to improper fractions...
...
But i havent found any program on this forum that does this... Does anyone have a program that i could look at?

On this site you will find various programs that convert a decimal number into fractions. For instance this program that is based on the one in the HP67/97 math pac.

Here is a slightly modified version for the HP41. Set the display mode to the desired accuracy and the result is a fraction that agrees with the original value to display precision. It should also run on the 42s. Here you can omit the PSE at the end because numerator and denominator will be returned in the two display lines as Y and X.

Code:
01 LBL"DF"
02 ENTER
03 STO 00
04 1
05 STO 02
06 STO 03
07 0
08 STO 01
09 STO 04
10 LBL 01
11 R↓
12 /
13 LASTX
14 ENTER
15 R↓
16 X<>Y
17 INT
18 STO 05
19 x
20 -
21 RCL 05
22 RCL 04
23 x
24 RCL 02
25 +
26 X<> 04
27 STO 02
28 R↓
29 RCL 05
30 RCL 03
31 x
32 RCL 01
33 +
34 X<> 03
35 STO 01
36 R↓
37 RCL 03
38 RCL 04
39 /
40 RND
41 RCL 00
42 RND
43 -
44 X≠0?
45 GTO 01
46 RCL 03
47 PSE
48 RCL 04
49 END

Example:
Find approximations for pi that agree in 2, 6 and 9 decimals.

FIX 2
[pi] XEQ"DF" => "22" 7

FIX 6
[pi] XEQ"DF" => "355" 113

FIX 9
[pi] XEQ"DF" => "104348" 33215

You can also set SCI display mode. In this case the calculated fraction agrees with the input to the specified number of significant digits.

On the 35s you could use an adapted version of this program. But why don't you simply take advantage of the built-in decimal-fraction conversion (FDISP key)? OK, this returns proper fractions, but you can easily convert these to improper ones. The 35s function can also be individually configured with flags and the /c key. Take a look at the manual for more information.

Edit and Update:
Here is a somewhat optimized version for the 42s / Free42. It returns the numerator and denominator as well as the approximation and its error. Also negative results are properly displayed.

Code:
00 { 90-Byte Prgm }
01>LBL "DF"
02 CF 00
03 X<0?
04 SF 00
05 STO 00
06 ABS
07 RCL ST X
08 1
09 STO 02
10 STO 03
11 0
12 STO 01
13 STO 04
14>LBL 01
15 Rv
16 ÷
17 LASTX
18 STO ST T
19 X<>Y
20 IP
21 STO 05
22 ×
23 -
24 RCL 05
25 RCL× 04
26 RCL+ 02
27 X<> 04
28 STO 02
29 Rv
30 RCL 05
31 RCL× 03
32 RCL+ 01
33 X<> 03
34 STO 01
35 Rv
36 RCL 03
37 RCL÷ 04
38 RND
39 RCL 00
40 ABS
41 RND
42 -
43 X!=0?
44 GTO 01
45 RCL 03
46 FS?C 00
47 +/-
48 "  "
49 AIP
50 |-"/"
51 RCL 04
52 AIP
53 ÷
54 ENTER
55 RCL- 00
56 X<>Y
57 AVIEW
58 END

Example: approximate ln 2 to six decimals.

Code:
[DISP] [FIX] 6
2 [LN] XEQ"DF"
                1143/1649
                0,693147
[<=]
                1,814777E-7
                0,693147

BTW, in ALL mode on Free42 I obtained this approximation of pi...

66 627 445 592 888 887  /  21 208 174 623 389 167

...which indeed agrees in 34 significant digits. ;-)

Dieter
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Decimals to improper fractions program? - Dieter - 06-05-2018 06:36 PM



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