1 # unit convertor for DCT, by Shiar
3 # 1.11.0 200410291000 - use redraw()
4 # 1.10.5 200410151900 - data storage units (8 total, including LOC)
5 # 1.10.4 200410132300 - hp49 units for mass
6 # 1.10.3 200410130000 - fix error when run without menu module
7 # 1.10.2 200410122200 - adds submenus with all units
8 # 1.10.1 200410122030 - hp49 units for area, volume, time, speed, force, energy, power
9 # 1.09.2 200410112205 - all code moved here as well
10 # 1.09.1 200410022305 - moved %unit specs from 1.9 main
16 my $menugroup = addmenu(["main", 0], "unit") if defined &addmenu;
18 my %unit; # unit table (build below)
19 my $i = 0; # unit group counter (temporary)
22 my $title = shift @$_; # first element is group title, no unit
23 addmenu(["unit", $menugroup], $title, map "_$_->[0]", @$_) if defined &addmenu;
25 type=>$i, name=>$_->[0], val=>$_->[1], desc=>$_->[2], diff=>$_->[3]
31 ['cm', .01, "centimeter"],
32 ['mm', .001, "millimeter"],
33 ['yd', .0254*36, "yard"],
34 ['ft', .0254*12, "feet"],
35 ['in', .0254, "inch"],
36 ['Mpc', 3.085_677_581_3*10**22, "Mega parsec"],
37 ['pc', 3.085_677_581_3*10**16, "parsec"], # 180*60*60/pi au
38 ['lyr', 299_792_458*86_400*365.25, "light-year"], # c0*(seconds/year) - 9.46052840488e+15
39 ['au', 149_597_870.691*30, "astronomical unit"],
40 ['km', 1000, "kilometer"],
41 ['mi', .0254*12*5280, "international mile"],
42 ['nmi', 1852, "nautical mile"],
43 ['miUS', 1200/3937*5280, "US statute mile"],
44 ['chain', .0254*12*66, "Gunter's chain"],
45 ['rd', .0254*198, "rod/pole/perch"],
46 ['fath', .0254*72, "fathom"],
47 ['ftUS', 1200/3937, "survey foot"],
48 ['Mil', 2.54e-5, "Mil/thou"],
49 ['μ', 1e-6, "micron"], # μm
50 ['Å', 1e-10, "ångström"], # .1nm
51 ['fermi', 1e-15, "fermi"], # 1fm
52 # ['a0', .291_772_083*10**-11*19e-20, "atomic unit of length"],
53 # ['ell', .0254*45, "ell"],
54 # ['rope', .0254*12*20, "rope"],
55 # TI86 order: mm cm m in ft yd km mile mmile lt-yr mil Ang fermi rod fath
59 ['m²', 1, "square metre"],
60 ['cm²', .01**2, "square centimetre"],
62 ['yd²', (.0254*36)**2, "square yard"],
63 ['ft²', (.0254*12)**2, "square feet"],
64 ['in²', (.0254)**2, "square inch"],
65 ['km²', 1_000_000, "square kilometre"],
66 ['ha', 10_000, "hectare"],
68 ['mi²', (.0254*12*5280)**2, "square mile"],
69 ['miUS²', (1200/3937*5280)**2, "square statute mile"],
70 ['acre', (.0254*12*66)**2*10, "acre"],
71 # ['chain²', (.0254*12*66)**2, "square Gunter's chain"],
72 # ['ba', .0254**2*12, "board"],
76 ['m³', 1, "cubic metre"],
78 ['cm³', 1e-6, "cubic centimetre"],
79 ['yd³', (.0254*36)**3, "cubic yard"],
80 ['ft³', (.0254*12)**3, "cubic feet"],
81 ['in³', (.0254)**3, "cubic inch"],
83 ['galUK', .045_460_9, "Imperial gallon"],
84 ['galC', 0, "Canadian gallon"],
85 ['gal', 0, "US gallon"],
88 ['ml', 100e6, "mililitre"],
90 ['ozfl', 0, "US fluid ounce"],
91 ['ozUK', 0, "UK fluid ounce"],
92 ['tbsp', 0, "tablespoon"],
93 ['tsp', 0, "teaspoon"],
97 ['fbm', 0, "board foot"],
101 ['yr', 365.2425*86_400, "year (Gregorian)"],
102 ['d', 86_400, "day"],
103 ['h', 3_600, "hour"],
104 ['min', 60, "minute"],
106 # ['Hz', 1, "hertz"],
107 ['week', 604_800, "week"], # new
108 # ['au', 2.418_884_254e-17, "atomic unit of time"], # a[0]/(α*c)
112 ['m/s', 1, "meter per second"],
113 ['cm/s', 100, "centimeter per second"],
114 ['ft/s', 8.466_667e-5, "feet per second"],
115 ['kph', 2.777_778e-1, "kilometer per hour"],
116 ['mph', .447_04, "mile per hour"],
117 # ['mph', .447_041, "statute mile per hour"],
118 ['knot', .514_444, "nautical miles per hour"],
119 ['c', 2.997_924_58e8, "speed of light in vacuum"],
120 # ['ga', 0, "acceleration of gravity"],
124 ['kg', 1, "kilogram"],
126 ['Lb', .453_592_37, "avoirdupois pound"],
127 ['oz', 28.349_523_125, "ounce"],
128 ['slug', 14.593_903, "slug"],
129 ['lbt', .373_241_721_6, "Troy pound"],
130 ['ton', 907.184_74, "short ton"],
131 ['tonUK', 1016.046_908_8, "long ton"],
132 ['t', 1000, "tonne (metric ton)"],
133 ['ozt', .031_103_476_8, "Troy ounce"],
134 ['ct', 64.798_91e-6*19/6, "carat"], # 3+1/6 gr
135 # ['kt', 200e-6, "metric carat"], # new
136 ['grain', 64.798_91e-6, "grain"],
137 ['u', 1.660_538_73e-27 * 13e-35, "unified atomic mass"],
138 # ['mol', 0, "mole"], # mole = g/u
142 ['N', 1, "newton"], # kg*m/s²
143 ['dyn', 1e-5, "dyne"],
144 ['gf', 9_806.65, "gram-force"],
145 ['kip', 4.448_221_615_260_500_0, "kilopound-force"],
146 ['lbf', 4.448_221_615_260_5, "pound-force"],
147 ['pdl', .138_254_954_376, "poundal"], # lb*ft/s²
148 ['kgf', 9.806_65, "kilogram-force/grave"],
152 ['J', 1, "joule"], # N*m
153 ['erg', 1e-7, "erg"], # g*cm²/s²
154 ['Kcal', 4_186.8, "kilocalorie"],
155 ['Cal', 4.186_8, "calorie"],
156 ['Btu', 1_055.055_852_62, "International table btu"],
157 ['ft*lbf', 1.355_817_948_331_400_4, "foot-pound"],
158 ['therm', 105_505_585.262, "EEC therm"],
159 ['MeV', 1/6.241_509_629_152_65e12, "mega electron-volt"],
160 ['eV', 1/6.241_509_629_152_65e18, "electron-volt"],
165 ['hp', 735.498_75, "horse power"],
172 ['°C', 1, "degree Celsius", 273.15],
173 ['°F', 5/9, "degree Fahrenheit", 459.67],
175 ['°R', 5/9, "degree Rankine"],
177 # [], # electric current
184 "data storage", # memory
186 ['kB', 1024, "kilobyte"],
187 ['MB', 1024**2, "megabyte"],
188 ['GB', 1024**3, "gigabyte"],
189 ['TB', 1024**4, "terabyte"],
190 ['bit', 1/8, "bit/octet"],
191 ['Mbit', 1024**2/8, "megabit"],
192 ['LOC', 19e12, 'Library of Congress'], # est. 17-20TB
196 push @{$hook{precmd}}, sub {
198 $_ = $unit{substr $_, 1} or next;
199 if (exists $val{unit} and $val{unit}{type}==$_->{type}) {
200 unshift @stack, $val{i} if defined $val{i};
201 # $stack[0] -= $_->{diff} if $_->{diff};
202 # $stack[0] += $val{unit}{diff}*$val{unit}{val}/$_->{val} if $val{unit}{diff};
203 $stack[0] *= delete($val{unit})->{val} / $_->{val};
214 push @{$hook{postentry}}, sub {
215 exists $val{unit} && '_'.$val{unit}{name};
220 title => "unit convertor",