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