The Museum of HP Calculators


Decimal To Rational for the HP-42S

This program is by Gerald Hillier and is used here by permission.

This program is supplied without representation or warranty of any kind. Gerald Hillier and The Museum of HP Calculators therefore assume no responsibility and shall have no liability, consequential or otherwise, of any kind arising from the use of this program material or any part thereof.

Overview

A programme for converting a decimal number to a rational.

Listing

  { 135-Byte Prgm }
01 ◆LBL ”DEC2F”
02  1
03  X≥Y? 
04  GTO 00
05  R↓
06  RCL ST Y
07  FP
08  X=0?
09  GTO 00
10  R↓
11  STO 00
12  R↓
13  STO 01
14  0
15  STO ST Z
16  SIGN
17  RCL+ ST X
18  STO 02
19  RCL÷ ST X
20  X⇔Y 
21 ◆LBL 01
22  ENTER
23  X=0?
24  GTO 02
25  R↓
26  RCL 00
27  RCL- ST Z
28  X≤0? 
29  GTO 02
30  R↓
31  1/X
32  ENTER
33  FP
34  R↓
35  IP
36  RCL* ST Y
37  RCL+ ST Z
38  RCL ST T
39  GTO 01
40 ◆LBL 02
41  R↓
42  R↓
43  STO ST Z
44  RCL 00
45  X≥Y? 
46  GTO 03
47  –
48  RCL÷ ST Y
49  ENTER
50  FP
51  SF 25
52  RCL÷ ST X
53  CF 25
54  +
55  IP
56  RCL* ST Y
57  +/-
58  RCL+ ST Z
59 ◆LBL 03
60  RCL ST Y
61  RCL* 01
62  FIX 00
63  RND
64  RCL÷ ST Z
65  RCL- 01
66  ABS
67  DSE 02
68  GTO 03
69  X≥Y? 
70  R↓
71  RCL 01
72  RCL* ST T
73  RND
74  ALL
75  R↑
76  RTN
77 ◆LBL 00
78  R↓
79  R↓
80  END

Example

       Stack Y:        7.143
       Stack X:       66

Returns

       Stack Y:        50
       Stack X:         7

i.e. 50/7 is the best approximation to 7.143 for denominators up to 66.

Go back to the software library
Go back to the main exhibit hall