UP | HOME

free42 Custom Unit Conversion Factors

Author: Mitch Richling
Updated: 2024-11-21 16:49:48

Copyright 2024 Mitch Richling. All rights reserved.

Table of Contents

1. Metadata

The home for this HTML file is: https://richmit.github.io/hp42/unit.html

Files related to this document may be found on github: https://github.com/richmit/hp42

Directory contents:

src - The org-mode file that generated this HTML document
src_42s - Ready to convert source listings for 42s code in this document
docs - This html document and associated PDF
bin - Importable RAW program files

2. Introduction

This application has a few unit conversions for temperatures; however, the focus is on a nice way to organize and recall various conversion factors I frequently use. That is to say, I don't really want to convert "in" to "cm", instead I want to push the conversion factor to the stack.

Aside from the TEMP menu, all of the menus have the same structure. The first menu key is the "base" unit for that page. Pressing any menu key will push a conversion factor for the given unit to the "base" unit to the stack. For example, if the left most menu is "mm" then pressing the "in" button will push 25.4 to the stack – the value you would multiply by to convert from inches to millimeters or divide by to convert from millimeters to inches.

Some constants are little programs. In some cases these are derived conversion factors, and the programs make that explicit. More importantly in cases when the base conversion factors are exact quantities, the programs insure we obtain machine accuracy – i.e. 1/2.54 is better than 0.3937.

3. Units

3.1. Conversion Factors

Type Conversion Description ……….
Leng:mm 1.0  
Leng:Ang 1e-7 Angstrom
Leng:μm 0.001 Micron
Leng:mil FUNC 01; 25.4; 1000; ÷ mil (1/1000 in)
Leng:cm 10  
Leng:in 25.4 Inch
Leng:in 1  
Leng:hand 4  
Leng:ft 12  
Leng:yd FUNC 01; 3; 12; ×  
Leng:fathom FUNC 01; 6; 12; ×  
Leng:mile FUNC 01; 5280; 12; ×  
Leng:km 1  
Leng:mi FUNC 01; 2.54; 12; ×; 5280; ×; 1e5; ÷ Int Mile
Leng:us mi 1.60934721869 US Mile
Leng:nmi 1.852 Int Nautical mi
Leng:m 0.001  
Leng:ft FUNC 01; 2.54; 12; ×; 1e5; ÷ Intl Foot
Area:hectare 1 hectare
Area:acre 0.40468564224 Int Acre
Area:m^2 0.0001  
Area:yd^2 8.36127360001e-5  
Vol:l 1  
Vol:qt 0.946352946 US Lq Quart
Vol:gal FUNC 01; 0.946352946; 4; × US Lq Gal
Vol:cc 0.001  
Vol:butt FUNC 01; 0.946352946; 4; ×; 126; × US Butt 126 US Gal
Vol:bbutt FUNC 01; 4.54609; 108; × UK Butt 108 UK Gal
Vol:tsp 1 Teaspoon
Vol:tbsp 3 Tablespoon
Vol:flOZ 6 fl oz
Vol:cup 48 Cup
Vol:pint 96 Pint
Vol:qt 192 Quart
Time:sec 1  
Time:min 60  
Time:hour 3600  
Time:day 86400  
Time:week 604800  
Time:year 31536000 365 day year
Time:day 1  
Time:year 365 Conentional
Time:acnt 360 Accounting
Time:greg 365.2425 Mean Gregorian
Time:julian 365.25  
Time:astro 365.256363004 Astronomical
Mass:kg 1  
Mass:lb 0.45359237 Pound Int
Mass:g 1e3  
Mass:ozWT 28e3 Ounce Food Weight
Mass:ozTr 31.1034768e3 Ounce Troy
Mass:stone FUNC 01; 0.45359237; 14; × Int Stone
Mass:lb 1  
Mass:US ton 2000 US (short) Ton
Mass:UK ton 2240 UK (long) Ton
Force:N 1  
Force:kgf 9.80665  
Force:lbf FUNC 01; 0.45359237; 9.80665; × Int lbf
Sped:m/s 1  
Sped:mph FUNC 01; 2.54; 12; ×; 5280; ×; 1e2; ÷; 3600; ÷ Int mph
Sped:kph FUNC 01; 1000; 3600; ÷  
Sped:knot FUNC 01; 1.852; 1000; ×; 3600; ÷ Intl nmi
Pres:Pa 1 N/m^2
Pres:bar 100000  
Pres:psi FUNC 01; 1e4; 0.45359237; ×; 9.80665; ×; 2.54; X^2; ÷  
Pres:atm 101325 Standard
Pres:mmHg 133.322  
Pres:inHg 3.386389e3 Conventional
Powr:watt 1  
Powr:BTU/h 0.2930710702 International
Powr:hp 735.49875 Metric
Powr:kcal/h 1.163  
Temp:C→F FUNC 11; 9; ×; 5; ÷; 32; +  
Temp:F→C FUNC 11; 32; -; 5; ×; 9; ÷  
Temp:K→C FUNC 11; 273.15; -  
Temp:C→K FUNC 11; 273.15; +  
▒▒▒▒    
SIPFX Run the SIPFX application  

3.2. Code

(MJR-generate-42-menu-code "UNIT" 
                           0
                           tbl
                           0
                           nil
                           'stay ;; Change to 'exit to exit immediatly after a conversion factor is pushed to the stack
                           'up   ;; Change to 'exit to have the application exit when [EXIT] is pressed
                           'yes
                           (lambda (atrg row) (if (string-equal "SIPFX" atrg) (format "\"%s\"" atrg)))
                           ;;#'MJR-local-only-gen-lab
                           (lambda (atrg target row) 
                             (cl-destructuring-bind (menu cfactor desc) row
                               (let ((print-desc 't))    ;; Set to NIL to not print description
                                 (let* ((dlen (length desc))
                                        (dsc1 (if (> dlen 0)
                                                  (substring desc 0 (min dlen 15))))
                                        (dsc2 (if (> dlen 14)
                                                  (substring desc 15 (min dlen 22)))))
                                   (mapconcat #'string-trim-left 
                                              (cl-remove nil (flatten-list
                                                              (list (split-string (message "%s" cfactor) ";")
                                                                    (and print-desc dsc1 (message "\"%s\"" dsc1))
                                                                    (and print-desc dsc2 (message "├\"%s\"" dsc2))
                                                                    (and print-desc (or dsc1 dsc2) "AVIEW"))))
                                              "\n"))))))

4. SI Prefixes

4.1. Generic

4.1.1. Menu

Prefix Sym Val
centi c 1e-2
milli m 1e-3
micro µ 1e-6
nano n 1e-9
pico p 1e-12
femto f 1e-15
kilo k 1e+3
mega M 1e+6
giga G 1e+9
tera T 1e+12
peta P 1e+15
exa E 1e+18
kibi Ki 1024
mebi Mi 1048576
gibi Gi 1073741824
tebi Ti 1099511627776
pebi Pi 1125899906842624
exbi Ei 1152921504606846976

Normally pressing the menu key will multiply the X value on the stack by the prefix multiplayer. If [SHIFT] is pressed befreo the menu key, then X will be divided by the prefix mulitplyer.

4.1.2. Menu Code

Note: This one will have an END generated to finish the UNIT application.

(MJR-generate-42-menu-code "SIPFX" 
                           0
                           tbl
                           0
                           nil
                           'stay ;; Change to 'exit to exit immediatly after a conversion factor is pushed to the stack
                           'up   ;; Change to 'exit to have the application exit when [EXIT] is pressed
                           'no
                           #'MJR-local-only-gen-lab
                           (lambda (atrg target row) 
                            (cl-destructuring-bind (prefix-str sym-string mult) row
                              (mapconcat #'string-trim-left 
                                         (list (format "%s" mult)
                                               (format "FS? 64")
                                               (format "÷")
                                               (format "FC? 64")
                                               (format "×")
                                               (format "\"%s (%s)\"" prefix-str sym-string)
                                               ;;(format "AVIEW")
                                               )
                                         "\n"))))

4.2. EE Prefixes

For EE problems a smaller set of prefixes is more efficient

4.2.1. Menu

Prefix Sym Val
micro µ 1e-6
nano n 1e-9
pico p 1e-12
▒▒▒▒    
kilo k 1e+3
mega M 1e+6

4.2.2. Menu Code

(MJR-generate-42-menu-code "EEPFX" 
                           30
                           tbl
                           0
                           nil
                           'stay ;; Change to 'exit to exit immediatly after a conversion factor is pushed to the stack
                           'up   ;; Change to 'exit to have the application exit when [EXIT] is pressed
                           'yes
                           #'MJR-local-only-gen-lab
                           (lambda (atrg target row) 
                            (cl-destructuring-bind (prefix-str sym-string mult) row
                              (mapconcat #'string-trim-left 
                                         (list (format "%s" mult)
                                               (format "FS? 64")
                                               (format "÷")
                                               (format "FC? 64")
                                               (format "×"))
                                         "\n"))))

5. EOF