author | Claus Gittinger <cg@exept.de> |
Sun, 18 Apr 2010 16:11:29 +0200 | |
changeset 2444 | 6c4c6addc97a |
parent 2443 | e64a7f2f3fb4 |
child 2547 | 283c931ce260 |
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] |
|
165 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
166 |
|
363 | 167 |
real estate buyers might want to know, how many acres |
168 |
a german ar is: |
|
982 | 169 |
[exBegin] |
170 |
Transcript showCR: |
|
171 |
(UnitConverter convert:1 from:#'german-ar' to:#acre) |
|
172 |
[exEnd] |
|
516 | 173 |
- or how many square-feet are there in your living room: |
982 | 174 |
[exBegin] |
175 |
Transcript showCR: |
|
176 |
(UnitConverter convert:100 from:#'meter^2' to:#'foot^2') |
|
177 |
[exEnd] |
|
1031 | 178 |
- or how about: |
179 |
[exBegin] |
|
180 |
Transcript showCR: |
|
181 |
(UnitConverter convert:3600 from:#'ft*ft' to:#'m*,') |
|
182 |
[exEnd] |
|
183 |
||
516 | 184 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
185 |
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
|
186 |
(roughly, since a teaspoon is not a standard unit) |
982 | 187 |
[exBegin] |
188 |
Transcript showCR: |
|
189 |
(UnitConverter convert:1 from:#'meter^3' to:#teaspoon) |
|
190 |
[exEnd] |
|
365 | 191 |
how wide is a US page in inches: |
982 | 192 |
[exBegin] |
193 |
Transcript showCR: |
|
194 |
(UnitConverter convert:1 from:#'letterW' to:#inch) |
|
195 |
[exEnd] |
|
516 | 196 |
- in millimeter: |
982 | 197 |
[exBegin] |
198 |
Transcript showCR: |
|
199 |
(UnitConverter convert:1 from:#'letterW' to:#mm) |
|
200 |
[exEnd] |
|
516 | 201 |
|
202 |
||
365 | 203 |
the height of a US page in inches: |
982 | 204 |
[exBegin] |
205 |
Transcript showCR: |
|
206 |
(UnitConverter convert:1 from:#'letterH' to:#inch) |
|
207 |
[exEnd] |
|
516 | 208 |
- in millimeter: |
982 | 209 |
[exBegin] |
210 |
Transcript showCR: |
|
211 |
(UnitConverter convert:1 from:#'letterH' to:#mm) |
|
212 |
[exEnd] |
|
516 | 213 |
|
214 |
||
365 | 215 |
the same for european A4 standard page: |
982 | 216 |
[exBegin] |
217 |
Transcript showCR: |
|
218 |
(UnitConverter convert:1 from:#'a4H' to:#mm) |
|
219 |
[exEnd] |
|
220 |
[exBegin] |
|
221 |
Transcript showCR: |
|
222 |
(UnitConverter convert:1 from:#'a4H' to:#inch) |
|
223 |
[exEnd] |
|
2443 | 224 |
|
225 |
the mass of a proton: |
|
226 |
[exBegin] |
|
227 |
Transcript showCR: |
|
228 |
(UnitConverter convert:1 from:#'proton' to:#eV) |
|
229 |
[exEnd] |
|
230 |
the energy of a single proton in the LHC (as of 2010) |
|
231 |
[exBegin] |
|
232 |
Transcript showCR: |
|
233 |
(UnitConverter convert:7 from:#'TeV' to:#joule) |
|
234 |
[exEnd] |
|
363 | 235 |
" |
236 |
! ! |
|
237 |
||
238 |
!UnitConverter class methodsFor:'initialization'! |
|
239 |
||
550 | 240 |
initializeConversions |
363 | 241 |
"initialize common conversions" |
242 |
||
1135 | 243 |
self initializeScaleFactors. |
244 |
||
363 | 245 |
Conversions := IdentityDictionary new. |
246 |
Aliases := IdentityDictionary new. |
|
516 | 247 |
Constants := IdentityDictionary new. |
363 | 248 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
249 |
"/ ---------- velocity ------------- |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
250 |
|
556 | 251 |
Constants at:#lightspeed put:#(2.997925E8 #'m/s'). |
2443 | 252 |
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
|
253 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
254 |
|
363 | 255 |
"/ -------------- length ------------- |
256 |
||
257 |
Aliases at:#km put:#kilometer. |
|
258 |
Aliases at:#m put:#meter. |
|
259 |
Aliases at:#dm put:#decimeter. |
|
260 |
Aliases at:#cm put:#centimeter. |
|
261 |
Aliases at:#mm put:#millimeter. |
|
262 |
Aliases at:#micron put:#micrometer. |
|
263 |
Aliases at:#nm put:#nanometer. |
|
264 |
Aliases at:#angstrom put:#decinanometer. |
|
265 |
||
266 |
"/ US |
|
267 |
self addConversion:12 from:#foot to:#inch. |
|
268 |
self addConversion:3 from:#yard to:#foot. |
|
269 |
self addConversion:5280 from:#mile to:#foot. |
|
515 | 270 |
self addConversion:0.001 from:#inch to:#mil. |
363 | 271 |
|
2320 | 272 |
Aliases at:#feet put:#foot. |
273 |
Aliases at:#ft put:#foot. |
|
274 |
Aliases at:#yd put:#yard. |
|
275 |
Aliases at:#mi put:#mile. |
|
276 |
Aliases at:#mph put:#'mile/hr'. |
|
363 | 277 |
|
278 |
"/ inch to millimeter |
|
279 |
self addConversion:(25.4/1000) from:#inch to:#meter. |
|
280 |
||
365 | 281 |
"/ nautic |
282 |
self addConversion:1852 from:#'nautical-mile' to:#meter. |
|
283 |
||
556 | 284 |
Aliases at:#lightsecond put:#'lightspeed*s'. |
285 |
Aliases at:#lightyear put:#'lightspeed*yr'. |
|
286 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
287 |
self addConversion:((Constants at:#lightspeed) at:1) from:#'lightspeed*s' to:#'meter'. |
385 | 288 |
|
516 | 289 |
"/ ---------- time ------------- |
290 |
||
291 |
self addConversion:60 from:#min to:#s. |
|
292 |
self addConversion:60 from:#hr to:#min. |
|
293 |
self addConversion:24 from:#dy to:#hr. |
|
294 |
self addConversion:365.24219879 from:#yr to:#dy. |
|
295 |
||
2320 | 296 |
Aliases at:#h put:#'hr'. |
297 |
Aliases at:#d put:#'dy'. |
|
516 | 298 |
|
385 | 299 |
"/ ---------- printing ------------- |
1135 | 300 |
self initializePrintValues. |
365 | 301 |
|
1135 | 302 |
"/ ---------------- area ------------------- |
303 |
||
304 |
"/ US |
|
305 |
self addConversion:4840 from:#acre to:#'yard^2'. |
|
306 |
||
307 |
self addConversion:100 from:#are to:#'meter^2'. |
|
308 |
self addConversion:100 from:#hectare to:#are. |
|
309 |
||
310 |
"/ german area - add your countries, and return to me ... |
|
311 |
Aliases at:#'german-ar' put:#are. |
|
312 |
Aliases at:#'german-hektar' put:#hectare. |
|
313 |
||
314 |
||
315 |
"/ ---------------- liquid ---------------- |
|
316 |
||
317 |
self addConversion:231 from:#gallon to:#'inch^3'. |
|
318 |
self addConversion:(1/4) from:#quart to:#gallon. "/ well - at least here, |
|
319 |
"/ thats also 1/4th of a good wine ;-) |
|
320 |
self addConversion:(1/2) from:#pint to:#quart. |
|
321 |
self addConversion:(1/16) from:#floz to:#pint. |
|
322 |
self addConversion:(1/8) from:#fldr to:#floz. |
|
323 |
||
324 |
self addConversion:42 from:#barrel to:#gallon. |
|
325 |
self addConversion:35.23907 from:#bushel to:#liter. |
|
326 |
||
327 |
Aliases at:#cc put:#'cm^3'. |
|
328 |
Aliases at:#liter put:#kilocc. |
|
329 |
Aliases at:#ml put:#milliliter. |
|
330 |
Aliases at:#gal put:#gallon. |
|
331 |
Aliases at:#qt put:#quart. |
|
332 |
||
2320 | 333 |
"/ well, a pint of beer is not always: |
334 |
self addConversion:277.420 from:#'british-gallon' to:#'inch^3'. |
|
335 |
self addConversion:(1/4) from:#'british-quart' to:#'british-gallon'. |
|
336 |
self addConversion:(1/2) from:#'british-pint' to:#'british-quart'. |
|
337 |
self addConversion:(1/16) from:#'british-floz' to:#'british-pint'. |
|
1135 | 338 |
|
339 |
"/ ---------------- mass ---------------- |
|
340 |
||
341 |
self addConversion:28.35 from:#ounce to:#gram. |
|
342 |
self addConversion:453.59237 from:#lb to:#gram. |
|
343 |
self addConversion:205 from:#carat to:#milligram. |
|
344 |
||
2444
6c4c6addc97a
changed: #initializeConversions
Claus Gittinger <cg@exept.de>
parents:
2443
diff
changeset
|
345 |
self addConversion:0.938272013 from:#proton to:#GeV. |
6c4c6addc97a
changed: #initializeConversions
Claus Gittinger <cg@exept.de>
parents:
2443
diff
changeset
|
346 |
self addConversion:0.510998910 from:#electron to:#MeV. |
6c4c6addc97a
changed: #initializeConversions
Claus Gittinger <cg@exept.de>
parents:
2443
diff
changeset
|
347 |
self addConversion:0.939565346 from:#neutron to:#GeV. |
2443 | 348 |
|
1135 | 349 |
Aliases at:#oz put:#ounce. |
350 |
Aliases at:#lbs put:#lb. |
|
351 |
||
352 |
Aliases at:#gm put:#gram. |
|
353 |
Aliases at:#kg put:#kilogram. |
|
354 |
||
355 |
"/ ---------------- energy ---------------- |
|
356 |
||
357 |
Aliases at:#cal put:#calorie. |
|
358 |
Aliases at:#nt put:#newton. |
|
359 |
Aliases at:#joule put:#'nt*m'. |
|
2320 | 360 |
Aliases at:#J put:#'joule'. |
361 |
Aliases at:#N put:#'newton'. |
|
2443 | 362 |
Aliases at:#eV put:#'electronvolt'. |
363 |
Aliases at:#MeV put:#'megaeV'. |
|
364 |
Aliases at:#GeV put:#'gigaeV'. |
|
365 |
Aliases at:#TeV put:#'teraeV'. |
|
2320 | 366 |
|
367 |
Aliases at:#watt put:#'J/s'. |
|
1135 | 368 |
|
369 |
self addConversion:4.1868 from:#calorie to:#joule. |
|
2443 | 370 |
self addConversion:1.60217653E-19 from:#electronvolt to:#joule. |
1135 | 371 |
|
372 |
||
373 |
"/ ---------------- cooking ---------------- |
|
374 |
||
375 |
self addConversion:4 from:#tablespoon to:#fldr. |
|
376 |
self addConversion:(4/3) from:#teaspoon to:#fldr. |
|
377 |
||
378 |
||
379 |
"/ ---------------- temperature ---------------- |
|
380 |
||
381 |
self addConversion:[:d | d * 1.8 + 32] from:#celsius to:#fahrenheit. |
|
2443 | 382 |
self addConversion:[:f | (f - 32) / 1.8] from:#fahrenheit to:#celsius. |
1135 | 383 |
|
384 |
" |
|
385 |
Conversions := nil. |
|
386 |
UnitConverter initializeConversions |
|
387 |
" |
|
388 |
||
389 |
"Created: / 22.7.1997 / 13:56:40 / cg" |
|
390 |
"Modified: / 10.10.2001 / 14:39:04 / cg" |
|
391 |
! |
|
392 |
||
393 |
initializePrintValues |
|
363 | 394 |
"/ inch to (roughly) a typesetter point |
395 |
self addConversion:(1/72) from:#point to:#inch. |
|
396 |
||
397 |
"/ point to twips; 20 twips (as in Rich-Text-Format) make a point |
|
398 |
self addConversion:(1/20) from:#twip to:#point. |
|
399 |
||
365 | 400 |
"/ US paper |
401 |
self addConversion:11 from:#'letter-page-height' to:#inch. |
|
402 |
self addConversion:8.5 from:#'letter-page-width' to:#inch. |
|
403 |
self addConversion:8.5 from:#'letter-landscape-page-height' to:#inch. |
|
404 |
self addConversion:11 from:#'letter-landscape-page-width' to:#inch. |
|
405 |
||
385 | 406 |
self addConversion:14 from:#'legal-page-height' to:#inch. |
407 |
self addConversion:8.5 from:#'legal-page-width' to:#inch. |
|
408 |
self addConversion:8.5 from:#'legal-landscape-page-height' to:#inch. |
|
409 |
self addConversion:14 from:#'legal-landscape-page-width' to:#inch. |
|
410 |
||
411 |
self addConversion:11 from:#'ledger-page-height' to:#inch. |
|
412 |
self addConversion:17 from:#'ledger-page-width' to:#inch. |
|
413 |
self addConversion:17 from:#'ledger-landscape-page-height' to:#inch. |
|
414 |
self addConversion:11 from:#'ledger-landscape-page-width' to:#inch. |
|
415 |
||
365 | 416 |
"/ European paper |
417 |
self addConversion:840 from:#'a1-page-height' to:#millimeter. |
|
418 |
self addConversion:592 from:#'a1-page-width' to:#millimeter. |
|
419 |
self addConversion:592 from:#'a1-landscape-page-height' to:#millimeter. |
|
420 |
self addConversion:840 from:#'a1-landscape-page-width' to:#millimeter. |
|
421 |
||
422 |
self addConversion:592 from:#'a2-page-height' to:#millimeter. |
|
423 |
self addConversion:420 from:#'a2-page-width' to:#millimeter. |
|
424 |
self addConversion:420 from:#'a2-landscape-page-height' to:#millimeter. |
|
425 |
self addConversion:592 from:#'a2-landscape-page-width' to:#millimeter. |
|
426 |
||
427 |
self addConversion:420 from:#'a3-page-height' to:#millimeter. |
|
428 |
self addConversion:296 from:#'a3-page-width' to:#millimeter. |
|
429 |
self addConversion:296 from:#'a3-landscape-page-height' to:#millimeter. |
|
430 |
self addConversion:420 from:#'a3-landscape-page-width' to:#millimeter. |
|
431 |
||
432 |
self addConversion:296 from:#'a4-page-height' to:#millimeter. |
|
433 |
self addConversion:210 from:#'a4-page-width' to:#millimeter. |
|
434 |
self addConversion:210 from:#'a4-landscape-page-height' to:#millimeter. |
|
435 |
self addConversion:296 from:#'a4-landscape-page-width' to:#millimeter. |
|
436 |
||
437 |
self addConversion:210 from:#'a5-page-height' to:#millimeter. |
|
438 |
self addConversion:148 from:#'a5-page-width' to:#millimeter. |
|
439 |
self addConversion:148 from:#'a5-landscape-page-height' to:#millimeter. |
|
440 |
self addConversion:210 from:#'a5-landscape-page-width' to:#millimeter. |
|
441 |
||
442 |
self addConversion:148 from:#'a6-page-height' to:#millimeter. |
|
443 |
self addConversion:105 from:#'a6-page-width' to:#millimeter. |
|
444 |
self addConversion:105 from:#'a6-landscape-page-height' to:#millimeter. |
|
445 |
self addConversion:148 from:#'a6-landscape-page-width' to:#millimeter. |
|
446 |
||
385 | 447 |
self addConversion:257 from:#'b5-page-height' to:#millimeter. |
448 |
self addConversion:182 from:#'b5-page-width' to:#millimeter. |
|
449 |
self addConversion:182 from:#'b5-landscape-page-height' to:#millimeter. |
|
450 |
self addConversion:257 from:#'b5-landscape-page-width' to:#millimeter. |
|
451 |
||
452 |
||
365 | 453 |
Aliases at:#letterW put:#'letter-page-width'. |
454 |
Aliases at:#letterH put:#'letter-page-height'. |
|
385 | 455 |
Aliases at:#legalW put:#'legal-page-width'. |
456 |
Aliases at:#legalH put:#'legal-page-height'. |
|
457 |
Aliases at:#ledgerW put:#'ledger-page-width'. |
|
458 |
Aliases at:#ledgerH put:#'ledger-page-height'. |
|
365 | 459 |
Aliases at:#a1W put:#'a1-page-width'. |
460 |
Aliases at:#a1H put:#'a1-page-height'. |
|
461 |
Aliases at:#a2W put:#'a2-page-width'. |
|
462 |
Aliases at:#a2H put:#'a2-page-height'. |
|
463 |
Aliases at:#a3W put:#'a3-page-width'. |
|
464 |
Aliases at:#a3H put:#'a3-page-height'. |
|
465 |
Aliases at:#a4W put:#'a4-page-width'. |
|
466 |
Aliases at:#a4H put:#'a4-page-height'. |
|
467 |
Aliases at:#a5W put:#'a5-page-width'. |
|
468 |
Aliases at:#a5H put:#'a5-page-height'. |
|
469 |
Aliases at:#a6W put:#'a6-page-width'. |
|
470 |
Aliases at:#a6H put:#'a6-page-height'. |
|
385 | 471 |
Aliases at:#b5W put:#'b5-page-width'. |
472 |
Aliases at:#b5H put:#'b5-page-height'. |
|
365 | 473 |
|
474 |
Aliases at:#letterlW put:#'letter-landscape-page-width'. |
|
475 |
Aliases at:#letterlH put:#'letter-landscape-page-height'. |
|
385 | 476 |
Aliases at:#legallW put:#'legal-landscape-page-width'. |
477 |
Aliases at:#legallH put:#'legal-landscape-page-height'. |
|
478 |
Aliases at:#ledgerlW put:#'ledger-landscape-page-width'. |
|
479 |
Aliases at:#ledgerlH put:#'ledger-landscape-page-height'. |
|
365 | 480 |
Aliases at:#a1lW put:#'a1-landscape-page-width'. |
481 |
Aliases at:#a1lH put:#'a1-landscape-page-height'. |
|
482 |
Aliases at:#a2lW put:#'a2-landscape-page-width'. |
|
483 |
Aliases at:#a2lH put:#'a2-landscape-page-height'. |
|
484 |
Aliases at:#a3lW put:#'a3-landscape-page-width'. |
|
485 |
Aliases at:#a3lH put:#'a3-landscape-page-height'. |
|
486 |
Aliases at:#a4lW put:#'a4-landscape-page-width'. |
|
487 |
Aliases at:#a4lH put:#'a4-landscape-page-height'. |
|
488 |
Aliases at:#a5lW put:#'a5-landscape-page-width'. |
|
489 |
Aliases at:#a5lH put:#'a5-landscape-page-height'. |
|
490 |
Aliases at:#a6lW put:#'a6-landscape-page-width'. |
|
491 |
Aliases at:#a6lH put:#'a6-landscape-page-height'. |
|
385 | 492 |
Aliases at:#b5lW put:#'b5-landscape-page-width'. |
493 |
Aliases at:#b5lH put:#'b5-landscape-page-height'. |
|
1135 | 494 |
! |
363 | 495 |
|
1135 | 496 |
initializeScaleFactors |
497 |
Scaling := IdentityDictionary new. |
|
363 | 498 |
|
1285 | 499 |
Scaling at:#yotta put:1000000000000000000000000. "/ 10^24 |
500 |
Scaling at:#zetta put:1000000000000000000000. "/ 10^21 |
|
501 |
Scaling at:#exa put:1000000000000000000. "/ 10^18 |
|
502 |
Scaling at:#peta put:1000000000000000. "/ 10^15 |
|
503 |
Scaling at:#tera put:1000000000000. "/ 10^12 |
|
504 |
Scaling at:#giga put:1000000000. "/ 10^9 |
|
505 |
Scaling at:#mega put:1000000. "/ 10^6 |
|
1135 | 506 |
Scaling at:#myria put:10000. |
1285 | 507 |
Scaling at:#kilo put:1000. "/ 10^3 |
508 |
Scaling at:#hecto put:100. "/ 10^2 |
|
509 |
Scaling at:#deca put:10. "/ 10^1 |
|
2320 | 510 |
Scaling at:#deka put:10. "/ 10^1 |
1323 | 511 |
Scaling at:#deci put:(1/10). "/ 10^-1 |
1135 | 512 |
Scaling at:#centi put:(1/100). |
1323 | 513 |
Scaling at:#milli put:(1/1000). "/ 10^-3 |
514 |
Scaling at:#micro put:(1/1000000). "/ 10^-6 |
|
515 |
Scaling at:#nano put:(1/1000000000). "/ 10^-9 |
|
516 |
Scaling at:#pico put:(1/1000000000000). "/ 10^-12 |
|
517 |
Scaling at:#femto put:(1/1000000000000000). "/ 10^-15 |
|
518 |
Scaling at:#atto put:(1/1000000000000000000). "/ 10^-18 |
|
519 |
Scaling at:#zepto put:(1/1000000000000000000000). "/ 10^-21 |
|
520 |
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
|
521 |
! ! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
522 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
523 |
!UnitConverter class methodsFor:'accessing'! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
524 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
525 |
scalings |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
526 |
"return the set of known scaling prefixes" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
527 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
528 |
^ Scaling keys |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
529 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
530 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
531 |
UnitConverter scalings |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
532 |
" |
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 |
"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
|
535 |
"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
|
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 |
units |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
539 |
"return the set of known units" |
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 |
|setOfUnits| |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
542 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
543 |
setOfUnits := IdentitySet new. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
544 |
Conversions keysAndValuesDo:[:srcUnit :conversionInfo | |
601 | 545 |
setOfUnits add:srcUnit. |
546 |
conversionInfo keysDo:[:targetUnit | |
|
547 |
setOfUnits add:targetUnit |
|
548 |
] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
549 |
]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
550 |
^ setOfUnits |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
551 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
552 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
553 |
UnitConverter units |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
554 |
" |
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 |
"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
|
557 |
"Modified: 6.8.1997 / 16:57:47 / cg" |
363 | 558 |
! ! |
559 |
||
560 |
!UnitConverter class methodsFor:'conversions'! |
|
561 |
||
562 |
convert:howMany from:sourceUnit to:destUnit |
|
563 |
"given a value in sourceUnit (symbolic), try to convert it |
|
564 |
to destUnit (symbolic); return nil, if the conversion is |
|
565 |
unknown." |
|
566 |
||
516 | 567 |
|u conversions alias rslt sU dU const val unit |
568 |
i suNumerator suDenominator duNumerator duDenominator uN uD |
|
569 |
sF1 sF2 dF1 dF2 |
|
570 |
s d| |
|
363 | 571 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
572 |
"/ Transcript showCR:('try ' , sourceUnit , '->' , destUnit). |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
573 |
|
550 | 574 |
Conversions isNil ifTrue:[ |
1031 | 575 |
self initializeConversions |
550 | 576 |
]. |
577 |
||
363 | 578 |
"/ somehow, recursion must end ... |
579 |
sourceUnit == destUnit ifTrue:[ |
|
1031 | 580 |
^ howMany |
363 | 581 |
]. |
582 |
||
369 | 583 |
sourceUnit isSymbol ifFalse:[ |
1031 | 584 |
s := sourceUnit withoutSeparators. |
585 |
sU := s asSymbolIfInterned. |
|
586 |
sU notNil ifTrue:[ |
|
587 |
^ self convert:howMany from:sU to:destUnit |
|
588 |
]. |
|
589 |
(s startsWith:$() ifTrue:[ |
|
590 |
(s endsWith:$)) ifTrue:[ |
|
591 |
s := s copyFrom:2 to:(s size - 1). |
|
592 |
^ self convert:howMany from:s to:destUnit |
|
593 |
] |
|
594 |
]. |
|
369 | 595 |
]. |
596 |
destUnit isSymbol ifFalse:[ |
|
1031 | 597 |
d := destUnit withoutSeparators. |
598 |
dU := d asSymbolIfInterned. |
|
599 |
dU notNil ifTrue:[ |
|
600 |
^ self convert:howMany from:sourceUnit to:dU |
|
601 |
]. |
|
602 |
(d startsWith:$() ifTrue:[ |
|
603 |
(d endsWith:$)) ifTrue:[ |
|
604 |
d := d copyFrom:2 to:(d size - 1). |
|
605 |
^ self convert:howMany from:sourceUnit to:d |
|
606 |
] |
|
607 |
]. |
|
369 | 608 |
]. |
609 |
||
363 | 610 |
"/ first, get rid of scalers ... |
611 |
||
612 |
u := self unscaled:sourceUnit. |
|
613 |
u notNil ifTrue:[ |
|
1031 | 614 |
^ self convert:(howMany*(u value)) from:(u key) to:destUnit |
363 | 615 |
]. |
616 |
||
617 |
u := self unscaled:destUnit. |
|
618 |
u notNil ifTrue:[ |
|
1031 | 619 |
^ self convert:(howMany/(u value)) from:sourceUnit to:(u key) |
363 | 620 |
]. |
621 |
||
622 |
"/ and of aliases ... |
|
623 |
||
624 |
alias := Aliases at:sourceUnit ifAbsent:nil. |
|
625 |
alias notNil ifTrue:[ |
|
1031 | 626 |
^ self convert:howMany from:alias to:destUnit |
363 | 627 |
]. |
628 |
alias := Aliases at:destUnit ifAbsent:nil. |
|
629 |
alias notNil ifTrue:[ |
|
1031 | 630 |
^ self convert:howMany from:sourceUnit to:alias |
363 | 631 |
]. |
632 |
||
516 | 633 |
"/ any constants ? |
634 |
||
635 |
(Constants includesKey:sourceUnit) ifTrue:[ |
|
1031 | 636 |
const := Constants at:sourceUnit. |
637 |
val := const at:1. |
|
638 |
unit := const at:2. |
|
639 |
^ self convert:(howMany*val) from:unit to:destUnit |
|
516 | 640 |
]. |
641 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
642 |
"/ compounds (^ , / or *) are very naively parsed |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
643 |
"/ 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
|
644 |
"/ 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
|
645 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
646 |
|
363 | 647 |
"/ working with squares or cubics ? |
648 |
||
516 | 649 |
((sourceUnit endsWith:'^2') and:[destUnit endsWith:'^2']) ifTrue:[ |
1031 | 650 |
sU := (sourceUnit copyWithoutLast:2) asSymbolIfInterned. |
651 |
dU := (destUnit copyWithoutLast:2) asSymbolIfInterned. |
|
652 |
(sU notNil and:[dU notNil]) ifTrue:[ |
|
653 |
^ (self convert:(howMany sqrt) from:sU to:dU) squared |
|
654 |
]. |
|
655 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
363 | 656 |
]. |
516 | 657 |
((sourceUnit endsWith:'^3') and:[destUnit endsWith:'^3']) ifTrue:[ |
1031 | 658 |
sU := (sourceUnit copyWithoutLast:2) asSymbolIfInterned. |
659 |
dU := (destUnit copyWithoutLast:2) asSymbolIfInterned. |
|
660 |
(sU notNil and:[dU notNil]) ifTrue:[ |
|
661 |
^ (self convert:(howMany raisedTo:(1/3)) from:sU to:dU) raisedTo:3 |
|
662 |
]. |
|
663 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
516 | 664 |
]. |
665 |
||
666 |
"/ working with fractions ? |
|
667 |
||
668 |
((sourceUnit includes:$/) and:[destUnit includes:$/]) ifTrue:[ |
|
1031 | 669 |
"/ look for a constant conversion factor |
670 |
i := sourceUnit indexOf:$/. |
|
671 |
suNumerator := sourceUnit copyTo:(i - 1). |
|
672 |
suDenominator := sourceUnit copyFrom:(i + 1). |
|
673 |
i := destUnit indexOf:$/. |
|
674 |
duNumerator := destUnit copyTo:(i - 1). |
|
675 |
duDenominator := destUnit copyFrom:(i + 1). |
|
516 | 676 |
|
1031 | 677 |
uN := self convert:howMany from:suNumerator to:duNumerator. |
678 |
uN notNil ifTrue:[ |
|
679 |
uD := self convert:1 from:suDenominator to:duDenominator. |
|
680 |
uD notNil ifTrue:[ |
|
681 |
^ uN / uD |
|
682 |
] |
|
683 |
]. |
|
516 | 684 |
]. |
685 |
||
686 |
"/ working with products ? |
|
687 |
||
688 |
((sourceUnit includes:$*) and:[destUnit includes:$*]) ifTrue:[ |
|
1031 | 689 |
i := sourceUnit indexOf:$*. |
690 |
sF1 := sourceUnit copyTo:(i - 1). |
|
691 |
sF2 := sourceUnit copyFrom:(i + 1). |
|
692 |
i := destUnit indexOf:$*. |
|
693 |
dF1 := destUnit copyTo:(i - 1). |
|
694 |
dF2 := destUnit copyFrom:(i + 1). |
|
516 | 695 |
|
1031 | 696 |
u := self convert:howMany from:sF1 to:dF1. |
697 |
u notNil ifTrue:[ |
|
698 |
u := self convert:u from:sF2 to:dF2. |
|
699 |
u notNil ifTrue:[ |
|
700 |
^ u |
|
701 |
] |
|
702 |
]. |
|
363 | 703 |
]. |
704 |
||
705 |
"/ the real work comes here ... |
|
706 |
||
707 |
"/ is there a direct conversion in the dataBase ? |
|
708 |
||
709 |
rslt := self convertDirect:howMany from:sourceUnit to:destUnit. |
|
710 |
rslt notNil ifTrue:[^ rslt]. |
|
711 |
||
712 |
"/ try inverse conversion ... |
|
713 |
||
714 |
rslt := self convertDirect:1 from:destUnit to:sourceUnit. |
|
715 |
rslt notNil ifTrue:[ ^ howMany / rslt]. |
|
716 |
||
516 | 717 |
"/ here's the deep recursion ... |
363 | 718 |
|
719 |
||
720 |
"/ try indirect conversion from source |
|
721 |
||
722 |
conversions := Conversions at:sourceUnit ifAbsent:nil. |
|
723 |
conversions notNil ifTrue:[ |
|
1031 | 724 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 | |
725 |
|factor2| |
|
363 | 726 |
|
1031 | 727 |
factor2 := self convert:factor1 from:intermediateUnit to:destUnit. |
728 |
factor2 notNil ifTrue:[^ factor2 * howMany]. |
|
729 |
]. |
|
363 | 730 |
]. |
731 |
||
732 |
"/ try indirect conversion from dest |
|
733 |
||
734 |
conversions := Conversions at:destUnit ifAbsent:nil. |
|
735 |
conversions notNil ifTrue:[ |
|
1031 | 736 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 | |
737 |
|factor2| |
|
363 | 738 |
|
1031 | 739 |
factor2 := self convert:(factor1) from:intermediateUnit to:sourceUnit. |
740 |
factor2 notNil ifTrue:[^ howMany / factor2]. |
|
741 |
]. |
|
363 | 742 |
]. |
743 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
744 |
"/ 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
|
745 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
746 |
(sourceUnit includes:$*) ifTrue:[ |
1031 | 747 |
i := sourceUnit indexOf:$*. |
748 |
sF1 := sourceUnit copyTo:(i - 1). |
|
749 |
sF2 := sourceUnit copyFrom:(i + 1). |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
750 |
|
1031 | 751 |
"/ see what we have ... |
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
752 |
|
1031 | 753 |
self units do:[:aUnit | |
754 |
|pref iUnit factor2 rslt| |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
755 |
|
1031 | 756 |
pref := sF1 , '*'. |
757 |
(aUnit startsWith:pref) ifTrue:[ |
|
758 |
"/ ok; want a/b -> x |
|
759 |
"/ found a/c -> any |
|
760 |
"/ what about c->b ? |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
761 |
|
1031 | 762 |
iUnit := aUnit copyFrom:pref size + 1. |
763 |
factor2 := self convert:1 from:sF2 to:iUnit. |
|
764 |
factor2 notNil ifTrue:[ |
|
765 |
"/ good ... |
|
766 |
rslt := self convert:(factor2 * howMany) |
|
767 |
from:aUnit to:destUnit. |
|
768 |
rslt notNil ifTrue:[^ rslt]. |
|
769 |
] |
|
770 |
] |
|
771 |
]. |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
772 |
]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
773 |
|
516 | 774 |
^ self noConversionFrom:sourceUnit to:destUnit. |
363 | 775 |
|
776 |
"direct - how many meters are there in two inches: |
|
777 |
UnitConverter convert:2 from:#inch to:#meter |
|
515 | 778 |
UnitConverter convert:37 from:#degrees to:#fahrenheit |
363 | 779 |
|
780 |
reverse - how many inches are there in one meter |
|
781 |
UnitConverter convert:1 from:#meter to:#inch |
|
782 |
||
783 |
with alias: |
|
784 |
UnitConverter convert:1 from:#inch to:#m |
|
785 |
UnitConverter convert:1 from:#inch to:#mm |
|
786 |
UnitConverter convert:1 from:#inch to:#millimeter |
|
787 |
UnitConverter convert:1 from:#inch to:#nanometer |
|
788 |
UnitConverter convert:1 from:#mm to:#km |
|
789 |
UnitConverter convert:1 from:#km to:#foot |
|
790 |
||
791 |
indirect: |
|
792 |
UnitConverter convert:1 from:#mm to:#twip |
|
793 |
UnitConverter convert:1 from:#inch to:#twip |
|
369 | 794 |
UnitConverter convert:1 from:'letterH' to:#point |
795 |
UnitConverter convert:1 from:'letterlH' to:#point |
|
364 | 796 |
|
797 |
UnitConverter convert:5 from:#barrel to:#liter |
|
798 |
UnitConverter convert:10 from:#kilogram to:#carat |
|
799 |
||
800 |
UnitConverter convert:1 from:#liter to:#floz |
|
363 | 801 |
" |
802 |
||
803 |
"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
|
804 |
"Modified: 6.8.1997 / 18:10:22 / cg" |
363 | 805 |
! |
806 |
||
1292 | 807 |
fileSizeFromString:fileSizeString |
808 |
"given a fileSize string (i.e. a number followed by optional Kb, Mb or Gb as required. |
|
809 |
Provided here since this is so common..." |
|
810 |
||
811 |
|numericValue s scaleChar scale| |
|
812 |
||
813 |
fileSizeString isEmpty ifTrue:[ |
|
814 |
^ 0 |
|
815 |
]. |
|
816 |
||
817 |
scale := 1. |
|
818 |
s := fileSizeString readStream. |
|
819 |
numericValue := Number readFrom:s onError:[0]. |
|
820 |
s skipSeparators. |
|
821 |
s atEnd ifFalse:[ |
|
822 |
scaleChar := s next asLowercase. |
|
823 |
scaleChar = $k ifTrue:[ |
|
824 |
scale := 1024 |
|
825 |
] ifFalse:[ |
|
826 |
scaleChar = $m ifTrue:[ |
|
827 |
scale := 1024 * 1024 |
|
828 |
] ifFalse:[ |
|
829 |
scaleChar = $g ifTrue:[ |
|
830 |
scale := 1024 * 1024 * 1024 |
|
831 |
] ifFalse:[ |
|
832 |
scaleChar = $t ifTrue:[ |
|
833 |
scale := 1024 * 1024 * 1024 * 1024 |
|
834 |
] |
|
835 |
] |
|
836 |
] |
|
837 |
]. |
|
838 |
]. |
|
839 |
^ (numericValue*scale) asInteger |
|
840 |
||
841 |
" |
|
842 |
self fileSizeFromString:'1234' |
|
1295 | 843 |
self fileSizeFromString:'10 k' |
844 |
self fileSizeFromString:'0.6 k' |
|
845 |
self fileSizeFromString:'1.8 M' |
|
846 |
self fileSizeFromString:'0.5 G' |
|
847 |
self fileSizeFromString:'0.5 T' |
|
1292 | 848 |
" |
849 |
! |
|
850 |
||
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
851 |
fileSizeStringFor:nBytes |
1293 | 852 |
"return a useful string for a size-in-bytes; |
1294 | 853 |
Scales by K (*1024), M (*1024*1024) or G (*1024*1024*1024) or even T |
854 |
as required and useful. |
|
1143 | 855 |
Provided here since this is so common..." |
856 |
||
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
857 |
|unitString n| |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
858 |
|
1293 | 859 |
nBytes < (1000 * 1024 * 1024 * 1024) ifTrue:[ |
860 |
nBytes < (1000 * 1024 * 1024) ifTrue:[ |
|
861 |
nBytes < (1000 * 1024) ifTrue:[ |
|
1457 | 862 |
nBytes < 1000 ifTrue:[ |
1293 | 863 |
n := nBytes. |
864 |
unitString := ' '. |
|
865 |
] ifFalse:[ |
|
866 |
n := (nBytes / 1024) asFixedPoint:1. |
|
867 |
unitString := ' K' |
|
868 |
] |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
869 |
] ifFalse:[ |
1293 | 870 |
n := (nBytes / (1024 * 1024)) asFixedPoint:1. |
871 |
unitString := ' M' |
|
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 * 1024)) asFixedPoint:1. |
875 |
unitString := ' G' |
|
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 * 1024)) asFixedPoint:1. |
879 |
unitString := ' T' |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
880 |
]. |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
881 |
|
1346 | 882 |
"/ n truncated = n ifTrue:[ |
883 |
"/ n := n truncated |
|
884 |
"/ ]. |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
885 |
^ n printString , unitString. |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
886 |
|
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
887 |
" |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
888 |
self fileSizeStringFor:10 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
889 |
self fileSizeStringFor:100 |
1298
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
890 |
self fileSizeStringFor:1000 |
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
891 |
self fileSizeStringFor:8192 |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
892 |
self fileSizeStringFor:10000 |
1298
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
893 |
self fileSizeStringFor:100000 |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
894 |
self fileSizeStringFor:1000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
895 |
self fileSizeStringFor:10000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
896 |
self fileSizeStringFor:100000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
897 |
self fileSizeStringFor:1000000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
898 |
self fileSizeStringFor:10000000000 |
1293 | 899 |
self fileSizeStringFor:100000000000 |
900 |
self fileSizeStringFor:1000000000000 |
|
901 |
self fileSizeStringFor:10000000000000 |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
902 |
" |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
903 |
! |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
904 |
|
363 | 905 |
inchToMillimeter:inches |
364 | 906 |
"convert inches to mm. Made this an extra method, since its so common." |
363 | 907 |
|
908 |
^ inches * 25.4 |
|
909 |
||
910 |
"Created: 31.5.1996 / 13:37:31 / cg" |
|
364 | 911 |
"Modified: 31.5.1996 / 18:08:14 / cg" |
363 | 912 |
! |
913 |
||
914 |
millimeterToInch:mm |
|
364 | 915 |
"convert mm to inches. Made this an extra method, since its so common." |
363 | 916 |
|
917 |
^ mm / 25.4 |
|
918 |
||
919 |
"Created: 31.5.1996 / 13:37:48 / cg" |
|
364 | 920 |
"Modified: 31.5.1996 / 18:08:20 / cg" |
363 | 921 |
! ! |
922 |
||
516 | 923 |
!UnitConverter class methodsFor:'missing conversion'! |
924 |
||
925 |
noConversionFrom:sourceUnit to:destUnit |
|
926 |
Transcript showCR:'no conversion ' , sourceUnit printString , ' -> ' , destUnit printString. |
|
927 |
^ nil |
|
928 |
||
929 |
"Created: 29.3.1997 / 17:56:03 / cg" |
|
930 |
! ! |
|
931 |
||
363 | 932 |
!UnitConverter class methodsFor:'private'! |
933 |
||
934 |
addConversion:factor from:sourceMetric to:destMetric |
|
935 |
"add a conversion" |
|
936 |
||
937 |
|conversion| |
|
938 |
||
939 |
conversion := Conversions at:sourceMetric ifAbsent:nil. |
|
940 |
conversion isNil ifTrue:[ |
|
601 | 941 |
conversion := IdentityDictionary new. |
942 |
Conversions at:sourceMetric put:conversion |
|
363 | 943 |
]. |
944 |
conversion at:destMetric put:factor. |
|
945 |
^ conversion |
|
946 |
||
947 |
"Created: 31.5.1996 / 13:51:25 / cg" |
|
515 | 948 |
"Modified: 29.3.1997 / 17:19:31 / cg" |
363 | 949 |
! |
950 |
||
951 |
convertDirect:howMany from:sourceMetric to:destMetric |
|
952 |
"given a value in sourceMetric (symbolic), try to convert it |
|
953 |
to destMetric (symbolic); |
|
954 |
Only direct conversions are tried; return nil, if the conversion is unknown." |
|
955 |
||
956 |
|conversion factor| |
|
957 |
||
550 | 958 |
Conversions isNil ifTrue:[ |
601 | 959 |
self initializeConversions |
550 | 960 |
]. |
961 |
||
363 | 962 |
conversion := Conversions at:sourceMetric ifAbsent:nil. |
963 |
conversion isNil ifTrue:[^ nil]. |
|
964 |
||
965 |
factor := conversion at:destMetric ifAbsent:nil. |
|
966 |
factor isNil ifTrue:[^ nil]. |
|
967 |
||
364 | 968 |
factor isNumber ifTrue:[ |
601 | 969 |
^ howMany * factor |
364 | 970 |
]. |
515 | 971 |
^ factor value:howMany |
363 | 972 |
|
973 |
" |
|
974 |
UnitConverter convertDirect:1 from:#inch to:#meter |
|
975 |
UnitConverter convertDirect:1 from:#inch to:#millimeter |
|
515 | 976 |
UnitConverter convertDirect:1 from:#degrees to:#fahrenheit |
363 | 977 |
" |
978 |
||
979 |
"Created: 31.5.1996 / 13:54:33 / cg" |
|
550 | 980 |
"Modified: 22.7.1997 / 13:57:52 / cg" |
363 | 981 |
! |
982 |
||
983 |
unscaled:what |
|
984 |
"given a unit, return the base and a factor as assoc, |
|
985 |
or nil if not found" |
|
986 |
||
550 | 987 |
Conversions isNil ifTrue:[ |
601 | 988 |
self initializeConversions |
550 | 989 |
]. |
990 |
||
363 | 991 |
Scaling keysAndValuesDo:[:name :factor | |
601 | 992 |
|rest| |
363 | 993 |
|
601 | 994 |
(what startsWith:name) ifTrue:[ |
995 |
rest := what copyFrom:(name size+1). |
|
996 |
rest := rest asSymbolIfInterned. |
|
997 |
rest notNil ifTrue:[ |
|
998 |
^ rest -> factor. |
|
999 |
] |
|
1000 |
]. |
|
363 | 1001 |
]. |
1002 |
^ nil |
|
1003 |
||
1004 |
" |
|
1005 |
UnitConverter unscaled:#millimeter |
|
1006 |
UnitConverter unscaled:#nanometer |
|
1007 |
UnitConverter unscaled:#kilometer |
|
1008 |
UnitConverter unscaled:#fuzzymeter |
|
1009 |
" |
|
1010 |
||
550 | 1011 |
"Modified: 22.7.1997 / 13:57:46 / cg" |
363 | 1012 |
! ! |
1013 |
||
1014 |
!UnitConverter class methodsFor:'documentation'! |
|
1015 |
||
1016 |
version |
|
2444
6c4c6addc97a
changed: #initializeConversions
Claus Gittinger <cg@exept.de>
parents:
2443
diff
changeset
|
1017 |
^ '$Header: /cvs/stx/stx/libbasic2/UnitConverter.st,v 1.38 2010-04-18 14:11:29 cg Exp $' |
2320 | 1018 |
! |
1019 |
||
1020 |
version_CVS |
|
2444
6c4c6addc97a
changed: #initializeConversions
Claus Gittinger <cg@exept.de>
parents:
2443
diff
changeset
|
1021 |
^ '$Header: /cvs/stx/stx/libbasic2/UnitConverter.st,v 1.38 2010-04-18 14:11:29 cg Exp $' |
363 | 1022 |
! ! |