author | Claus Gittinger <cg@exept.de> |
Tue, 24 Sep 2013 23:27:44 +0200 | |
changeset 3120 | 7f1a546af4f0 |
parent 2995 | ff546a7a872e |
child 3141 | d789144feb2f |
permissions | -rw-r--r-- |
884 | 1 |
" |
2 |
COPYRIGHT (c) 1996 eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
981 | 12 |
"{ Package: 'stx:libbasic2' }" |
13 |
||
363 | 14 |
Object subclass:#UnitConverter |
15 |
instanceVariableNames:'' |
|
516 | 16 |
classVariableNames:'Conversions Scaling Aliases Constants' |
363 | 17 |
poolDictionaries:'' |
18 |
category:'Magnitude-General' |
|
19 |
! |
|
20 |
||
21 |
!UnitConverter class methodsFor:'documentation'! |
|
22 |
||
884 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 1996 eXept Software AG |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
||
36 |
||
37 |
! |
|
38 |
||
363 | 39 |
documentation |
40 |
" |
|
41 |
In order to collect various unit conversions into one central |
|
42 |
manageable (and especially: browsable) place, all previously |
|
564 | 43 |
spread knowledge about metric (and other) conversions has been |
363 | 44 |
brought into this class. |
45 |
||
46 |
This class is purely functional, abstract and has no instances; |
|
47 |
all queries are via class protocol. |
|
48 |
Choosing the Magnitudes category as its home is arbitrary. |
|
49 |
||
515 | 50 |
The supported units are setup in the classes initialize method; |
364 | 51 |
supported are: |
886 | 52 |
meter - inch - feet - foot - yard |
53 |
meter2 - acre - 'german-ar' 'german-hektar' are |
|
54 |
liter - gallon - barrel - floz |
|
55 |
celsius - fahrenheit |
|
364 | 56 |
and many others. |
57 |
||
515 | 58 |
The converter does a recursive search for a conversion; |
59 |
thus, if there is a conversion from #inch to #millimeter and another |
|
60 |
from #millimeter to #kilometer, conversion from #inch to #kilometer |
|
61 |
is found automatically. |
|
62 |
||
886 | 63 |
No Warranty: |
64 |
The numbers and conversion factors were obtained from the Unix |
|
65 |
units command. Please check before using it - there might be |
|
66 |
typos or wrong conversions in the setup code. |
|
67 |
||
68 |
||
1323 | 69 |
Notice: |
70 |
This class is *much* older than the younger and more object oriented |
|
71 |
Measurements package. Other than solving similar problems, these two packages |
|
72 |
are not related. |
|
73 |
||
74 |
||
363 | 75 |
[author:] |
886 | 76 |
Claus Gittinger |
363 | 77 |
|
78 |
see also: |
|
886 | 79 |
examples |
80 |
/usr/share/lib/unittab (if present on your system) |
|
1323 | 81 |
Measurement Unit |
363 | 82 |
" |
83 |
! |
|
84 |
||
85 |
examples |
|
86 |
" |
|
87 |
ever wanted to know, how many floz's are there in a european |
|
88 |
Coce bottle ? |
|
982 | 89 |
[exBegin] |
90 |
Transcript showCR: |
|
91 |
(UnitConverter convert:1 from:#liter to:#floz) |
|
92 |
[exEnd] |
|
93 |
||
94 |
[exBegin] |
|
95 |
Transcript showCR: |
|
96 |
(UnitConverter convert:43.5 from:#oz to:#gram) |
|
97 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
98 |
|
363 | 99 |
or, how many square-meters an acre is: |
982 | 100 |
[exBegin] |
101 |
Transcript showCR: |
|
102 |
(UnitConverter convert:1 from:#acre to:#'meter^2') |
|
103 |
[exEnd] |
|
363 | 104 |
|
516 | 105 |
|
515 | 106 |
or europeans might want to know, what those |
107 |
fahrenheit numbers mean in an US weather report ;-): |
|
982 | 108 |
[exBegin] |
109 |
Transcript showCR:( |
|
110 |
(UnitConverter convert:80 from:#fahrenheit to:#celsius) |
|
111 |
asFixedPoint:1) |
|
112 |
[exEnd] |
|
515 | 113 |
|
516 | 114 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
115 |
how fast do I drive ? :-) |
982 | 116 |
[exBegin] |
117 |
Transcript showCR:( |
|
118 |
UnitConverter convert:200 from:#'km/hr' to:#'mile/hr') |
|
119 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
120 |
how fast does Chris drive ? :-) |
982 | 121 |
[exBegin] |
122 |
Transcript showCR:( |
|
123 |
UnitConverter convert:65 from:#'mile/hr' to:#'km/hr') |
|
124 |
[exEnd] |
|
516 | 125 |
|
126 |
||
127 |
calories or joule ? |
|
982 | 128 |
[exBegin] |
129 |
Transcript showCR:( |
|
130 |
UnitConverter convert:0.18 from:#'kilocalorie' to:#'kilojoule') |
|
131 |
[exEnd] |
|
132 |
[exBegin] |
|
133 |
Transcript showCR:( |
|
134 |
UnitConverter convert:2000 from:#'kilocalorie' to:#'kilojoule') |
|
135 |
[exEnd] |
|
516 | 136 |
|
137 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
138 |
distances: |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
139 |
|
982 | 140 |
[exBegin] |
141 |
Transcript showCR:( |
|
142 |
UnitConverter convert:1 from:#'lightsecond' to:#'kilometer') |
|
143 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
144 |
thats the same: |
982 | 145 |
[exBegin] |
146 |
Transcript showCR:( |
|
147 |
UnitConverter convert:1 from:#'lightspeed*s' to:#'kilometer') |
|
148 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
149 |
a days travel ... |
982 | 150 |
[exBegin] |
151 |
Transcript showCR:( |
|
152 |
UnitConverter convert:1 from:#'lightspeed*dy' to:#'kilometer') |
|
153 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
154 |
a year travel ... |
982 | 155 |
[exBegin] |
156 |
Transcript showCR:( |
|
157 |
UnitConverter convert:1 from:#'lightyear' to:#'kilometer') |
|
158 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
159 |
|
2320 | 160 |
wonna race on the autobahn ? |
161 |
[exBegin] |
|
162 |
Transcript showCR:( |
|
163 |
UnitConverter convert:200 from:#'km/h' to:#'mph') |
|
164 |
[exEnd] |
|
2547 | 165 |
[exBegin] |
166 |
Transcript showCR:( |
|
167 |
UnitConverter convert:13 from:#'m/s' to:#'km/h') |
|
168 |
[exEnd] |
|
2320 | 169 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
170 |
|
363 | 171 |
real estate buyers might want to know, how many acres |
172 |
a german ar is: |
|
982 | 173 |
[exBegin] |
174 |
Transcript showCR: |
|
175 |
(UnitConverter convert:1 from:#'german-ar' to:#acre) |
|
176 |
[exEnd] |
|
516 | 177 |
- or how many square-feet are there in your living room: |
982 | 178 |
[exBegin] |
179 |
Transcript showCR: |
|
180 |
(UnitConverter convert:100 from:#'meter^2' to:#'foot^2') |
|
181 |
[exEnd] |
|
1031 | 182 |
- or how about: |
183 |
[exBegin] |
|
184 |
Transcript showCR: |
|
185 |
(UnitConverter convert:3600 from:#'ft*ft' to:#'m*,') |
|
186 |
[exEnd] |
|
187 |
||
516 | 188 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
189 |
how many tea spoons are there in a cubic meter ? |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
190 |
(roughly, since a teaspoon is not a standard unit) |
982 | 191 |
[exBegin] |
192 |
Transcript showCR: |
|
193 |
(UnitConverter convert:1 from:#'meter^3' to:#teaspoon) |
|
194 |
[exEnd] |
|
365 | 195 |
how wide is a US page in inches: |
982 | 196 |
[exBegin] |
197 |
Transcript showCR: |
|
198 |
(UnitConverter convert:1 from:#'letterW' to:#inch) |
|
199 |
[exEnd] |
|
516 | 200 |
- in millimeter: |
982 | 201 |
[exBegin] |
202 |
Transcript showCR: |
|
203 |
(UnitConverter convert:1 from:#'letterW' to:#mm) |
|
204 |
[exEnd] |
|
516 | 205 |
|
206 |
||
365 | 207 |
the height of a US page in inches: |
982 | 208 |
[exBegin] |
209 |
Transcript showCR: |
|
210 |
(UnitConverter convert:1 from:#'letterH' to:#inch) |
|
211 |
[exEnd] |
|
516 | 212 |
- in millimeter: |
982 | 213 |
[exBegin] |
214 |
Transcript showCR: |
|
215 |
(UnitConverter convert:1 from:#'letterH' to:#mm) |
|
216 |
[exEnd] |
|
516 | 217 |
|
218 |
||
365 | 219 |
the same for european A4 standard page: |
982 | 220 |
[exBegin] |
221 |
Transcript showCR: |
|
222 |
(UnitConverter convert:1 from:#'a4H' to:#mm) |
|
223 |
[exEnd] |
|
224 |
[exBegin] |
|
225 |
Transcript showCR: |
|
226 |
(UnitConverter convert:1 from:#'a4H' to:#inch) |
|
227 |
[exEnd] |
|
2443 | 228 |
|
229 |
the mass of a proton: |
|
230 |
[exBegin] |
|
231 |
Transcript showCR: |
|
232 |
(UnitConverter convert:1 from:#'proton' to:#eV) |
|
233 |
[exEnd] |
|
234 |
the energy of a single proton in the LHC (as of 2010) |
|
235 |
[exBegin] |
|
236 |
Transcript showCR: |
|
237 |
(UnitConverter convert:7 from:#'TeV' to:#joule) |
|
238 |
[exEnd] |
|
363 | 239 |
" |
240 |
! ! |
|
241 |
||
242 |
!UnitConverter class methodsFor:'initialization'! |
|
243 |
||
550 | 244 |
initializeConversions |
363 | 245 |
"initialize common conversions" |
246 |
||
1135 | 247 |
self initializeScaleFactors. |
248 |
||
363 | 249 |
Conversions := IdentityDictionary new. |
250 |
Aliases := IdentityDictionary new. |
|
516 | 251 |
Constants := IdentityDictionary new. |
363 | 252 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
253 |
"/ ---------- velocity ------------- |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
254 |
|
556 | 255 |
Constants at:#lightspeed put:#(2.997925E8 #'m/s'). |
2443 | 256 |
Constants at:#soundspeed put:#(343 #'m/s'). "/ 20degC - AIR |
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
257 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
258 |
|
363 | 259 |
"/ -------------- length ------------- |
260 |
||
261 |
Aliases at:#km put:#kilometer. |
|
262 |
Aliases at:#m put:#meter. |
|
263 |
Aliases at:#dm put:#decimeter. |
|
264 |
Aliases at:#cm put:#centimeter. |
|
265 |
Aliases at:#mm put:#millimeter. |
|
266 |
Aliases at:#micron put:#micrometer. |
|
267 |
Aliases at:#nm put:#nanometer. |
|
268 |
Aliases at:#angstrom put:#decinanometer. |
|
269 |
||
270 |
"/ US |
|
271 |
self addConversion:12 from:#foot to:#inch. |
|
272 |
self addConversion:3 from:#yard to:#foot. |
|
273 |
self addConversion:5280 from:#mile to:#foot. |
|
515 | 274 |
self addConversion:0.001 from:#inch to:#mil. |
363 | 275 |
|
2320 | 276 |
Aliases at:#feet put:#foot. |
277 |
Aliases at:#ft put:#foot. |
|
278 |
Aliases at:#yd put:#yard. |
|
279 |
Aliases at:#mi put:#mile. |
|
280 |
Aliases at:#mph put:#'mile/hr'. |
|
363 | 281 |
|
282 |
"/ inch to millimeter |
|
283 |
self addConversion:(25.4/1000) from:#inch to:#meter. |
|
284 |
||
365 | 285 |
"/ nautic |
286 |
self addConversion:1852 from:#'nautical-mile' to:#meter. |
|
287 |
||
556 | 288 |
Aliases at:#lightsecond put:#'lightspeed*s'. |
289 |
Aliases at:#lightyear put:#'lightspeed*yr'. |
|
290 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
291 |
self addConversion:((Constants at:#lightspeed) at:1) from:#'lightspeed*s' to:#'meter'. |
385 | 292 |
|
516 | 293 |
"/ ---------- time ------------- |
294 |
||
295 |
self addConversion:60 from:#min to:#s. |
|
296 |
self addConversion:60 from:#hr to:#min. |
|
297 |
self addConversion:24 from:#dy to:#hr. |
|
298 |
self addConversion:365.24219879 from:#yr to:#dy. |
|
299 |
||
2320 | 300 |
Aliases at:#h put:#'hr'. |
301 |
Aliases at:#d put:#'dy'. |
|
516 | 302 |
|
385 | 303 |
"/ ---------- printing ------------- |
1135 | 304 |
self initializePrintValues. |
365 | 305 |
|
1135 | 306 |
"/ ---------------- area ------------------- |
307 |
||
308 |
"/ US |
|
309 |
self addConversion:4840 from:#acre to:#'yard^2'. |
|
310 |
||
311 |
self addConversion:100 from:#are to:#'meter^2'. |
|
312 |
self addConversion:100 from:#hectare to:#are. |
|
313 |
||
314 |
"/ german area - add your countries, and return to me ... |
|
315 |
Aliases at:#'german-ar' put:#are. |
|
316 |
Aliases at:#'german-hektar' put:#hectare. |
|
317 |
||
318 |
||
319 |
"/ ---------------- liquid ---------------- |
|
320 |
||
321 |
self addConversion:231 from:#gallon to:#'inch^3'. |
|
322 |
self addConversion:(1/4) from:#quart to:#gallon. "/ well - at least here, |
|
323 |
"/ thats also 1/4th of a good wine ;-) |
|
324 |
self addConversion:(1/2) from:#pint to:#quart. |
|
325 |
self addConversion:(1/16) from:#floz to:#pint. |
|
326 |
self addConversion:(1/8) from:#fldr to:#floz. |
|
327 |
||
328 |
self addConversion:42 from:#barrel to:#gallon. |
|
329 |
self addConversion:35.23907 from:#bushel to:#liter. |
|
330 |
||
331 |
Aliases at:#cc put:#'cm^3'. |
|
332 |
Aliases at:#liter put:#kilocc. |
|
333 |
Aliases at:#ml put:#milliliter. |
|
334 |
Aliases at:#gal put:#gallon. |
|
335 |
Aliases at:#qt put:#quart. |
|
336 |
||
2320 | 337 |
"/ well, a pint of beer is not always: |
338 |
self addConversion:277.420 from:#'british-gallon' to:#'inch^3'. |
|
339 |
self addConversion:(1/4) from:#'british-quart' to:#'british-gallon'. |
|
340 |
self addConversion:(1/2) from:#'british-pint' to:#'british-quart'. |
|
341 |
self addConversion:(1/16) from:#'british-floz' to:#'british-pint'. |
|
1135 | 342 |
|
343 |
"/ ---------------- mass ---------------- |
|
344 |
||
345 |
self addConversion:28.35 from:#ounce to:#gram. |
|
346 |
self addConversion:453.59237 from:#lb to:#gram. |
|
347 |
self addConversion:205 from:#carat to:#milligram. |
|
348 |
||
2444
6c4c6addc97a
changed: #initializeConversions
Claus Gittinger <cg@exept.de>
parents:
2443
diff
changeset
|
349 |
self addConversion:0.938272013 from:#proton to:#GeV. |
6c4c6addc97a
changed: #initializeConversions
Claus Gittinger <cg@exept.de>
parents:
2443
diff
changeset
|
350 |
self addConversion:0.510998910 from:#electron to:#MeV. |
6c4c6addc97a
changed: #initializeConversions
Claus Gittinger <cg@exept.de>
parents:
2443
diff
changeset
|
351 |
self addConversion:0.939565346 from:#neutron to:#GeV. |
2443 | 352 |
|
1135 | 353 |
Aliases at:#oz put:#ounce. |
354 |
Aliases at:#lbs put:#lb. |
|
355 |
||
356 |
Aliases at:#gm put:#gram. |
|
357 |
Aliases at:#kg put:#kilogram. |
|
358 |
||
359 |
"/ ---------------- energy ---------------- |
|
360 |
||
361 |
Aliases at:#cal put:#calorie. |
|
362 |
Aliases at:#nt put:#newton. |
|
363 |
Aliases at:#joule put:#'nt*m'. |
|
2320 | 364 |
Aliases at:#J put:#'joule'. |
365 |
Aliases at:#N put:#'newton'. |
|
2443 | 366 |
Aliases at:#eV put:#'electronvolt'. |
367 |
Aliases at:#MeV put:#'megaeV'. |
|
368 |
Aliases at:#GeV put:#'gigaeV'. |
|
369 |
Aliases at:#TeV put:#'teraeV'. |
|
2320 | 370 |
|
371 |
Aliases at:#watt put:#'J/s'. |
|
1135 | 372 |
|
373 |
self addConversion:4.1868 from:#calorie to:#joule. |
|
2443 | 374 |
self addConversion:1.60217653E-19 from:#electronvolt to:#joule. |
1135 | 375 |
|
376 |
||
377 |
"/ ---------------- cooking ---------------- |
|
378 |
||
379 |
self addConversion:4 from:#tablespoon to:#fldr. |
|
380 |
self addConversion:(4/3) from:#teaspoon to:#fldr. |
|
381 |
||
382 |
||
383 |
"/ ---------------- temperature ---------------- |
|
384 |
||
385 |
self addConversion:[:d | d * 1.8 + 32] from:#celsius to:#fahrenheit. |
|
2443 | 386 |
self addConversion:[:f | (f - 32) / 1.8] from:#fahrenheit to:#celsius. |
1135 | 387 |
|
388 |
" |
|
389 |
Conversions := nil. |
|
390 |
UnitConverter initializeConversions |
|
391 |
" |
|
392 |
||
393 |
"Created: / 22.7.1997 / 13:56:40 / cg" |
|
394 |
"Modified: / 10.10.2001 / 14:39:04 / cg" |
|
395 |
! |
|
396 |
||
397 |
initializePrintValues |
|
363 | 398 |
"/ inch to (roughly) a typesetter point |
399 |
self addConversion:(1/72) from:#point to:#inch. |
|
400 |
||
401 |
"/ point to twips; 20 twips (as in Rich-Text-Format) make a point |
|
402 |
self addConversion:(1/20) from:#twip to:#point. |
|
403 |
||
365 | 404 |
"/ US paper |
405 |
self addConversion:11 from:#'letter-page-height' to:#inch. |
|
406 |
self addConversion:8.5 from:#'letter-page-width' to:#inch. |
|
407 |
self addConversion:8.5 from:#'letter-landscape-page-height' to:#inch. |
|
408 |
self addConversion:11 from:#'letter-landscape-page-width' to:#inch. |
|
409 |
||
385 | 410 |
self addConversion:14 from:#'legal-page-height' to:#inch. |
411 |
self addConversion:8.5 from:#'legal-page-width' to:#inch. |
|
412 |
self addConversion:8.5 from:#'legal-landscape-page-height' to:#inch. |
|
413 |
self addConversion:14 from:#'legal-landscape-page-width' to:#inch. |
|
414 |
||
415 |
self addConversion:11 from:#'ledger-page-height' to:#inch. |
|
416 |
self addConversion:17 from:#'ledger-page-width' to:#inch. |
|
417 |
self addConversion:17 from:#'ledger-landscape-page-height' to:#inch. |
|
418 |
self addConversion:11 from:#'ledger-landscape-page-width' to:#inch. |
|
419 |
||
365 | 420 |
"/ European paper |
421 |
self addConversion:840 from:#'a1-page-height' to:#millimeter. |
|
422 |
self addConversion:592 from:#'a1-page-width' to:#millimeter. |
|
423 |
self addConversion:592 from:#'a1-landscape-page-height' to:#millimeter. |
|
424 |
self addConversion:840 from:#'a1-landscape-page-width' to:#millimeter. |
|
425 |
||
426 |
self addConversion:592 from:#'a2-page-height' to:#millimeter. |
|
427 |
self addConversion:420 from:#'a2-page-width' to:#millimeter. |
|
428 |
self addConversion:420 from:#'a2-landscape-page-height' to:#millimeter. |
|
429 |
self addConversion:592 from:#'a2-landscape-page-width' to:#millimeter. |
|
430 |
||
431 |
self addConversion:420 from:#'a3-page-height' to:#millimeter. |
|
432 |
self addConversion:296 from:#'a3-page-width' to:#millimeter. |
|
433 |
self addConversion:296 from:#'a3-landscape-page-height' to:#millimeter. |
|
434 |
self addConversion:420 from:#'a3-landscape-page-width' to:#millimeter. |
|
435 |
||
436 |
self addConversion:296 from:#'a4-page-height' to:#millimeter. |
|
437 |
self addConversion:210 from:#'a4-page-width' to:#millimeter. |
|
438 |
self addConversion:210 from:#'a4-landscape-page-height' to:#millimeter. |
|
439 |
self addConversion:296 from:#'a4-landscape-page-width' to:#millimeter. |
|
440 |
||
441 |
self addConversion:210 from:#'a5-page-height' to:#millimeter. |
|
442 |
self addConversion:148 from:#'a5-page-width' to:#millimeter. |
|
443 |
self addConversion:148 from:#'a5-landscape-page-height' to:#millimeter. |
|
444 |
self addConversion:210 from:#'a5-landscape-page-width' to:#millimeter. |
|
445 |
||
446 |
self addConversion:148 from:#'a6-page-height' to:#millimeter. |
|
447 |
self addConversion:105 from:#'a6-page-width' to:#millimeter. |
|
448 |
self addConversion:105 from:#'a6-landscape-page-height' to:#millimeter. |
|
449 |
self addConversion:148 from:#'a6-landscape-page-width' to:#millimeter. |
|
450 |
||
385 | 451 |
self addConversion:257 from:#'b5-page-height' to:#millimeter. |
452 |
self addConversion:182 from:#'b5-page-width' to:#millimeter. |
|
453 |
self addConversion:182 from:#'b5-landscape-page-height' to:#millimeter. |
|
454 |
self addConversion:257 from:#'b5-landscape-page-width' to:#millimeter. |
|
455 |
||
456 |
||
365 | 457 |
Aliases at:#letterW put:#'letter-page-width'. |
458 |
Aliases at:#letterH put:#'letter-page-height'. |
|
385 | 459 |
Aliases at:#legalW put:#'legal-page-width'. |
460 |
Aliases at:#legalH put:#'legal-page-height'. |
|
461 |
Aliases at:#ledgerW put:#'ledger-page-width'. |
|
462 |
Aliases at:#ledgerH put:#'ledger-page-height'. |
|
365 | 463 |
Aliases at:#a1W put:#'a1-page-width'. |
464 |
Aliases at:#a1H put:#'a1-page-height'. |
|
465 |
Aliases at:#a2W put:#'a2-page-width'. |
|
466 |
Aliases at:#a2H put:#'a2-page-height'. |
|
467 |
Aliases at:#a3W put:#'a3-page-width'. |
|
468 |
Aliases at:#a3H put:#'a3-page-height'. |
|
469 |
Aliases at:#a4W put:#'a4-page-width'. |
|
470 |
Aliases at:#a4H put:#'a4-page-height'. |
|
471 |
Aliases at:#a5W put:#'a5-page-width'. |
|
472 |
Aliases at:#a5H put:#'a5-page-height'. |
|
473 |
Aliases at:#a6W put:#'a6-page-width'. |
|
474 |
Aliases at:#a6H put:#'a6-page-height'. |
|
385 | 475 |
Aliases at:#b5W put:#'b5-page-width'. |
476 |
Aliases at:#b5H put:#'b5-page-height'. |
|
365 | 477 |
|
478 |
Aliases at:#letterlW put:#'letter-landscape-page-width'. |
|
479 |
Aliases at:#letterlH put:#'letter-landscape-page-height'. |
|
385 | 480 |
Aliases at:#legallW put:#'legal-landscape-page-width'. |
481 |
Aliases at:#legallH put:#'legal-landscape-page-height'. |
|
482 |
Aliases at:#ledgerlW put:#'ledger-landscape-page-width'. |
|
483 |
Aliases at:#ledgerlH put:#'ledger-landscape-page-height'. |
|
365 | 484 |
Aliases at:#a1lW put:#'a1-landscape-page-width'. |
485 |
Aliases at:#a1lH put:#'a1-landscape-page-height'. |
|
486 |
Aliases at:#a2lW put:#'a2-landscape-page-width'. |
|
487 |
Aliases at:#a2lH put:#'a2-landscape-page-height'. |
|
488 |
Aliases at:#a3lW put:#'a3-landscape-page-width'. |
|
489 |
Aliases at:#a3lH put:#'a3-landscape-page-height'. |
|
490 |
Aliases at:#a4lW put:#'a4-landscape-page-width'. |
|
491 |
Aliases at:#a4lH put:#'a4-landscape-page-height'. |
|
492 |
Aliases at:#a5lW put:#'a5-landscape-page-width'. |
|
493 |
Aliases at:#a5lH put:#'a5-landscape-page-height'. |
|
494 |
Aliases at:#a6lW put:#'a6-landscape-page-width'. |
|
495 |
Aliases at:#a6lH put:#'a6-landscape-page-height'. |
|
385 | 496 |
Aliases at:#b5lW put:#'b5-landscape-page-width'. |
497 |
Aliases at:#b5lH put:#'b5-landscape-page-height'. |
|
1135 | 498 |
! |
363 | 499 |
|
1135 | 500 |
initializeScaleFactors |
501 |
Scaling := IdentityDictionary new. |
|
363 | 502 |
|
1285 | 503 |
Scaling at:#yotta put:1000000000000000000000000. "/ 10^24 |
504 |
Scaling at:#zetta put:1000000000000000000000. "/ 10^21 |
|
505 |
Scaling at:#exa put:1000000000000000000. "/ 10^18 |
|
506 |
Scaling at:#peta put:1000000000000000. "/ 10^15 |
|
507 |
Scaling at:#tera put:1000000000000. "/ 10^12 |
|
508 |
Scaling at:#giga put:1000000000. "/ 10^9 |
|
509 |
Scaling at:#mega put:1000000. "/ 10^6 |
|
1135 | 510 |
Scaling at:#myria put:10000. |
1285 | 511 |
Scaling at:#kilo put:1000. "/ 10^3 |
512 |
Scaling at:#hecto put:100. "/ 10^2 |
|
513 |
Scaling at:#deca put:10. "/ 10^1 |
|
2320 | 514 |
Scaling at:#deka put:10. "/ 10^1 |
1323 | 515 |
Scaling at:#deci put:(1/10). "/ 10^-1 |
1135 | 516 |
Scaling at:#centi put:(1/100). |
1323 | 517 |
Scaling at:#milli put:(1/1000). "/ 10^-3 |
518 |
Scaling at:#micro put:(1/1000000). "/ 10^-6 |
|
519 |
Scaling at:#nano put:(1/1000000000). "/ 10^-9 |
|
520 |
Scaling at:#pico put:(1/1000000000000). "/ 10^-12 |
|
521 |
Scaling at:#femto put:(1/1000000000000000). "/ 10^-15 |
|
522 |
Scaling at:#atto put:(1/1000000000000000000). "/ 10^-18 |
|
523 |
Scaling at:#zepto put:(1/1000000000000000000000). "/ 10^-21 |
|
524 |
Scaling at:#yocto put:(1/1000000000000000000000000)."/ 10^-24 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
525 |
! ! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
526 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
527 |
!UnitConverter class methodsFor:'accessing'! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
528 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
529 |
scalings |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
530 |
"return the set of known scaling prefixes" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
531 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
532 |
^ Scaling keys |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
533 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
534 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
535 |
UnitConverter scalings |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
536 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
537 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
538 |
"Created: 6.8.1997 / 16:53:19 / cg" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
539 |
"Modified: 6.8.1997 / 16:58:11 / cg" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
540 |
! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
541 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
542 |
units |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
543 |
"return the set of known units" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
544 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
545 |
|setOfUnits| |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
546 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
547 |
setOfUnits := IdentitySet new. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
548 |
Conversions keysAndValuesDo:[:srcUnit :conversionInfo | |
601 | 549 |
setOfUnits add:srcUnit. |
550 |
conversionInfo keysDo:[:targetUnit | |
|
551 |
setOfUnits add:targetUnit |
|
552 |
] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
553 |
]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
554 |
^ setOfUnits |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
555 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
556 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
557 |
UnitConverter units |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
558 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
559 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
560 |
"Created: 6.8.1997 / 16:57:08 / cg" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
561 |
"Modified: 6.8.1997 / 16:57:47 / cg" |
363 | 562 |
! ! |
563 |
||
564 |
!UnitConverter class methodsFor:'conversions'! |
|
565 |
||
566 |
convert:howMany from:sourceUnit to:destUnit |
|
567 |
"given a value in sourceUnit (symbolic), try to convert it |
|
568 |
to destUnit (symbolic); return nil, if the conversion is |
|
569 |
unknown." |
|
570 |
||
516 | 571 |
|u conversions alias rslt sU dU const val unit |
572 |
i suNumerator suDenominator duNumerator duDenominator uN uD |
|
573 |
sF1 sF2 dF1 dF2 |
|
574 |
s d| |
|
363 | 575 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
576 |
"/ Transcript showCR:('try ' , sourceUnit , '->' , destUnit). |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
577 |
|
550 | 578 |
Conversions isNil ifTrue:[ |
1031 | 579 |
self initializeConversions |
550 | 580 |
]. |
581 |
||
363 | 582 |
"/ somehow, recursion must end ... |
583 |
sourceUnit == destUnit ifTrue:[ |
|
1031 | 584 |
^ howMany |
363 | 585 |
]. |
586 |
||
369 | 587 |
sourceUnit isSymbol ifFalse:[ |
1031 | 588 |
s := sourceUnit withoutSeparators. |
589 |
sU := s asSymbolIfInterned. |
|
590 |
sU notNil ifTrue:[ |
|
591 |
^ self convert:howMany from:sU to:destUnit |
|
592 |
]. |
|
593 |
(s startsWith:$() ifTrue:[ |
|
594 |
(s endsWith:$)) ifTrue:[ |
|
595 |
s := s copyFrom:2 to:(s size - 1). |
|
596 |
^ self convert:howMany from:s to:destUnit |
|
597 |
] |
|
598 |
]. |
|
369 | 599 |
]. |
600 |
destUnit isSymbol ifFalse:[ |
|
1031 | 601 |
d := destUnit withoutSeparators. |
602 |
dU := d asSymbolIfInterned. |
|
603 |
dU notNil ifTrue:[ |
|
604 |
^ self convert:howMany from:sourceUnit to:dU |
|
605 |
]. |
|
606 |
(d startsWith:$() ifTrue:[ |
|
607 |
(d endsWith:$)) ifTrue:[ |
|
608 |
d := d copyFrom:2 to:(d size - 1). |
|
609 |
^ self convert:howMany from:sourceUnit to:d |
|
610 |
] |
|
611 |
]. |
|
369 | 612 |
]. |
613 |
||
363 | 614 |
"/ first, get rid of scalers ... |
615 |
||
616 |
u := self unscaled:sourceUnit. |
|
617 |
u notNil ifTrue:[ |
|
1031 | 618 |
^ self convert:(howMany*(u value)) from:(u key) to:destUnit |
363 | 619 |
]. |
620 |
||
621 |
u := self unscaled:destUnit. |
|
622 |
u notNil ifTrue:[ |
|
1031 | 623 |
^ self convert:(howMany/(u value)) from:sourceUnit to:(u key) |
363 | 624 |
]. |
625 |
||
626 |
"/ and of aliases ... |
|
627 |
||
628 |
alias := Aliases at:sourceUnit ifAbsent:nil. |
|
629 |
alias notNil ifTrue:[ |
|
1031 | 630 |
^ self convert:howMany from:alias to:destUnit |
363 | 631 |
]. |
632 |
alias := Aliases at:destUnit ifAbsent:nil. |
|
633 |
alias notNil ifTrue:[ |
|
1031 | 634 |
^ self convert:howMany from:sourceUnit to:alias |
363 | 635 |
]. |
636 |
||
516 | 637 |
"/ any constants ? |
638 |
||
639 |
(Constants includesKey:sourceUnit) ifTrue:[ |
|
1031 | 640 |
const := Constants at:sourceUnit. |
641 |
val := const at:1. |
|
642 |
unit := const at:2. |
|
643 |
^ self convert:(howMany*val) from:unit to:destUnit |
|
516 | 644 |
]. |
645 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
646 |
"/ compounds (^ , / or *) are very naively parsed |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
647 |
"/ need a full expression parser (tree) for full power. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
648 |
"/ I leave that as an excercise to you ... |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
649 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
650 |
|
363 | 651 |
"/ working with squares or cubics ? |
652 |
||
516 | 653 |
((sourceUnit endsWith:'^2') and:[destUnit endsWith:'^2']) ifTrue:[ |
2988
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
654 |
sU := (sourceUnit copyButLast:2) asSymbolIfInterned. |
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
655 |
dU := (destUnit copyButLast:2) asSymbolIfInterned. |
1031 | 656 |
(sU notNil and:[dU notNil]) ifTrue:[ |
657 |
^ (self convert:(howMany sqrt) from:sU to:dU) squared |
|
658 |
]. |
|
659 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
363 | 660 |
]. |
516 | 661 |
((sourceUnit endsWith:'^3') and:[destUnit endsWith:'^3']) ifTrue:[ |
2988
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
662 |
sU := (sourceUnit copyButLast:2) asSymbolIfInterned. |
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
663 |
dU := (destUnit copyButLast:2) asSymbolIfInterned. |
1031 | 664 |
(sU notNil and:[dU notNil]) ifTrue:[ |
665 |
^ (self convert:(howMany raisedTo:(1/3)) from:sU to:dU) raisedTo:3 |
|
666 |
]. |
|
667 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
516 | 668 |
]. |
669 |
||
670 |
"/ working with fractions ? |
|
671 |
||
672 |
((sourceUnit includes:$/) and:[destUnit includes:$/]) ifTrue:[ |
|
1031 | 673 |
"/ look for a constant conversion factor |
674 |
i := sourceUnit indexOf:$/. |
|
675 |
suNumerator := sourceUnit copyTo:(i - 1). |
|
676 |
suDenominator := sourceUnit copyFrom:(i + 1). |
|
677 |
i := destUnit indexOf:$/. |
|
678 |
duNumerator := destUnit copyTo:(i - 1). |
|
679 |
duDenominator := destUnit copyFrom:(i + 1). |
|
516 | 680 |
|
1031 | 681 |
uN := self convert:howMany from:suNumerator to:duNumerator. |
682 |
uN notNil ifTrue:[ |
|
683 |
uD := self convert:1 from:suDenominator to:duDenominator. |
|
684 |
uD notNil ifTrue:[ |
|
685 |
^ uN / uD |
|
686 |
] |
|
687 |
]. |
|
516 | 688 |
]. |
689 |
||
690 |
"/ working with products ? |
|
691 |
||
692 |
((sourceUnit includes:$*) and:[destUnit includes:$*]) ifTrue:[ |
|
1031 | 693 |
i := sourceUnit indexOf:$*. |
694 |
sF1 := sourceUnit copyTo:(i - 1). |
|
695 |
sF2 := sourceUnit copyFrom:(i + 1). |
|
696 |
i := destUnit indexOf:$*. |
|
697 |
dF1 := destUnit copyTo:(i - 1). |
|
698 |
dF2 := destUnit copyFrom:(i + 1). |
|
516 | 699 |
|
1031 | 700 |
u := self convert:howMany from:sF1 to:dF1. |
701 |
u notNil ifTrue:[ |
|
702 |
u := self convert:u from:sF2 to:dF2. |
|
703 |
u notNil ifTrue:[ |
|
704 |
^ u |
|
705 |
] |
|
706 |
]. |
|
363 | 707 |
]. |
708 |
||
709 |
"/ the real work comes here ... |
|
710 |
||
711 |
"/ is there a direct conversion in the dataBase ? |
|
712 |
||
713 |
rslt := self convertDirect:howMany from:sourceUnit to:destUnit. |
|
714 |
rslt notNil ifTrue:[^ rslt]. |
|
715 |
||
716 |
"/ try inverse conversion ... |
|
717 |
||
718 |
rslt := self convertDirect:1 from:destUnit to:sourceUnit. |
|
719 |
rslt notNil ifTrue:[ ^ howMany / rslt]. |
|
720 |
||
516 | 721 |
"/ here's the deep recursion ... |
363 | 722 |
|
723 |
||
724 |
"/ try indirect conversion from source |
|
725 |
||
726 |
conversions := Conversions at:sourceUnit ifAbsent:nil. |
|
727 |
conversions notNil ifTrue:[ |
|
1031 | 728 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 | |
729 |
|factor2| |
|
363 | 730 |
|
1031 | 731 |
factor2 := self convert:factor1 from:intermediateUnit to:destUnit. |
732 |
factor2 notNil ifTrue:[^ factor2 * howMany]. |
|
733 |
]. |
|
363 | 734 |
]. |
735 |
||
736 |
"/ try indirect conversion from dest |
|
737 |
||
738 |
conversions := Conversions at:destUnit ifAbsent:nil. |
|
739 |
conversions notNil ifTrue:[ |
|
1031 | 740 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 | |
741 |
|factor2| |
|
363 | 742 |
|
1031 | 743 |
factor2 := self convert:(factor1) from:intermediateUnit to:sourceUnit. |
744 |
factor2 notNil ifTrue:[^ howMany / factor2]. |
|
745 |
]. |
|
363 | 746 |
]. |
747 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
748 |
"/ if working with a product, try each component |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
749 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
750 |
(sourceUnit includes:$*) ifTrue:[ |
1031 | 751 |
i := sourceUnit indexOf:$*. |
752 |
sF1 := sourceUnit copyTo:(i - 1). |
|
753 |
sF2 := sourceUnit copyFrom:(i + 1). |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
754 |
|
1031 | 755 |
"/ see what we have ... |
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
756 |
|
1031 | 757 |
self units do:[:aUnit | |
758 |
|pref iUnit factor2 rslt| |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
759 |
|
1031 | 760 |
pref := sF1 , '*'. |
761 |
(aUnit startsWith:pref) ifTrue:[ |
|
762 |
"/ ok; want a/b -> x |
|
763 |
"/ found a/c -> any |
|
764 |
"/ what about c->b ? |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
765 |
|
1031 | 766 |
iUnit := aUnit copyFrom:pref size + 1. |
767 |
factor2 := self convert:1 from:sF2 to:iUnit. |
|
768 |
factor2 notNil ifTrue:[ |
|
769 |
"/ good ... |
|
770 |
rslt := self convert:(factor2 * howMany) |
|
771 |
from:aUnit to:destUnit. |
|
772 |
rslt notNil ifTrue:[^ rslt]. |
|
773 |
] |
|
774 |
] |
|
775 |
]. |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
776 |
]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
777 |
|
516 | 778 |
^ self noConversionFrom:sourceUnit to:destUnit. |
363 | 779 |
|
780 |
"direct - how many meters are there in two inches: |
|
781 |
UnitConverter convert:2 from:#inch to:#meter |
|
515 | 782 |
UnitConverter convert:37 from:#degrees to:#fahrenheit |
363 | 783 |
|
784 |
reverse - how many inches are there in one meter |
|
785 |
UnitConverter convert:1 from:#meter to:#inch |
|
786 |
||
787 |
with alias: |
|
788 |
UnitConverter convert:1 from:#inch to:#m |
|
789 |
UnitConverter convert:1 from:#inch to:#mm |
|
790 |
UnitConverter convert:1 from:#inch to:#millimeter |
|
791 |
UnitConverter convert:1 from:#inch to:#nanometer |
|
792 |
UnitConverter convert:1 from:#mm to:#km |
|
793 |
UnitConverter convert:1 from:#km to:#foot |
|
794 |
||
795 |
indirect: |
|
796 |
UnitConverter convert:1 from:#mm to:#twip |
|
797 |
UnitConverter convert:1 from:#inch to:#twip |
|
369 | 798 |
UnitConverter convert:1 from:'letterH' to:#point |
799 |
UnitConverter convert:1 from:'letterlH' to:#point |
|
364 | 800 |
|
801 |
UnitConverter convert:5 from:#barrel to:#liter |
|
802 |
UnitConverter convert:10 from:#kilogram to:#carat |
|
803 |
||
804 |
UnitConverter convert:1 from:#liter to:#floz |
|
363 | 805 |
" |
806 |
||
807 |
"Created: 31.5.1996 / 16:23:38 / cg" |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
808 |
"Modified: 6.8.1997 / 18:10:22 / cg" |
363 | 809 |
! |
810 |
||
1292 | 811 |
fileSizeFromString:fileSizeString |
812 |
"given a fileSize string (i.e. a number followed by optional Kb, Mb or Gb as required. |
|
813 |
Provided here since this is so common..." |
|
814 |
||
815 |
|numericValue s scaleChar scale| |
|
816 |
||
817 |
fileSizeString isEmpty ifTrue:[ |
|
818 |
^ 0 |
|
819 |
]. |
|
820 |
||
821 |
scale := 1. |
|
822 |
s := fileSizeString readStream. |
|
823 |
numericValue := Number readFrom:s onError:[0]. |
|
824 |
s skipSeparators. |
|
825 |
s atEnd ifFalse:[ |
|
826 |
scaleChar := s next asLowercase. |
|
827 |
scaleChar = $k ifTrue:[ |
|
828 |
scale := 1024 |
|
829 |
] ifFalse:[ |
|
830 |
scaleChar = $m ifTrue:[ |
|
831 |
scale := 1024 * 1024 |
|
832 |
] ifFalse:[ |
|
833 |
scaleChar = $g ifTrue:[ |
|
834 |
scale := 1024 * 1024 * 1024 |
|
835 |
] ifFalse:[ |
|
836 |
scaleChar = $t ifTrue:[ |
|
837 |
scale := 1024 * 1024 * 1024 * 1024 |
|
838 |
] |
|
839 |
] |
|
840 |
] |
|
841 |
]. |
|
842 |
]. |
|
843 |
^ (numericValue*scale) asInteger |
|
844 |
||
845 |
" |
|
846 |
self fileSizeFromString:'1234' |
|
1295 | 847 |
self fileSizeFromString:'10 k' |
848 |
self fileSizeFromString:'0.6 k' |
|
849 |
self fileSizeFromString:'1.8 M' |
|
850 |
self fileSizeFromString:'0.5 G' |
|
851 |
self fileSizeFromString:'0.5 T' |
|
1292 | 852 |
" |
853 |
! |
|
854 |
||
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
855 |
fileSizeStringFor:nBytes |
1293 | 856 |
"return a useful string for a size-in-bytes; |
1294 | 857 |
Scales by K (*1024), M (*1024*1024) or G (*1024*1024*1024) or even T |
858 |
as required and useful. |
|
1143 | 859 |
Provided here since this is so common..." |
860 |
||
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
861 |
|unitString n| |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
862 |
|
1293 | 863 |
nBytes < (1000 * 1024 * 1024 * 1024) ifTrue:[ |
864 |
nBytes < (1000 * 1024 * 1024) ifTrue:[ |
|
865 |
nBytes < (1000 * 1024) ifTrue:[ |
|
1457 | 866 |
nBytes < 1000 ifTrue:[ |
1293 | 867 |
n := nBytes. |
868 |
unitString := ' '. |
|
869 |
] ifFalse:[ |
|
870 |
n := (nBytes / 1024) asFixedPoint:1. |
|
871 |
unitString := ' K' |
|
872 |
] |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
873 |
] ifFalse:[ |
1293 | 874 |
n := (nBytes / (1024 * 1024)) asFixedPoint:1. |
875 |
unitString := ' M' |
|
876 |
]. |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
877 |
] ifFalse:[ |
1293 | 878 |
n := (nBytes / 1024 / (1024 * 1024)) asFixedPoint:1. |
879 |
unitString := ' G' |
|
880 |
] |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
881 |
] ifFalse:[ |
1293 | 882 |
n := (nBytes / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
883 |
unitString := ' T' |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
884 |
]. |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
885 |
|
1346 | 886 |
"/ n truncated = n ifTrue:[ |
887 |
"/ n := n truncated |
|
888 |
"/ ]. |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
889 |
^ n printString , unitString. |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
890 |
|
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
891 |
" |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
892 |
self fileSizeStringFor:10 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
893 |
self fileSizeStringFor:100 |
1298
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
894 |
self fileSizeStringFor:1000 |
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
895 |
self fileSizeStringFor:8192 |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
896 |
self fileSizeStringFor:10000 |
1298
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
897 |
self fileSizeStringFor:100000 |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
898 |
self fileSizeStringFor:1000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
899 |
self fileSizeStringFor:10000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
900 |
self fileSizeStringFor:100000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
901 |
self fileSizeStringFor:1000000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
902 |
self fileSizeStringFor:10000000000 |
1293 | 903 |
self fileSizeStringFor:100000000000 |
904 |
self fileSizeStringFor:1000000000000 |
|
905 |
self fileSizeStringFor:10000000000000 |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
906 |
" |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
907 |
! |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
908 |
|
3120 | 909 |
frequencyFromString:frequencyString |
910 |
"given a frequency string (i.e. a number followed by optional kHz, Mhz or Hhz as required. |
|
911 |
Provided here since this is so common..." |
|
912 |
||
913 |
|numericValue s scaleChar scale rest| |
|
914 |
||
915 |
frequencyString isEmpty ifTrue:[ |
|
916 |
^ 0 |
|
917 |
]. |
|
918 |
||
919 |
scale := 1. |
|
920 |
s := frequencyString readStream. |
|
921 |
numericValue := Number readFrom:s onError:[0]. |
|
922 |
s skipSeparators. |
|
923 |
s atEnd ifFalse:[ |
|
924 |
scaleChar := s next asLowercase. |
|
925 |
scaleChar = $/ ifTrue:[ |
|
926 |
rest := s upToSeparator asLowercase. |
|
927 |
(rest = 'm' or:[rest = 'min']) ifTrue:[ |
|
928 |
scale := 1/60 |
|
929 |
] ifFalse:[ |
|
930 |
(rest = 'h' or:[rest = 'hr' or:[rest = 'hour']]) ifTrue:[ |
|
931 |
scale := 1/60/60 |
|
932 |
] ifFalse:[ |
|
933 |
(rest = 'd' or:[rest = 'dy' or:[rest = 'day']]) ifTrue:[ |
|
934 |
scale := 1/60/60/24 |
|
935 |
]. |
|
936 |
]. |
|
937 |
]. |
|
938 |
] ifFalse:[ |
|
939 |
scaleChar = $k ifTrue:[ |
|
940 |
scale := 1000 |
|
941 |
] ifFalse:[ |
|
942 |
scaleChar = $m ifTrue:[ |
|
943 |
scale := 1000 * 1000 |
|
944 |
] ifFalse:[ |
|
945 |
scaleChar = $g ifTrue:[ |
|
946 |
scale := 1000 * 1000 * 1000 |
|
947 |
] ifFalse:[ |
|
948 |
scaleChar = $t ifTrue:[ |
|
949 |
scale := 1000 * 1000 * 1000 * 1000 |
|
950 |
] |
|
951 |
] |
|
952 |
] |
|
953 |
]. |
|
954 |
] |
|
955 |
]. |
|
956 |
^ (numericValue*scale) |
|
957 |
||
958 |
" |
|
959 |
self frequencyFromString:'1234' |
|
960 |
self frequencyFromString:'10 kHz' |
|
961 |
self frequencyFromString:'0.6 k' |
|
962 |
self frequencyFromString:'1.8 Mhz' |
|
963 |
self frequencyFromString:'0.5 Ghz' |
|
964 |
self frequencyFromString:'0.5 Thz' |
|
965 |
self frequencyFromString:'10/s' |
|
966 |
self frequencyFromString:'10/min' |
|
967 |
self frequencyFromString:'10/hr' |
|
968 |
" |
|
969 |
! |
|
970 |
||
971 |
frequencyStringFor:hertz |
|
972 |
"return a useful string for a frequency; |
|
973 |
Scales by K (*1000), M (*1000*1000) or G (*1000*1000*1000) or even T |
|
974 |
as required and useful. |
|
975 |
Provided here since this is so common..." |
|
976 |
||
977 |
|unitString n| |
|
978 |
||
979 |
hertz < (1000 * 1000 * 1000 * 1000) ifTrue:[ |
|
980 |
hertz < (1000 * 1000 * 1000) ifTrue:[ |
|
981 |
hertz < (1000 * 1000) ifTrue:[ |
|
982 |
hertz < 1000 ifTrue:[ |
|
983 |
n := hertz. |
|
984 |
unitString := ' Hz'. |
|
985 |
] ifFalse:[ |
|
986 |
n := (hertz / 1000) asFixedPoint:1. |
|
987 |
unitString := ' Khz' |
|
988 |
] |
|
989 |
] ifFalse:[ |
|
990 |
n := (hertz / (1000 * 1000)) asFixedPoint:1. |
|
991 |
unitString := ' Mhz' |
|
992 |
]. |
|
993 |
] ifFalse:[ |
|
994 |
n := (hertz / 1000 / (1000 * 1000)) asFixedPoint:1. |
|
995 |
unitString := ' Ghz' |
|
996 |
] |
|
997 |
] ifFalse:[ |
|
998 |
n := (hertz / (1000 * 1000) / (1000 * 1000)) asFixedPoint:1. |
|
999 |
unitString := ' Thz' |
|
1000 |
]. |
|
1001 |
||
1002 |
^ n printString , unitString. |
|
1003 |
||
1004 |
" |
|
1005 |
self frequencyStringFor:10 |
|
1006 |
self frequencyStringFor:100 |
|
1007 |
self frequencyStringFor:1000 |
|
1008 |
self frequencyStringFor:8192 |
|
1009 |
self frequencyStringFor:10000 |
|
1010 |
self frequencyStringFor:100000 |
|
1011 |
self frequencyStringFor:1000000 |
|
1012 |
self frequencyStringFor:10000000 |
|
1013 |
self frequencyStringFor:100000000 |
|
1014 |
self frequencyStringFor:1000000000 |
|
1015 |
self frequencyStringFor:10000000000 |
|
1016 |
self frequencyStringFor:100000000000 |
|
1017 |
self frequencyStringFor:1000000000000 |
|
1018 |
self frequencyStringFor:10000000000000 |
|
1019 |
" |
|
1020 |
! |
|
1021 |
||
363 | 1022 |
inchToMillimeter:inches |
364 | 1023 |
"convert inches to mm. Made this an extra method, since its so common." |
363 | 1024 |
|
1025 |
^ inches * 25.4 |
|
1026 |
||
1027 |
"Created: 31.5.1996 / 13:37:31 / cg" |
|
364 | 1028 |
"Modified: 31.5.1996 / 18:08:14 / cg" |
363 | 1029 |
! |
1030 |
||
1031 |
millimeterToInch:mm |
|
364 | 1032 |
"convert mm to inches. Made this an extra method, since its so common." |
363 | 1033 |
|
1034 |
^ mm / 25.4 |
|
1035 |
||
1036 |
"Created: 31.5.1996 / 13:37:48 / cg" |
|
364 | 1037 |
"Modified: 31.5.1996 / 18:08:20 / cg" |
363 | 1038 |
! ! |
1039 |
||
516 | 1040 |
!UnitConverter class methodsFor:'missing conversion'! |
1041 |
||
1042 |
noConversionFrom:sourceUnit to:destUnit |
|
1043 |
Transcript showCR:'no conversion ' , sourceUnit printString , ' -> ' , destUnit printString. |
|
1044 |
^ nil |
|
1045 |
||
1046 |
"Created: 29.3.1997 / 17:56:03 / cg" |
|
1047 |
! ! |
|
1048 |
||
363 | 1049 |
!UnitConverter class methodsFor:'private'! |
1050 |
||
1051 |
addConversion:factor from:sourceMetric to:destMetric |
|
1052 |
"add a conversion" |
|
1053 |
||
1054 |
|conversion| |
|
1055 |
||
1056 |
conversion := Conversions at:sourceMetric ifAbsent:nil. |
|
1057 |
conversion isNil ifTrue:[ |
|
601 | 1058 |
conversion := IdentityDictionary new. |
1059 |
Conversions at:sourceMetric put:conversion |
|
363 | 1060 |
]. |
1061 |
conversion at:destMetric put:factor. |
|
1062 |
^ conversion |
|
1063 |
||
1064 |
"Created: 31.5.1996 / 13:51:25 / cg" |
|
515 | 1065 |
"Modified: 29.3.1997 / 17:19:31 / cg" |
363 | 1066 |
! |
1067 |
||
1068 |
convertDirect:howMany from:sourceMetric to:destMetric |
|
1069 |
"given a value in sourceMetric (symbolic), try to convert it |
|
1070 |
to destMetric (symbolic); |
|
1071 |
Only direct conversions are tried; return nil, if the conversion is unknown." |
|
1072 |
||
1073 |
|conversion factor| |
|
1074 |
||
550 | 1075 |
Conversions isNil ifTrue:[ |
601 | 1076 |
self initializeConversions |
550 | 1077 |
]. |
1078 |
||
363 | 1079 |
conversion := Conversions at:sourceMetric ifAbsent:nil. |
1080 |
conversion isNil ifTrue:[^ nil]. |
|
1081 |
||
1082 |
factor := conversion at:destMetric ifAbsent:nil. |
|
1083 |
factor isNil ifTrue:[^ nil]. |
|
1084 |
||
364 | 1085 |
factor isNumber ifTrue:[ |
601 | 1086 |
^ howMany * factor |
364 | 1087 |
]. |
515 | 1088 |
^ factor value:howMany |
363 | 1089 |
|
1090 |
" |
|
1091 |
UnitConverter convertDirect:1 from:#inch to:#meter |
|
1092 |
UnitConverter convertDirect:1 from:#inch to:#millimeter |
|
515 | 1093 |
UnitConverter convertDirect:1 from:#degrees to:#fahrenheit |
363 | 1094 |
" |
1095 |
||
1096 |
"Created: 31.5.1996 / 13:54:33 / cg" |
|
550 | 1097 |
"Modified: 22.7.1997 / 13:57:52 / cg" |
363 | 1098 |
! |
1099 |
||
1100 |
unscaled:what |
|
1101 |
"given a unit, return the base and a factor as assoc, |
|
1102 |
or nil if not found" |
|
1103 |
||
550 | 1104 |
Conversions isNil ifTrue:[ |
2995 | 1105 |
self initializeConversions |
550 | 1106 |
]. |
1107 |
||
363 | 1108 |
Scaling keysAndValuesDo:[:name :factor | |
2995 | 1109 |
|rest| |
363 | 1110 |
|
2995 | 1111 |
(what startsWith:name) ifTrue:[ |
1112 |
rest := what withoutPrefix:name. |
|
1113 |
rest := rest asSymbolIfInterned. |
|
1114 |
rest notNil ifTrue:[ |
|
1115 |
^ rest -> factor. |
|
1116 |
] |
|
1117 |
]. |
|
363 | 1118 |
]. |
1119 |
^ nil |
|
1120 |
||
1121 |
" |
|
1122 |
UnitConverter unscaled:#millimeter |
|
1123 |
UnitConverter unscaled:#nanometer |
|
1124 |
UnitConverter unscaled:#kilometer |
|
1125 |
UnitConverter unscaled:#fuzzymeter |
|
1126 |
" |
|
1127 |
||
550 | 1128 |
"Modified: 22.7.1997 / 13:57:46 / cg" |
363 | 1129 |
! ! |
1130 |
||
1131 |
!UnitConverter class methodsFor:'documentation'! |
|
1132 |
||
1133 |
version |
|
3120 | 1134 |
^ '$Header: /cvs/stx/stx/libbasic2/UnitConverter.st,v 1.42 2013-09-24 21:27:44 cg Exp $' |
2320 | 1135 |
! |
1136 |
||
1137 |
version_CVS |
|
3120 | 1138 |
^ '$Header: /cvs/stx/stx/libbasic2/UnitConverter.st,v 1.42 2013-09-24 21:27:44 cg Exp $' |
363 | 1139 |
! ! |
2988
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
1140 |