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

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

e.g.      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