release 1.14pre0
[descalc.git] / 35_unitconv.pm
1 # unit convertor for DCT, by Shiar
2
3 # 1.14.0 200501261830 - units can have different offsets, so we can convert ^C/^F
4 # 1.13.1 200501071420 - added cd/dvd sizes to data storage
5 # 1.13.0 200411042100 - changed calls addmenu() and redraw()
6 # 1.11.0 200410291000 - use redraw()
7 # 1.10.5 200410151900 - data storage units (8 total, including LOC)
8 # 1.10.4 200410132300 - hp49 units for mass
9 # 1.10.3 200410130000 - fix error when run without menu module
10 # 1.10.2 200410122200 - adds submenus with all units
11 # 1.10.1 200410122030 - hp49 units for area, volume, time, speed, force, energy, power
12 # 1.09.2 200410112205 - all code moved here as well
13 # 1.09.1 200410022305 - moved %unit specs from 1.9 main
14
15 use strict;
16 use warnings;
17 use utf8;
18
19 addmenu("main", "unit");
20
21 my %unit;  # unit table (build below)
22 my $i = 0;  # unit group counter (temporary)
23 do {
24         $i++;  # next group
25         my $title = shift @$_;  # first element is group title, no unit
26         addmenu("unit", $title, map "_$_->[0]", @$_);
27         $unit{$_->[0]} = {
28                 type=>$i, name=>$_->[0], val=>$_->[1], desc=>$_->[2], diff=>$_->[3]
29         } for @$_;
30 } for (
31         [
32                 "length",
33                 ['m', 1, "metre"],
34                 ['cm', .01, "centimeter"],
35                 ['mm', .001, "millimeter"],
36                 ['yd', .0254*36, "yard"],
37                 ['ft', .0254*12, "feet"],
38                 ['in', .0254, "inch"],
39                 ['Mpc', 3.085_677_581_3*10**22, "Mega parsec"],
40                 ['pc', 3.085_677_581_3*10**16, "parsec"], # 180*60*60/pi au
41                 ['lyr', 299_792_458*86_400*365.25, "light-year"], # c0*(seconds/year) - 9.46052840488e+15
42                 ['au', 149_597_870.691*30, "astronomical unit"],
43                 ['km', 1000, "kilometer"],
44                 ['mi', .0254*12*5280, "international mile"],
45                 ['nmi', 1852, "nautical mile"],
46                 ['miUS', 1200/3937*5280, "US statute mile"],
47                 ['chain', .0254*12*66, "Gunter's chain"],
48                 ['rd', .0254*198, "rod/pole/perch"],
49                 ['fath', .0254*72, "fathom"],
50                 ['ftUS', 1200/3937, "survey foot"],
51                 ['Mil', 2.54e-5, "Mil/thou"],
52                 ['μ', 1e-6, "micron"], # μm
53                 ['Å', 1e-10, "ångström"], # .1nm
54                 ['fermi', 1e-15, "fermi"], # 1fm
55 #               ['a0', .291_772_083*10**-11*19e-20, "atomic unit of length"],
56 #               ['ell', .0254*45, "ell"],
57 #               ['rope', .0254*12*20, "rope"],
58 #               TI86 order: mm cm m in ft  yd km mile mmile lt-yr  mil Ang fermi rod fath
59         ], # lengths
60         [
61                 "area",
62                 ['m²', 1, "square metre"],
63                 ['cm²', .01**2, "square centimetre"],
64                 ['b', 1e-28, "barn"],
65                 ['yd²', (.0254*36)**2, "square yard"],
66                 ['ft²', (.0254*12)**2, "square feet"],
67                 ['in²', (.0254)**2, "square inch"],
68                 ['km²', 1_000_000, "square kilometre"],
69                 ['ha', 10_000, "hectare"],
70                 ['a', 100, "are"],
71                 ['mi²', (.0254*12*5280)**2, "square mile"],
72                 ['miUS²', (1200/3937*5280)**2, "square statute mile"],
73                 ['acre', (.0254*12*66)**2*10, "acre"],
74 #               ['chain²', (.0254*12*66)**2, "square Gunter's chain"],
75 #               ['ba', .0254**2*12, "board"],
76         ], # area
77         [
78                 "volume", #todo
79                 ['m³', 1, "cubic metre"],
80 #               ['st', 0, "stere"],
81                 ['cm³', 1e-6, "cubic centimetre"],
82                 ['yd³', (.0254*36)**3, "cubic yard"],
83                 ['ft³', (.0254*12)**3, "cubic feet"],
84                 ['in³', (.0254)**3, "cubic inch"],
85                 ['l', 100, "litre"],
86                 ['galUK', .045_460_9, "Imperial gallon"],
87                 ['galC', 0, "Canadian gallon"],
88                 ['gal', 0, "US gallon"],
89                 ['qt', 0, "quart"],
90                 ['pt', 0, "pint"],
91                 ['ml', 100e6, "mililitre"],
92                 ['cu', 0, "US cup"],
93                 ['ozfl', 0, "US fluid ounce"],
94                 ['ozUK', 0, "UK fluid ounce"],
95                 ['tbsp', 0, "tablespoon"],
96                 ['tsp', 0, "teaspoon"],
97                 ['bbl', 0, "barrel"],
98                 ['bu', 0, "bushel"],
99                 ['pk', 0, "peck"],
100                 ['fbm', 0, "board foot"],
101         ], # volume
102         [
103                 "time",
104                 ['yr', 365.2425*86_400, "year (Gregorian)"],
105                 ['d', 86_400, "day"],
106                 ['h', 3_600, "hour"],
107                 ['min', 60, "minute"],
108                 ['s', 1, "second"],
109 #               ['Hz', 1, "hertz"],
110                 ['week', 604_800, "week"], # new
111 #               ['au', 2.418_884_254e-17, "atomic unit of time"], # a[0]/(α*c)
112         ], # time
113         [
114                 "speed",
115                 ['m/s', 1, "meter per second"],
116                 ['cm/s', 100, "centimeter per second"],
117                 ['ft/s', 8.466_667e-5, "feet per second"],
118                 ['kph', 2.777_778e-1, "kilometer per hour"],
119                 ['mph', .447_04, "mile per hour"],
120 #               ['mph', .447_041, "statute mile per hour"],
121                 ['knot', .514_444, "nautical miles per hour"],
122                 ['c', 2.997_924_58e8, "speed of light in vacuum"],
123 #               ['ga', 0, "acceleration of gravity"],
124         ], # speed
125         [
126                 "mass",
127                 ['kg', 1, "kilogram"],
128                 ['g', 1e-3, "gram"],
129                 ['Lb', .453_592_37, "avoirdupois pound"],
130                 ['oz', 28.349_523_125, "ounce"],
131                 ['slug', 14.593_903, "slug"],
132                 ['lbt', .373_241_721_6, "Troy pound"],
133                 ['ton', 907.184_74, "short ton"],
134                 ['tonUK', 1016.046_908_8, "long ton"],
135                 ['t', 1000, "tonne (metric ton)"],
136                 ['ozt', .031_103_476_8, "Troy ounce"],
137                 ['ct', 64.798_91e-6*19/6, "carat"], # 3+1/6 gr
138 #               ['kt', 200e-6, "metric carat"], # new
139                 ['grain', 64.798_91e-6, "grain"],
140                 ['u', 1.660_538_73e-27 * 13e-35, "unified atomic mass"],
141 #               ['mol', 0, "mole"], # mole = g/u
142         ], # mass
143         [
144                 "force",
145                 ['N', 1, "newton"], # kg*m/s²
146                 ['dyn', 1e-5, "dyne"],
147                 ['gf', 9_806.65, "gram-force"],
148                 ['kip', 4.448_221_615_260_500_0, "kilopound-force"],
149                 ['lbf', 4.448_221_615_260_5, "pound-force"],
150                 ['pdl', .138_254_954_376, "poundal"], # lb*ft/s²
151                 ['kgf', 9.806_65, "kilogram-force/grave"],
152         ], # force
153         [
154                 "energy",
155                 ['J', 1, "joule"], # N*m
156                 ['erg', 1e-7, "erg"], # g*cm²/s²
157                 ['Kcal', 4_186.8, "kilocalorie"],
158                 ['Cal', 4.186_8, "calorie"],
159                 ['Btu', 1_055.055_852_62, "International table btu"],
160                 ['ft*lbf', 1.355_817_948_331_400_4, "foot-pound"],
161                 ['therm', 105_505_585.262, "EEC therm"],
162                 ['MeV', 1/6.241_509_629_152_65e12, "mega electron-volt"],
163                 ['eV', 1/6.241_509_629_152_65e18, "electron-volt"],
164         ], # energy
165         [
166                 "power",
167                 ['W', 1, "watt"],
168                 ['hp', 735.498_75, "horse power"],
169         ], # power
170 #       [
171 #               "pressure",
172 #       ], # pressure
173         [
174                 "temperature", #todo
175                 ['°C', 1, "degree Celsius", 273.15],
176                 ['°F', 5/9, "degree Fahrenheit", 459.67],
177                 ['K', 1, "Kelvin"],
178                 ['°R', 5/9, "degree Rankine"],
179         ], # temperature
180 #       [], # electric current
181 #       [], # angle
182 #       [], # light
183 #       [], # radiation
184 #       [], # viscosity
185
186         [
187                 "data storage", # memory
188                 ['B', 1, "byte"],
189                 ['kB', 1024, "kilobyte"],
190                 ['MB', 1024**2, "megabyte"],
191                 ['GB', 1024**3, "gigabyte"],
192                 ['TB', 1024**4, "terabyte"],
193                 ['bit', 1/8, "bit/octet"],
194                 ['Mbit', 1024**2/8, "megabit"],
195                 ['LOC', 19e12, 'Library of Congress'], # est. 17-20TB
196                 ['CD', 735e6, 'max CD-ROM data'],
197                 ['DVD', 47e8, 'max DVD-ROM data'],
198         ],
199 ); # units table
200
201 push @{$hook{precmd}}, sub {
202         if ($_ =~ /^_/) {{
203                 $_ = $unit{substr $_, 1} or next;
204                 if (exists $val{unit} and $val{unit}{type}==$_->{type}) {
205                         unshift @stack, $val{i} if defined $val{i};
206                         $stack[0] += $val{unit}{diff} if $val{unit}{diff};
207                         $stack[0] *= delete($val{unit})->{val} / $_->{val};
208                         $stack[0] -= $_->{diff} if $_->{diff};
209                         redraw(stack=>1);
210                         undef %val;
211                 } # convert
212                 else {
213                         $val{unit} = $_;
214                 } # set source unit
215                 return 1;
216         }} # conversion
217 }; # precmd
218
219 push @{$hook{postentry}}, sub {
220         exists $val{unit} && '_'.$val{unit}{name};
221 }; # showentry
222
223 return {
224         author  => "Shiar",
225         title   => "unit convertor",
226         version => "1.14",
227 };
228