I really like Dieter's first version (below) since it is compact and I don't mind the occasional "0/1" fraction portion of the answer. After having it on my calculator for a while I bought a "Thermal & Transport Science Pac" module. An unexpected (for me) feature of the module is that it has a fairly powerful unit management system that can be incorporated into user programs. After playing with it a while it dawned on me that with some simple additions to Dieter's program I could do full, fractional conversions to imperial unit systems.
See the "Thermal & Transport Science Pac" manual for details on how the conversion system works.
Conversion examples, entered in the alpha register before running D2F:
CM-IN (centimeters to inches with 1/8, 1/16, 1/32... precision by storing 8, 16, 32... in R00)
M-YD (meters to yards and feet, by storing 3 in R00)
KG-LBM (kilogram to pounds and ounces of mass by storing 16 in R00)
I made the following additions to Dieter's program:
At line 02, 5 lines are inserted:
FS? 01
EXQ 03 (If flag 01 is set a unit conversion is run on the input before being fractionalized)
FS? 55
VIEW X (if a printer is present it prints the converted, digital, input)
ASTO 01 (stores unit conversion string to put back into the alpha register at the program's end)
At line 27 insert:
CLA (to clear the alpha register of the conversion string)
At line 40 insert:
CLA (clears the alpha register of the fractionalized answer)
ARCL 01 (Puts the unit conversion string back into alpha so its ready for another input.)
GTO 04
LBL 03
VIEW X (prints the input)
AVIEW (prints the conversion type to be be run)
-SI (The unit conversion sub-routine on the Thermal module)
RTN
LBL 04
END
Usage:
If I want to run the unit conversion before fractionalizing the number I enter the unit conversion string into alpha and set flag 01.
Note that since register 1 is temporarily used to store the alpha string of the unit conversion, that string is limited to 6 characters. KG-LBM is fine, KGF-LBF (kilogram force to pound force) is seven characters so will not work. Most of the valid conversions strings are 6 characters or less. If a printer is attached, the program prints four items: the original input, the conversion string used, the conversion, and the fractionalized conversion.
(02-01-2016 07:10 AM)Dieter Wrote: [ -> ] (02-01-2016 02:06 AM)quantalume Wrote: [ -> ]OK, I shall then. We can compare code when I finish.
Fine. Here's my version:
Code:
01 LBL "D2F"
02 RCL 00
03 *
04 FIX 0
05 CF 29
06 RND
07 RCL 00
08 FS? 04
09 GTO 02
10 RCL Y
11 RCL Y
12 LBL 01
13 MOD
14 LASTX
15 X<>Y
16 X≠0?
17 GTO 01
18 +
19 ST/ Z
20 /
21 LBL 02
22 RCL Y
23 RCL Y
24 /
25 ENTER
26 INT
27 " "
28 ARCL X
29 RDN
30 RDN
31 MOD
32 LASTX
33 "├ "
34 ARCL Y
35 "├/"
36 ARCL X
37 R↑
38 FIX 4
39 SF 29
40 AVIEW
41 END
Usage:
Store the maximum denominator in R00. No other data registers are used.
Set flag 04 if you want a fixed denominator, otherwise the fraction with the minimum denominator is returned.
64 STO 00
1,3525 XEQ"D2F" => 1 23/64
The rounded result is returned in X, the other stack registers hold the three values of the result.
T: 1
Z: 23
Y: 64
X: 1,359375
Variations:
The program can be easily modified to accept the max. denominator on the stack (fraction ENTER denominator XEQ"D2F") and/or return improper fractions (87/64 in this case).
Dieter