Post Reply 
(42) Feet inches fractions
07-11-2021, 08:31 PM (This post was last modified: 06-22-2022 06:43 PM by DMaier.)
Post: #1
(42) Feet inches fractions
This code takes decimal inches, e.g., 14.25 in X and returns the following in Alpha:
  • feet inches-fractions if flag 1 is clear, e.g., 1' 2-1/4"
  • inches-fractions if flag 1 is set, e.g., 14-1/4"
The intended application is formatting inches for display or printing after performing decimal calculations.

The maximum denominator, which should be a power of 2, is assumed to be set in variable DEN (store 16 in DEN if you don't want anything smaller than sixteenths, for example). Note that the code rounds to the nearest fraction.

The code uses flag 82 internally and doesn't preserve the stack (but see suggested modification in comments).

EDIT (2022-06-22): Updated to remove a serious bug in handling non-integer values that rounded to integers.

Sample output (DEN=8, flag 1 clear):

Code:

12.0000     1'
12.4900     1' 1/2"
 1.0000     1"
 1.2500     1-1/4"
14.0000     1' 2"
14.0626     1' 2-1/8"
14.3125     1' 2-3/8"
 4.0010     4"
 7.9990     8"

Code:

00 { 138-Byte Prgm }
01▸LBL "FIF" 
02 CLA  @ Free42 (including DM42): insert FUNC 00 above to preserve stack
03 CF 82
04 RCL× "DEN"
05 0.5
06 +
07 IP
08 RCL÷ "DEN"
09 ENTER
10 FS? 01
11 GTO 00
12 IP
13 X=0?
14 GTO 01
15 ENTER
16 12
17 ÷
18 IP
19 X=0?
20 GTO 01
21 AIP
22 ├"' "
23▸LBL 01
24 R↓
25 ENTER
26 ENTER
27 12
28 MOD
29▸LBL 00
30 IP
31 X=0?
32 GTO 02
33 SF 82
34 AIP
35▸LBL 02
36 R↓
37 FP
38 X=0?
39 GTO 05
40 FS? 82
41 ├"-"
42 SF 82
43 RCL× "DEN"
44 STO "N"  @ Use LSTO on Free 42 (including DM42)
45 RCL "DEN"
46 STO "D" @ Use LSTO on Free 42 (including DM42)
47▸LBL 03
48 RCL "N"
49 0
50 BIT?
51 GTO 04
52 2
53 STO÷ "D"
54 STO÷ "N"
55 GTO 03
56▸LBL 04
57 RCL "N"
58 AIP
59 ├"/"
60 RCL "D"
61 AIP
62▸LBL 05
63 FS? 82
64 ├"""
65 AVIEW
66 RTN
67 END
Find all posts by this user
Quote this message in a reply
06-22-2022, 07:47 PM
Post: #2
RE: (42) Feet inches fractions
For anyone aspiring to pursue the lucrative construction calculator market, here's a fancier version targeting Plus42 (remove the units conversion code in lines 25 to 32 for Free42).

This version uses two variables (both of which will be created and set to default values if not specified by the user):
  • DEN (default 16) specifies the maximum denominator. Should be a power of 2
  • RNDFAC (default 0.5) specifies the rounding factor. Possible values include:
    • 0 (round down to nearest multiple of 1/DEN)
    • 0.4999 (round to nearest multiple of 1/DEN, ties round down)
    • 0.5 (round to nearest multiple of 1/DEN, ties round up)
    • 0.9999 (round up to nearest multiple of 1/DEN)

In addition:
  • Negative numbers are now handled.
  • Zeros display as -. (They were previously blank.)
  • The stack and the previous state of flag 82 should be preserved.
Examples (DEN=16, RNDFAC=0.5, flag 1 clear):
Code:

 12.0000   1' 
 12.4900   1' 1/2"
  1.0000   1"
  1.2500   1-1/4"
 14.0000   1' 2"
 14.0626   1' 2-1/16"
 14.3125   1' 2-5/16"
  4.0010   4"
  7.9990   8"
-21.0625   -1' 9-1/16"
  0.0000   -
14.0000_cm 5-1/2"
 2.0000_yd 6'

Code:

00 { 228-Byte Prgm }
01▸LBL "FIF"
02 FUNC 00
03 CLA
04 RCLFLAG
05 LSTO "F"
06 R↓
07 CF 82
08 SF 25
09 RCL "DEN"
10 FS? 25
11 GTO 06
12 16
13 STO "DEN"
14▸LBL 06
15 R↓
16 SF 25
17 RCL "RNDFAC"
18 FS? 25
19 GTO 08
20 0.5
21 STO "RNDFAC"
22▸LBL 08
23 R↓
24 CF 25
25 UNIT?
26 SKIP
27 GTO 07
28 0_in
29 +
30 UVAL
31▸LBL 07
32 X≤0?
33 "-"
34 ABS
35 RCL× "DEN"
36 RCL+ "RNDFAC"
37 IP
38 RCL÷ "DEN"
39 ENTER
40 FS? 01
41 GTO 00
42 IP
43 X=0?
44 GTO 01
45 12
46 ÷
47 IP
48 X=0?
49 GTO 01
50 AIP
51 ├"' "
52▸LBL 01
53 R↓
54 ENTER
55 ENTER
56 12
57 MOD
58▸LBL 00
59 IP
60 X=0?
61 GTO 02
62 SF 82
63 AIP
64▸LBL 02
65 R↓
66 FP
67 RCL× "DEN"
68 X=0?
69 GTO 05
70 FS? 82
71 ├"-"
72 SF 82
73 LSTO "N"
74 RCL "DEN"
75 LSTO "D"
76▸LBL 03
77 RCL "N"
78 0
79 BIT?
80 GTO 04
81 2
82 STO÷ "D"
83 STO÷ "N"
84 GTO 03
85▸LBL 04
86 RCL "N"
87 AIP
88 ├"/"
89 RCL "D"
90 AIP
91▸LBL 05
92 FS? 82
93 ├"""
94 RCL "F"
95 STOFLAG
96 AVIEW
97 RTN
98 END
Find all posts by this user
Quote this message in a reply
Post Reply 




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