author | Claus Gittinger <cg@exept.de> |
Mon, 18 Sep 2017 09:07:02 +0200 | |
changeset 4514 | 360a740542a7 |
parent 4092 | 61384ffc7271 |
child 4515 | 34fbe1d0b79f |
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 |
||
3486 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
363 | 16 |
Object subclass:#UnitConverter |
17 |
instanceVariableNames:'' |
|
516 | 18 |
classVariableNames:'Conversions Scaling Aliases Constants' |
363 | 19 |
poolDictionaries:'' |
20 |
category:'Magnitude-General' |
|
21 |
! |
|
22 |
||
23 |
!UnitConverter class methodsFor:'documentation'! |
|
24 |
||
884 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 1996 eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
||
38 |
||
39 |
! |
|
40 |
||
363 | 41 |
documentation |
42 |
" |
|
43 |
In order to collect various unit conversions into one central |
|
44 |
manageable (and especially: browsable) place, all previously |
|
564 | 45 |
spread knowledge about metric (and other) conversions has been |
363 | 46 |
brought into this class. |
47 |
||
48 |
This class is purely functional, abstract and has no instances; |
|
49 |
all queries are via class protocol. |
|
50 |
Choosing the Magnitudes category as its home is arbitrary. |
|
51 |
||
515 | 52 |
The supported units are setup in the classes initialize method; |
364 | 53 |
supported are: |
886 | 54 |
meter - inch - feet - foot - yard |
55 |
meter2 - acre - 'german-ar' 'german-hektar' are |
|
56 |
liter - gallon - barrel - floz |
|
57 |
celsius - fahrenheit |
|
364 | 58 |
and many others. |
59 |
||
515 | 60 |
The converter does a recursive search for a conversion; |
61 |
thus, if there is a conversion from #inch to #millimeter and another |
|
62 |
from #millimeter to #kilometer, conversion from #inch to #kilometer |
|
63 |
is found automatically. |
|
64 |
||
886 | 65 |
No Warranty: |
66 |
The numbers and conversion factors were obtained from the Unix |
|
67 |
units command. Please check before using it - there might be |
|
68 |
typos or wrong conversions in the setup code. |
|
69 |
||
70 |
||
1323 | 71 |
Notice: |
72 |
This class is *much* older than the younger and more object oriented |
|
73 |
Measurements package. Other than solving similar problems, these two packages |
|
74 |
are not related. |
|
75 |
||
76 |
||
363 | 77 |
[author:] |
886 | 78 |
Claus Gittinger |
363 | 79 |
|
80 |
see also: |
|
886 | 81 |
examples |
82 |
/usr/share/lib/unittab (if present on your system) |
|
1323 | 83 |
Measurement Unit |
363 | 84 |
" |
85 |
! |
|
86 |
||
87 |
examples |
|
88 |
" |
|
89 |
ever wanted to know, how many floz's are there in a european |
|
90 |
Coce bottle ? |
|
982 | 91 |
[exBegin] |
92 |
Transcript showCR: |
|
93 |
(UnitConverter convert:1 from:#liter to:#floz) |
|
94 |
[exEnd] |
|
95 |
||
96 |
[exBegin] |
|
97 |
Transcript showCR: |
|
98 |
(UnitConverter convert:43.5 from:#oz to:#gram) |
|
99 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
100 |
|
363 | 101 |
or, how many square-meters an acre is: |
982 | 102 |
[exBegin] |
103 |
Transcript showCR: |
|
104 |
(UnitConverter convert:1 from:#acre to:#'meter^2') |
|
105 |
[exEnd] |
|
363 | 106 |
|
516 | 107 |
|
515 | 108 |
or europeans might want to know, what those |
109 |
fahrenheit numbers mean in an US weather report ;-): |
|
982 | 110 |
[exBegin] |
111 |
Transcript showCR:( |
|
112 |
(UnitConverter convert:80 from:#fahrenheit to:#celsius) |
|
113 |
asFixedPoint:1) |
|
114 |
[exEnd] |
|
515 | 115 |
|
516 | 116 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
117 |
how fast do I drive ? :-) |
982 | 118 |
[exBegin] |
119 |
Transcript showCR:( |
|
120 |
UnitConverter convert:200 from:#'km/hr' to:#'mile/hr') |
|
121 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
122 |
how fast does Chris drive ? :-) |
982 | 123 |
[exBegin] |
124 |
Transcript showCR:( |
|
125 |
UnitConverter convert:65 from:#'mile/hr' to:#'km/hr') |
|
126 |
[exEnd] |
|
516 | 127 |
|
128 |
||
129 |
calories or joule ? |
|
982 | 130 |
[exBegin] |
131 |
Transcript showCR:( |
|
132 |
UnitConverter convert:0.18 from:#'kilocalorie' to:#'kilojoule') |
|
133 |
[exEnd] |
|
134 |
[exBegin] |
|
135 |
Transcript showCR:( |
|
136 |
UnitConverter convert:2000 from:#'kilocalorie' to:#'kilojoule') |
|
137 |
[exEnd] |
|
516 | 138 |
|
139 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
140 |
distances: |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
141 |
|
982 | 142 |
[exBegin] |
143 |
Transcript showCR:( |
|
144 |
UnitConverter convert:1 from:#'lightsecond' to:#'kilometer') |
|
145 |
[exEnd] |
|
4092 | 146 |
that's the same: |
982 | 147 |
[exBegin] |
148 |
Transcript showCR:( |
|
149 |
UnitConverter convert:1 from:#'lightspeed*s' to:#'kilometer') |
|
150 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
151 |
a days travel ... |
982 | 152 |
[exBegin] |
153 |
Transcript showCR:( |
|
154 |
UnitConverter convert:1 from:#'lightspeed*dy' to:#'kilometer') |
|
155 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
156 |
a year travel ... |
982 | 157 |
[exBegin] |
158 |
Transcript showCR:( |
|
159 |
UnitConverter convert:1 from:#'lightyear' to:#'kilometer') |
|
160 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
161 |
|
2320 | 162 |
wonna race on the autobahn ? |
163 |
[exBegin] |
|
164 |
Transcript showCR:( |
|
165 |
UnitConverter convert:200 from:#'km/h' to:#'mph') |
|
166 |
[exEnd] |
|
2547 | 167 |
[exBegin] |
168 |
Transcript showCR:( |
|
169 |
UnitConverter convert:13 from:#'m/s' to:#'km/h') |
|
170 |
[exEnd] |
|
2320 | 171 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
172 |
|
363 | 173 |
real estate buyers might want to know, how many acres |
174 |
a german ar is: |
|
982 | 175 |
[exBegin] |
176 |
Transcript showCR: |
|
177 |
(UnitConverter convert:1 from:#'german-ar' to:#acre) |
|
178 |
[exEnd] |
|
516 | 179 |
- or how many square-feet are there in your living room: |
982 | 180 |
[exBegin] |
181 |
Transcript showCR: |
|
182 |
(UnitConverter convert:100 from:#'meter^2' to:#'foot^2') |
|
183 |
[exEnd] |
|
1031 | 184 |
- or how about: |
185 |
[exBegin] |
|
186 |
Transcript showCR: |
|
187 |
(UnitConverter convert:3600 from:#'ft*ft' to:#'m*,') |
|
188 |
[exEnd] |
|
189 |
||
516 | 190 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
191 |
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
|
192 |
(roughly, since a teaspoon is not a standard unit) |
982 | 193 |
[exBegin] |
194 |
Transcript showCR: |
|
195 |
(UnitConverter convert:1 from:#'meter^3' to:#teaspoon) |
|
196 |
[exEnd] |
|
365 | 197 |
how wide is a US page in inches: |
982 | 198 |
[exBegin] |
199 |
Transcript showCR: |
|
200 |
(UnitConverter convert:1 from:#'letterW' to:#inch) |
|
201 |
[exEnd] |
|
516 | 202 |
- in millimeter: |
982 | 203 |
[exBegin] |
204 |
Transcript showCR: |
|
205 |
(UnitConverter convert:1 from:#'letterW' to:#mm) |
|
206 |
[exEnd] |
|
516 | 207 |
|
208 |
||
365 | 209 |
the height of a US page in inches: |
982 | 210 |
[exBegin] |
211 |
Transcript showCR: |
|
212 |
(UnitConverter convert:1 from:#'letterH' to:#inch) |
|
213 |
[exEnd] |
|
516 | 214 |
- in millimeter: |
982 | 215 |
[exBegin] |
216 |
Transcript showCR: |
|
217 |
(UnitConverter convert:1 from:#'letterH' to:#mm) |
|
218 |
[exEnd] |
|
516 | 219 |
|
220 |
||
365 | 221 |
the same for european A4 standard page: |
982 | 222 |
[exBegin] |
223 |
Transcript showCR: |
|
224 |
(UnitConverter convert:1 from:#'a4H' to:#mm) |
|
225 |
[exEnd] |
|
226 |
[exBegin] |
|
227 |
Transcript showCR: |
|
228 |
(UnitConverter convert:1 from:#'a4H' to:#inch) |
|
229 |
[exEnd] |
|
2443 | 230 |
|
231 |
the mass of a proton: |
|
232 |
[exBegin] |
|
233 |
Transcript showCR: |
|
234 |
(UnitConverter convert:1 from:#'proton' to:#eV) |
|
235 |
[exEnd] |
|
236 |
the energy of a single proton in the LHC (as of 2010) |
|
237 |
[exBegin] |
|
238 |
Transcript showCR: |
|
239 |
(UnitConverter convert:7 from:#'TeV' to:#joule) |
|
240 |
[exEnd] |
|
363 | 241 |
" |
242 |
! ! |
|
243 |
||
244 |
!UnitConverter class methodsFor:'initialization'! |
|
245 |
||
550 | 246 |
initializeConversions |
363 | 247 |
"initialize common conversions" |
248 |
||
1135 | 249 |
self initializeScaleFactors. |
250 |
||
363 | 251 |
Conversions := IdentityDictionary new. |
252 |
Aliases := IdentityDictionary new. |
|
516 | 253 |
Constants := IdentityDictionary new. |
363 | 254 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
255 |
"/ ---------- velocity ------------- |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
256 |
|
556 | 257 |
Constants at:#lightspeed put:#(2.997925E8 #'m/s'). |
2443 | 258 |
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
|
259 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
260 |
|
363 | 261 |
"/ -------------- length ------------- |
262 |
||
263 |
Aliases at:#km put:#kilometer. |
|
264 |
Aliases at:#m put:#meter. |
|
265 |
Aliases at:#dm put:#decimeter. |
|
266 |
Aliases at:#cm put:#centimeter. |
|
267 |
Aliases at:#mm put:#millimeter. |
|
268 |
Aliases at:#micron put:#micrometer. |
|
269 |
Aliases at:#nm put:#nanometer. |
|
270 |
Aliases at:#angstrom put:#decinanometer. |
|
271 |
||
272 |
"/ US |
|
273 |
self addConversion:12 from:#foot to:#inch. |
|
274 |
self addConversion:3 from:#yard to:#foot. |
|
275 |
self addConversion:5280 from:#mile to:#foot. |
|
515 | 276 |
self addConversion:0.001 from:#inch to:#mil. |
363 | 277 |
|
2320 | 278 |
Aliases at:#feet put:#foot. |
279 |
Aliases at:#ft put:#foot. |
|
280 |
Aliases at:#yd put:#yard. |
|
281 |
Aliases at:#mi put:#mile. |
|
282 |
Aliases at:#mph put:#'mile/hr'. |
|
363 | 283 |
|
284 |
"/ inch to millimeter |
|
285 |
self addConversion:(25.4/1000) from:#inch to:#meter. |
|
286 |
||
365 | 287 |
"/ nautic |
288 |
self addConversion:1852 from:#'nautical-mile' to:#meter. |
|
289 |
||
556 | 290 |
Aliases at:#lightsecond put:#'lightspeed*s'. |
291 |
Aliases at:#lightyear put:#'lightspeed*yr'. |
|
292 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
293 |
self addConversion:((Constants at:#lightspeed) at:1) from:#'lightspeed*s' to:#'meter'. |
385 | 294 |
|
516 | 295 |
"/ ---------- time ------------- |
296 |
||
297 |
self addConversion:60 from:#min to:#s. |
|
298 |
self addConversion:60 from:#hr to:#min. |
|
299 |
self addConversion:24 from:#dy to:#hr. |
|
300 |
self addConversion:365.24219879 from:#yr to:#dy. |
|
301 |
||
2320 | 302 |
Aliases at:#h put:#'hr'. |
303 |
Aliases at:#d put:#'dy'. |
|
516 | 304 |
|
385 | 305 |
"/ ---------- printing ------------- |
1135 | 306 |
self initializePrintValues. |
365 | 307 |
|
1135 | 308 |
"/ ---------------- area ------------------- |
309 |
||
310 |
"/ US |
|
311 |
self addConversion:4840 from:#acre to:#'yard^2'. |
|
312 |
||
313 |
self addConversion:100 from:#are to:#'meter^2'. |
|
314 |
self addConversion:100 from:#hectare to:#are. |
|
315 |
||
3701 | 316 |
"/ german area - add your countries, and return to me ... |
1135 | 317 |
Aliases at:#'german-ar' put:#are. |
318 |
Aliases at:#'german-hektar' put:#hectare. |
|
319 |
||
320 |
||
321 |
"/ ---------------- liquid ---------------- |
|
322 |
||
323 |
self addConversion:231 from:#gallon to:#'inch^3'. |
|
324 |
self addConversion:(1/4) from:#quart to:#gallon. "/ well - at least here, |
|
4092 | 325 |
"/ that's also 1/4th of a good wine ;-) |
1135 | 326 |
self addConversion:(1/2) from:#pint to:#quart. |
327 |
self addConversion:(1/16) from:#floz to:#pint. |
|
328 |
self addConversion:(1/8) from:#fldr to:#floz. |
|
329 |
||
330 |
self addConversion:42 from:#barrel to:#gallon. |
|
331 |
self addConversion:35.23907 from:#bushel to:#liter. |
|
332 |
||
333 |
Aliases at:#cc put:#'cm^3'. |
|
334 |
Aliases at:#liter put:#kilocc. |
|
335 |
Aliases at:#ml put:#milliliter. |
|
336 |
Aliases at:#gal put:#gallon. |
|
337 |
Aliases at:#qt put:#quart. |
|
338 |
||
2320 | 339 |
"/ well, a pint of beer is not always: |
340 |
self addConversion:277.420 from:#'british-gallon' to:#'inch^3'. |
|
341 |
self addConversion:(1/4) from:#'british-quart' to:#'british-gallon'. |
|
342 |
self addConversion:(1/2) from:#'british-pint' to:#'british-quart'. |
|
343 |
self addConversion:(1/16) from:#'british-floz' to:#'british-pint'. |
|
1135 | 344 |
|
345 |
"/ ---------------- mass ---------------- |
|
346 |
||
347 |
self addConversion:28.35 from:#ounce to:#gram. |
|
348 |
self addConversion:453.59237 from:#lb to:#gram. |
|
349 |
self addConversion:205 from:#carat to:#milligram. |
|
350 |
||
2444
6c4c6addc97a
changed: #initializeConversions
Claus Gittinger <cg@exept.de>
parents:
2443
diff
changeset
|
351 |
self addConversion:0.938272013 from:#proton to:#GeV. |
6c4c6addc97a
changed: #initializeConversions
Claus Gittinger <cg@exept.de>
parents:
2443
diff
changeset
|
352 |
self addConversion:0.510998910 from:#electron to:#MeV. |
6c4c6addc97a
changed: #initializeConversions
Claus Gittinger <cg@exept.de>
parents:
2443
diff
changeset
|
353 |
self addConversion:0.939565346 from:#neutron to:#GeV. |
2443 | 354 |
|
1135 | 355 |
Aliases at:#oz put:#ounce. |
356 |
Aliases at:#lbs put:#lb. |
|
357 |
||
358 |
Aliases at:#gm put:#gram. |
|
359 |
Aliases at:#kg put:#kilogram. |
|
360 |
||
361 |
"/ ---------------- energy ---------------- |
|
362 |
||
363 |
Aliases at:#cal put:#calorie. |
|
364 |
Aliases at:#nt put:#newton. |
|
365 |
Aliases at:#joule put:#'nt*m'. |
|
2320 | 366 |
Aliases at:#J put:#'joule'. |
367 |
Aliases at:#N put:#'newton'. |
|
2443 | 368 |
Aliases at:#eV put:#'electronvolt'. |
369 |
Aliases at:#MeV put:#'megaeV'. |
|
370 |
Aliases at:#GeV put:#'gigaeV'. |
|
371 |
Aliases at:#TeV put:#'teraeV'. |
|
2320 | 372 |
|
373 |
Aliases at:#watt put:#'J/s'. |
|
1135 | 374 |
|
375 |
self addConversion:4.1868 from:#calorie to:#joule. |
|
2443 | 376 |
self addConversion:1.60217653E-19 from:#electronvolt to:#joule. |
1135 | 377 |
|
378 |
||
379 |
"/ ---------------- cooking ---------------- |
|
380 |
||
381 |
self addConversion:4 from:#tablespoon to:#fldr. |
|
382 |
self addConversion:(4/3) from:#teaspoon to:#fldr. |
|
383 |
||
384 |
||
385 |
"/ ---------------- temperature ---------------- |
|
386 |
||
4514 | 387 |
self addConversion:[:d | d * 1.8 + 32] from:#celsius to:#fahrenheit. |
2443 | 388 |
self addConversion:[:f | (f - 32) / 1.8] from:#fahrenheit to:#celsius. |
4514 | 389 |
self addConversion:[:d | d + 273.15] from:#celsius to:#kelvin. |
390 |
self addConversion:[:k | k - 273.15] from:#kelvin to:#celsius. |
|
1135 | 391 |
|
3701 | 392 |
"/ ---------------- nature ---------------- |
393 |
Constants at:#planckLength put:#(1.616e-35 #'m'). |
|
394 |
Constants at:#planckMass put:#(2.176-8 #'Kg'). |
|
395 |
Constants at:#planckTime put:#(5.391e-44 #'s'). |
|
396 |
||
1135 | 397 |
" |
398 |
Conversions := nil. |
|
399 |
UnitConverter initializeConversions |
|
400 |
" |
|
401 |
||
4514 | 402 |
"Created: / 22-07-1997 / 13:56:40 / cg" |
403 |
"Modified: / 18-09-2017 / 08:38:14 / cg" |
|
1135 | 404 |
! |
405 |
||
406 |
initializePrintValues |
|
363 | 407 |
"/ inch to (roughly) a typesetter point |
408 |
self addConversion:(1/72) from:#point to:#inch. |
|
409 |
||
410 |
"/ point to twips; 20 twips (as in Rich-Text-Format) make a point |
|
411 |
self addConversion:(1/20) from:#twip to:#point. |
|
412 |
||
365 | 413 |
"/ US paper |
414 |
self addConversion:11 from:#'letter-page-height' to:#inch. |
|
415 |
self addConversion:8.5 from:#'letter-page-width' to:#inch. |
|
416 |
self addConversion:8.5 from:#'letter-landscape-page-height' to:#inch. |
|
417 |
self addConversion:11 from:#'letter-landscape-page-width' to:#inch. |
|
418 |
||
385 | 419 |
self addConversion:14 from:#'legal-page-height' to:#inch. |
420 |
self addConversion:8.5 from:#'legal-page-width' to:#inch. |
|
421 |
self addConversion:8.5 from:#'legal-landscape-page-height' to:#inch. |
|
422 |
self addConversion:14 from:#'legal-landscape-page-width' to:#inch. |
|
423 |
||
424 |
self addConversion:11 from:#'ledger-page-height' to:#inch. |
|
425 |
self addConversion:17 from:#'ledger-page-width' to:#inch. |
|
426 |
self addConversion:17 from:#'ledger-landscape-page-height' to:#inch. |
|
427 |
self addConversion:11 from:#'ledger-landscape-page-width' to:#inch. |
|
428 |
||
365 | 429 |
"/ European paper |
430 |
self addConversion:840 from:#'a1-page-height' to:#millimeter. |
|
431 |
self addConversion:592 from:#'a1-page-width' to:#millimeter. |
|
432 |
self addConversion:592 from:#'a1-landscape-page-height' to:#millimeter. |
|
433 |
self addConversion:840 from:#'a1-landscape-page-width' to:#millimeter. |
|
434 |
||
435 |
self addConversion:592 from:#'a2-page-height' to:#millimeter. |
|
436 |
self addConversion:420 from:#'a2-page-width' to:#millimeter. |
|
437 |
self addConversion:420 from:#'a2-landscape-page-height' to:#millimeter. |
|
438 |
self addConversion:592 from:#'a2-landscape-page-width' to:#millimeter. |
|
439 |
||
440 |
self addConversion:420 from:#'a3-page-height' to:#millimeter. |
|
441 |
self addConversion:296 from:#'a3-page-width' to:#millimeter. |
|
442 |
self addConversion:296 from:#'a3-landscape-page-height' to:#millimeter. |
|
443 |
self addConversion:420 from:#'a3-landscape-page-width' to:#millimeter. |
|
444 |
||
445 |
self addConversion:296 from:#'a4-page-height' to:#millimeter. |
|
446 |
self addConversion:210 from:#'a4-page-width' to:#millimeter. |
|
447 |
self addConversion:210 from:#'a4-landscape-page-height' to:#millimeter. |
|
448 |
self addConversion:296 from:#'a4-landscape-page-width' to:#millimeter. |
|
449 |
||
450 |
self addConversion:210 from:#'a5-page-height' to:#millimeter. |
|
451 |
self addConversion:148 from:#'a5-page-width' to:#millimeter. |
|
452 |
self addConversion:148 from:#'a5-landscape-page-height' to:#millimeter. |
|
453 |
self addConversion:210 from:#'a5-landscape-page-width' to:#millimeter. |
|
454 |
||
455 |
self addConversion:148 from:#'a6-page-height' to:#millimeter. |
|
456 |
self addConversion:105 from:#'a6-page-width' to:#millimeter. |
|
457 |
self addConversion:105 from:#'a6-landscape-page-height' to:#millimeter. |
|
458 |
self addConversion:148 from:#'a6-landscape-page-width' to:#millimeter. |
|
459 |
||
385 | 460 |
self addConversion:257 from:#'b5-page-height' to:#millimeter. |
461 |
self addConversion:182 from:#'b5-page-width' to:#millimeter. |
|
462 |
self addConversion:182 from:#'b5-landscape-page-height' to:#millimeter. |
|
463 |
self addConversion:257 from:#'b5-landscape-page-width' to:#millimeter. |
|
464 |
||
465 |
||
365 | 466 |
Aliases at:#letterW put:#'letter-page-width'. |
467 |
Aliases at:#letterH put:#'letter-page-height'. |
|
385 | 468 |
Aliases at:#legalW put:#'legal-page-width'. |
469 |
Aliases at:#legalH put:#'legal-page-height'. |
|
470 |
Aliases at:#ledgerW put:#'ledger-page-width'. |
|
471 |
Aliases at:#ledgerH put:#'ledger-page-height'. |
|
365 | 472 |
Aliases at:#a1W put:#'a1-page-width'. |
473 |
Aliases at:#a1H put:#'a1-page-height'. |
|
474 |
Aliases at:#a2W put:#'a2-page-width'. |
|
475 |
Aliases at:#a2H put:#'a2-page-height'. |
|
476 |
Aliases at:#a3W put:#'a3-page-width'. |
|
477 |
Aliases at:#a3H put:#'a3-page-height'. |
|
478 |
Aliases at:#a4W put:#'a4-page-width'. |
|
479 |
Aliases at:#a4H put:#'a4-page-height'. |
|
480 |
Aliases at:#a5W put:#'a5-page-width'. |
|
481 |
Aliases at:#a5H put:#'a5-page-height'. |
|
482 |
Aliases at:#a6W put:#'a6-page-width'. |
|
483 |
Aliases at:#a6H put:#'a6-page-height'. |
|
385 | 484 |
Aliases at:#b5W put:#'b5-page-width'. |
485 |
Aliases at:#b5H put:#'b5-page-height'. |
|
365 | 486 |
|
487 |
Aliases at:#letterlW put:#'letter-landscape-page-width'. |
|
488 |
Aliases at:#letterlH put:#'letter-landscape-page-height'. |
|
385 | 489 |
Aliases at:#legallW put:#'legal-landscape-page-width'. |
490 |
Aliases at:#legallH put:#'legal-landscape-page-height'. |
|
491 |
Aliases at:#ledgerlW put:#'ledger-landscape-page-width'. |
|
492 |
Aliases at:#ledgerlH put:#'ledger-landscape-page-height'. |
|
365 | 493 |
Aliases at:#a1lW put:#'a1-landscape-page-width'. |
494 |
Aliases at:#a1lH put:#'a1-landscape-page-height'. |
|
495 |
Aliases at:#a2lW put:#'a2-landscape-page-width'. |
|
496 |
Aliases at:#a2lH put:#'a2-landscape-page-height'. |
|
497 |
Aliases at:#a3lW put:#'a3-landscape-page-width'. |
|
498 |
Aliases at:#a3lH put:#'a3-landscape-page-height'. |
|
499 |
Aliases at:#a4lW put:#'a4-landscape-page-width'. |
|
500 |
Aliases at:#a4lH put:#'a4-landscape-page-height'. |
|
501 |
Aliases at:#a5lW put:#'a5-landscape-page-width'. |
|
502 |
Aliases at:#a5lH put:#'a5-landscape-page-height'. |
|
503 |
Aliases at:#a6lW put:#'a6-landscape-page-width'. |
|
504 |
Aliases at:#a6lH put:#'a6-landscape-page-height'. |
|
385 | 505 |
Aliases at:#b5lW put:#'b5-landscape-page-width'. |
506 |
Aliases at:#b5lH put:#'b5-landscape-page-height'. |
|
1135 | 507 |
! |
363 | 508 |
|
1135 | 509 |
initializeScaleFactors |
510 |
Scaling := IdentityDictionary new. |
|
363 | 511 |
|
1285 | 512 |
Scaling at:#yotta put:1000000000000000000000000. "/ 10^24 |
513 |
Scaling at:#zetta put:1000000000000000000000. "/ 10^21 |
|
514 |
Scaling at:#exa put:1000000000000000000. "/ 10^18 |
|
515 |
Scaling at:#peta put:1000000000000000. "/ 10^15 |
|
516 |
Scaling at:#tera put:1000000000000. "/ 10^12 |
|
517 |
Scaling at:#giga put:1000000000. "/ 10^9 |
|
518 |
Scaling at:#mega put:1000000. "/ 10^6 |
|
1135 | 519 |
Scaling at:#myria put:10000. |
1285 | 520 |
Scaling at:#kilo put:1000. "/ 10^3 |
521 |
Scaling at:#hecto put:100. "/ 10^2 |
|
522 |
Scaling at:#deca put:10. "/ 10^1 |
|
2320 | 523 |
Scaling at:#deka put:10. "/ 10^1 |
1323 | 524 |
Scaling at:#deci put:(1/10). "/ 10^-1 |
1135 | 525 |
Scaling at:#centi put:(1/100). |
1323 | 526 |
Scaling at:#milli put:(1/1000). "/ 10^-3 |
527 |
Scaling at:#micro put:(1/1000000). "/ 10^-6 |
|
528 |
Scaling at:#nano put:(1/1000000000). "/ 10^-9 |
|
529 |
Scaling at:#pico put:(1/1000000000000). "/ 10^-12 |
|
530 |
Scaling at:#femto put:(1/1000000000000000). "/ 10^-15 |
|
531 |
Scaling at:#atto put:(1/1000000000000000000). "/ 10^-18 |
|
532 |
Scaling at:#zepto put:(1/1000000000000000000000). "/ 10^-21 |
|
533 |
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
|
534 |
! ! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
535 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
536 |
!UnitConverter class methodsFor:'accessing'! |
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 |
scalings |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
539 |
"return the set of known scaling prefixes" |
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 |
^ Scaling keys |
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 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
544 |
UnitConverter scalings |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
545 |
" |
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 |
"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
|
548 |
"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
|
549 |
! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
550 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
551 |
units |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
552 |
"return the set of known units" |
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 |
setOfUnits := IdentitySet new. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
557 |
Conversions keysAndValuesDo:[:srcUnit :conversionInfo | |
601 | 558 |
setOfUnits add:srcUnit. |
559 |
conversionInfo keysDo:[:targetUnit | |
|
560 |
setOfUnits add:targetUnit |
|
561 |
] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
562 |
]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
563 |
^ setOfUnits |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
564 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
565 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
566 |
UnitConverter units |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
567 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
568 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
569 |
"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
|
570 |
"Modified: 6.8.1997 / 16:57:47 / cg" |
363 | 571 |
! ! |
572 |
||
573 |
!UnitConverter class methodsFor:'conversions'! |
|
574 |
||
575 |
convert:howMany from:sourceUnit to:destUnit |
|
576 |
"given a value in sourceUnit (symbolic), try to convert it |
|
577 |
to destUnit (symbolic); return nil, if the conversion is |
|
578 |
unknown." |
|
579 |
||
4514 | 580 |
^ self convert:howMany from:sourceUnit to:destUnit pairsAlreadyTried:(OrderedCollection new) |
581 |
||
582 |
"direct - how many meters are there in two inches: |
|
583 |
UnitConverter convert:2 from:#inch to:#meter |
|
584 |
||
585 |
reverse - how many inches are there in one meter |
|
586 |
UnitConverter convert:1 from:#meter to:#inch |
|
587 |
||
588 |
with alias: |
|
589 |
UnitConverter convert:1 from:#inch to:#m |
|
590 |
UnitConverter convert:1 from:#inch to:#mm |
|
591 |
UnitConverter convert:1 from:#inch to:#millimeter |
|
592 |
UnitConverter convert:1 from:#inch to:#nanometer |
|
593 |
UnitConverter convert:1 from:#mm to:#km |
|
594 |
UnitConverter convert:1 from:#km to:#foot |
|
595 |
||
596 |
indirect: |
|
597 |
UnitConverter convert:1 from:#mm to:#twip |
|
598 |
UnitConverter convert:1 from:#inch to:#twip |
|
599 |
UnitConverter convert:1 from:'letterH' to:#point |
|
600 |
UnitConverter convert:1 from:'letterlH' to:#point |
|
601 |
||
602 |
UnitConverter convert:5 from:#barrel to:#liter |
|
603 |
UnitConverter convert:10 from:#kilogram to:#carat |
|
604 |
||
605 |
UnitConverter convert:1 from:#liter to:#floz |
|
606 |
UnitConverter convert:1 from:#floz to:#liter |
|
607 |
UnitConverter convert:1 from:#oz to:#gram |
|
608 |
UnitConverter convert:1 from:#ounce to:#gram |
|
609 |
||
610 |
UnitConverter convert:37 from:#celsius to:#fahrenheit -> 98.6 |
|
611 |
UnitConverter convert:37 from:#celsius to:#kelvin -> 310.15 |
|
612 |
UnitConverter convert:0 from:#celsius to:#kelvin -> 273.15 |
|
613 |
UnitConverter convert:0 from:#kelvin to:#celsius -> -273.15 |
|
614 |
UnitConverter convert:0 from:#kelvin to:#fahrenheit -> -459.67 |
|
615 |
UnitConverter convert:0 from:#fahrenheit to:#celsius -> -17.7777777777778 |
|
616 |
UnitConverter convert:0 from:#fahrenheit to:#kelvin -> 255.372222222222 |
|
617 |
||
618 |
" |
|
619 |
||
620 |
"Created: / 31-05-1996 / 16:23:38 / cg" |
|
621 |
"Modified (comment): / 18-09-2017 / 09:02:09 / cg" |
|
622 |
! |
|
623 |
||
624 |
convert:howMany from:sourceUnit to:destUnit pairsAlreadyTried:pairsAlready |
|
625 |
"given a value in sourceUnit (symbolic), try to convert it |
|
626 |
to destUnit (symbolic); return nil, if the conversion is |
|
627 |
unknown." |
|
628 |
||
516 | 629 |
|u conversions alias rslt sU dU const val unit |
630 |
i suNumerator suDenominator duNumerator duDenominator uN uD |
|
631 |
sF1 sF2 dF1 dF2 |
|
632 |
s d| |
|
363 | 633 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
634 |
"/ Transcript showCR:('try ' , sourceUnit , '->' , destUnit). |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
635 |
|
550 | 636 |
Conversions isNil ifTrue:[ |
1031 | 637 |
self initializeConversions |
550 | 638 |
]. |
639 |
||
363 | 640 |
"/ somehow, recursion must end ... |
641 |
sourceUnit == destUnit ifTrue:[ |
|
1031 | 642 |
^ howMany |
363 | 643 |
]. |
4514 | 644 |
thisContext isReallyRecursive ifTrue:[ |
645 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
646 |
]. |
|
647 |
(pairsAlready includes:(sourceUnit->destUnit)) ifTrue:[ |
|
648 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
649 |
]. |
|
650 |
pairsAlready add:(sourceUnit->destUnit). |
|
651 |
||
369 | 652 |
sourceUnit isSymbol ifFalse:[ |
1031 | 653 |
s := sourceUnit withoutSeparators. |
654 |
sU := s asSymbolIfInterned. |
|
655 |
sU notNil ifTrue:[ |
|
4514 | 656 |
^ self convert:howMany from:sU to:destUnit pairsAlreadyTried:pairsAlready |
1031 | 657 |
]. |
658 |
(s startsWith:$() ifTrue:[ |
|
659 |
(s endsWith:$)) ifTrue:[ |
|
660 |
s := s copyFrom:2 to:(s size - 1). |
|
4514 | 661 |
^ self convert:howMany from:s to:destUnit pairsAlreadyTried:pairsAlready |
1031 | 662 |
] |
663 |
]. |
|
369 | 664 |
]. |
665 |
destUnit isSymbol ifFalse:[ |
|
1031 | 666 |
d := destUnit withoutSeparators. |
667 |
dU := d asSymbolIfInterned. |
|
668 |
dU notNil ifTrue:[ |
|
4514 | 669 |
^ self convert:howMany from:sourceUnit to:dU pairsAlreadyTried:pairsAlready |
1031 | 670 |
]. |
671 |
(d startsWith:$() ifTrue:[ |
|
672 |
(d endsWith:$)) ifTrue:[ |
|
673 |
d := d copyFrom:2 to:(d size - 1). |
|
4514 | 674 |
^ self convert:howMany from:sourceUnit to:d pairsAlreadyTried:pairsAlready |
1031 | 675 |
] |
676 |
]. |
|
369 | 677 |
]. |
678 |
||
363 | 679 |
"/ first, get rid of scalers ... |
680 |
||
681 |
u := self unscaled:sourceUnit. |
|
682 |
u notNil ifTrue:[ |
|
4514 | 683 |
^ self convert:(howMany*(u value)) from:(u key) to:destUnit pairsAlreadyTried:pairsAlready |
363 | 684 |
]. |
685 |
||
686 |
u := self unscaled:destUnit. |
|
687 |
u notNil ifTrue:[ |
|
4514 | 688 |
^ self convert:(howMany/(u value)) from:sourceUnit to:(u key) pairsAlreadyTried:pairsAlready |
363 | 689 |
]. |
690 |
||
691 |
"/ and of aliases ... |
|
692 |
||
693 |
alias := Aliases at:sourceUnit ifAbsent:nil. |
|
694 |
alias notNil ifTrue:[ |
|
4514 | 695 |
^ self convert:howMany from:alias to:destUnit pairsAlreadyTried:pairsAlready |
363 | 696 |
]. |
697 |
alias := Aliases at:destUnit ifAbsent:nil. |
|
698 |
alias notNil ifTrue:[ |
|
4514 | 699 |
^ self convert:howMany from:sourceUnit to:alias pairsAlreadyTried:pairsAlready |
363 | 700 |
]. |
701 |
||
516 | 702 |
"/ any constants ? |
703 |
||
704 |
(Constants includesKey:sourceUnit) ifTrue:[ |
|
1031 | 705 |
const := Constants at:sourceUnit. |
706 |
val := const at:1. |
|
707 |
unit := const at:2. |
|
4514 | 708 |
^ self convert:(howMany*val) from:unit to:destUnit pairsAlreadyTried:pairsAlready |
516 | 709 |
]. |
710 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
711 |
"/ compounds (^ , / or *) are very naively parsed |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
712 |
"/ 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
|
713 |
"/ 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
|
714 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
715 |
|
363 | 716 |
"/ working with squares or cubics ? |
717 |
||
516 | 718 |
((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
|
719 |
sU := (sourceUnit copyButLast:2) asSymbolIfInterned. |
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
720 |
dU := (destUnit copyButLast:2) asSymbolIfInterned. |
1031 | 721 |
(sU notNil and:[dU notNil]) ifTrue:[ |
4514 | 722 |
^ (self convert:(howMany sqrt) from:sU to:dU pairsAlreadyTried:pairsAlready) squared |
1031 | 723 |
]. |
724 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
363 | 725 |
]. |
516 | 726 |
((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
|
727 |
sU := (sourceUnit copyButLast:2) asSymbolIfInterned. |
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
728 |
dU := (destUnit copyButLast:2) asSymbolIfInterned. |
1031 | 729 |
(sU notNil and:[dU notNil]) ifTrue:[ |
4514 | 730 |
^ (self convert:(howMany raisedTo:(1/3)) from:sU to:dU pairsAlreadyTried:pairsAlready) raisedTo:3 |
1031 | 731 |
]. |
732 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
516 | 733 |
]. |
734 |
||
735 |
"/ working with fractions ? |
|
736 |
||
737 |
((sourceUnit includes:$/) and:[destUnit includes:$/]) ifTrue:[ |
|
1031 | 738 |
"/ look for a constant conversion factor |
739 |
i := sourceUnit indexOf:$/. |
|
740 |
suNumerator := sourceUnit copyTo:(i - 1). |
|
741 |
suDenominator := sourceUnit copyFrom:(i + 1). |
|
742 |
i := destUnit indexOf:$/. |
|
743 |
duNumerator := destUnit copyTo:(i - 1). |
|
744 |
duDenominator := destUnit copyFrom:(i + 1). |
|
516 | 745 |
|
4514 | 746 |
uN := self convert:howMany from:suNumerator to:duNumerator pairsAlreadyTried:pairsAlready. |
1031 | 747 |
uN notNil ifTrue:[ |
4514 | 748 |
uD := self convert:1 from:suDenominator to:duDenominator pairsAlreadyTried:pairsAlready. |
1031 | 749 |
uD notNil ifTrue:[ |
750 |
^ uN / uD |
|
751 |
] |
|
752 |
]. |
|
516 | 753 |
]. |
754 |
||
755 |
"/ working with products ? |
|
756 |
||
757 |
((sourceUnit includes:$*) and:[destUnit includes:$*]) ifTrue:[ |
|
1031 | 758 |
i := sourceUnit indexOf:$*. |
759 |
sF1 := sourceUnit copyTo:(i - 1). |
|
760 |
sF2 := sourceUnit copyFrom:(i + 1). |
|
761 |
i := destUnit indexOf:$*. |
|
762 |
dF1 := destUnit copyTo:(i - 1). |
|
763 |
dF2 := destUnit copyFrom:(i + 1). |
|
516 | 764 |
|
4514 | 765 |
u := self convert:howMany from:sF1 to:dF1 pairsAlreadyTried:pairsAlready. |
1031 | 766 |
u notNil ifTrue:[ |
4514 | 767 |
u := self convert:u from:sF2 to:dF2 pairsAlreadyTried:pairsAlready. |
1031 | 768 |
u notNil ifTrue:[ |
769 |
^ u |
|
770 |
] |
|
771 |
]. |
|
363 | 772 |
]. |
773 |
||
774 |
"/ the real work comes here ... |
|
775 |
||
776 |
"/ is there a direct conversion in the dataBase ? |
|
777 |
||
778 |
rslt := self convertDirect:howMany from:sourceUnit to:destUnit. |
|
779 |
rslt notNil ifTrue:[^ rslt]. |
|
780 |
||
781 |
"/ try inverse conversion ... |
|
4514 | 782 |
"/ but here, only linear factors (numeric conversions) are allowed!! |
783 |
[ |
|
784 |
|conversions revFactor| |
|
785 |
||
786 |
conversions := Conversions at:destUnit ifAbsent:nil. |
|
787 |
conversions notNil ifTrue:[ |
|
788 |
revFactor := conversions at:sourceUnit ifAbsent:nil. |
|
789 |
revFactor isNumber ifTrue:[ |
|
790 |
^ howMany / revFactor |
|
791 |
]. |
|
792 |
]. |
|
793 |
] value. |
|
363 | 794 |
|
4514 | 795 |
|
516 | 796 |
"/ here's the deep recursion ... |
363 | 797 |
|
798 |
"/ try indirect conversion from source |
|
799 |
conversions := Conversions at:sourceUnit ifAbsent:nil. |
|
800 |
conversions notNil ifTrue:[ |
|
1031 | 801 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 | |
4514 | 802 |
|factor2 intermediateValue result| |
363 | 803 |
|
4514 | 804 |
factor1 isBlock ifTrue:[ |
805 |
intermediateValue := factor1 value:howMany. |
|
806 |
result := self convert:intermediateValue from:intermediateUnit to:destUnit pairsAlreadyTried:pairsAlready. |
|
807 |
result notNil ifTrue:[ ^ result ]. |
|
808 |
] ifFalse:[ |
|
809 |
factor2 := self convert:factor1 from:intermediateUnit to:destUnit pairsAlreadyTried:pairsAlready. |
|
810 |
factor2 notNil ifTrue:[^ factor2 * howMany]. |
|
811 |
]. |
|
1031 | 812 |
]. |
363 | 813 |
]. |
814 |
||
815 |
"/ try indirect conversion from dest |
|
816 |
||
817 |
conversions := Conversions at:destUnit ifAbsent:nil. |
|
818 |
conversions notNil ifTrue:[ |
|
1031 | 819 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 | |
820 |
|factor2| |
|
363 | 821 |
|
4514 | 822 |
factor1 isBlock ifFalse:[ |
823 |
factor2 := self convert:(factor1) from:intermediateUnit to:sourceUnit pairsAlreadyTried:pairsAlready. |
|
824 |
factor2 notNil ifTrue:[^ howMany / factor2]. |
|
825 |
]. |
|
1031 | 826 |
]. |
363 | 827 |
]. |
828 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
829 |
"/ 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
|
830 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
831 |
(sourceUnit includes:$*) ifTrue:[ |
1031 | 832 |
i := sourceUnit indexOf:$*. |
833 |
sF1 := sourceUnit copyTo:(i - 1). |
|
834 |
sF2 := sourceUnit copyFrom:(i + 1). |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
835 |
|
1031 | 836 |
"/ see what we have ... |
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
837 |
|
1031 | 838 |
self units do:[:aUnit | |
839 |
|pref iUnit factor2 rslt| |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
840 |
|
1031 | 841 |
pref := sF1 , '*'. |
842 |
(aUnit startsWith:pref) ifTrue:[ |
|
843 |
"/ ok; want a/b -> x |
|
844 |
"/ found a/c -> any |
|
845 |
"/ what about c->b ? |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
846 |
|
1031 | 847 |
iUnit := aUnit copyFrom:pref size + 1. |
4514 | 848 |
factor2 := self convert:1 from:sF2 to:iUnit pairsAlreadyTried:pairsAlready. |
1031 | 849 |
factor2 notNil ifTrue:[ |
850 |
"/ good ... |
|
4514 | 851 |
rslt := self convert:(factor2 * howMany) from:aUnit to:destUnit pairsAlreadyTried:pairsAlready. |
1031 | 852 |
rslt notNil ifTrue:[^ rslt]. |
853 |
] |
|
854 |
] |
|
855 |
]. |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
856 |
]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
857 |
|
516 | 858 |
^ self noConversionFrom:sourceUnit to:destUnit. |
363 | 859 |
|
860 |
"direct - how many meters are there in two inches: |
|
861 |
UnitConverter convert:2 from:#inch to:#meter |
|
515 | 862 |
UnitConverter convert:37 from:#degrees to:#fahrenheit |
363 | 863 |
|
864 |
reverse - how many inches are there in one meter |
|
865 |
UnitConverter convert:1 from:#meter to:#inch |
|
866 |
||
867 |
with alias: |
|
868 |
UnitConverter convert:1 from:#inch to:#m |
|
869 |
UnitConverter convert:1 from:#inch to:#mm |
|
870 |
UnitConverter convert:1 from:#inch to:#millimeter |
|
871 |
UnitConverter convert:1 from:#inch to:#nanometer |
|
872 |
UnitConverter convert:1 from:#mm to:#km |
|
873 |
UnitConverter convert:1 from:#km to:#foot |
|
874 |
||
875 |
indirect: |
|
876 |
UnitConverter convert:1 from:#mm to:#twip |
|
877 |
UnitConverter convert:1 from:#inch to:#twip |
|
369 | 878 |
UnitConverter convert:1 from:'letterH' to:#point |
879 |
UnitConverter convert:1 from:'letterlH' to:#point |
|
364 | 880 |
|
881 |
UnitConverter convert:5 from:#barrel to:#liter |
|
882 |
UnitConverter convert:10 from:#kilogram to:#carat |
|
883 |
||
884 |
UnitConverter convert:1 from:#liter to:#floz |
|
4514 | 885 |
UnitConverter convert:0 from:#fahrenheit to:#kelvin |
886 |
UnitConverter convert:0 from:#fahrenheit to:#celsius |
|
887 |
UnitConverter convert:0 from:#celsius to:#kelvin |
|
888 |
UnitConverter convert:0 from:#kelvin to:#celsius |
|
363 | 889 |
" |
890 |
||
4514 | 891 |
"Created: / 17-09-2017 / 10:11:40 / cg" |
892 |
"Modified: / 18-09-2017 / 08:39:15 / cg" |
|
363 | 893 |
! |
894 |
||
3573 | 895 |
fileSizeBinaryStringFor:nBytes |
3575 | 896 |
"return a useful string for a size-in-bytes in programmer's notation; |
3573 | 897 |
Scales by Ki (*1024), Mi (*1024*1024) or Gi (*1024*1024*1024) or even Ti |
898 |
as required and useful. |
|
899 |
Provided here since this is so common... |
|
900 |
Notice: the scale is 1024 |
|
901 |
the 1024 scale has been standardized in 1998 by IEC to MiB, GiB, etc. |
|
902 |
See https://en.wikipedia.org/wiki/Mebibyte" |
|
903 |
||
904 |
^ self |
|
905 |
unitStringFor:nBytes |
|
3574 | 906 |
scale:1024 |
3575 | 907 |
rounded:true |
3576 | 908 |
unitStrings:#('Byte' 'KiB' 'MiB' 'GiB' 'TiB' 'PiB' 'EiB' 'ZiB' 'YiB') |
3573 | 909 |
|
910 |
"/ the one below was written to save divisions. |
|
911 |
"/ It seems to be not worth the effort... |
|
912 |
||
913 |
"/ |unitString n| |
|
914 |
"/ |
|
915 |
"/ nBytes isNil ifTrue:[ |
|
916 |
"/ ^ '-' |
|
917 |
"/ ]. |
|
918 |
"/ |
|
919 |
"/ nBytes < (1000 * 1024 * 1024 * 1024) ifTrue:[ |
|
920 |
"/ nBytes < (1000 * 1024 * 1024) ifTrue:[ |
|
921 |
"/ nBytes < (1000 * 1024) ifTrue:[ |
|
922 |
"/ nBytes < 1000 ifTrue:[ |
|
923 |
"/ n := nBytes. |
|
924 |
"/ unitString := ' '. |
|
925 |
"/ ] ifFalse:[ |
|
926 |
"/ n := (nBytes / 1024) asFixedPoint:1. |
|
927 |
"/ unitString := ' K' |
|
928 |
"/ ] |
|
929 |
"/ ] ifFalse:[ |
|
930 |
"/ n := (nBytes / (1024 * 1024)) asFixedPoint:1. |
|
931 |
"/ unitString := ' M' |
|
932 |
"/ ]. |
|
933 |
"/ ] ifFalse:[ |
|
934 |
"/ n := (nBytes / 1024 / (1024 * 1024)) asFixedPoint:1. |
|
935 |
"/ unitString := ' G' |
|
936 |
"/ ] |
|
937 |
"/ ] ifFalse:[ |
|
938 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
939 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
940 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
941 |
"/ unitString := ' E' |
|
942 |
"/ ] ifFalse:[ |
|
943 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / 1024) asFixedPoint:1. |
|
944 |
"/ unitString := ' P' |
|
945 |
"/ ] |
|
946 |
"/ ] ifFalse:[ |
|
947 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
948 |
"/ unitString := ' T' |
|
949 |
"/ ]. |
|
950 |
"/ ]. |
|
951 |
"/ |
|
952 |
"/"/ n truncated = n ifTrue:[ |
|
953 |
"/"/ n := n truncated |
|
954 |
"/"/ ]. |
|
955 |
"/ ^ n printString , unitString. |
|
956 |
||
957 |
" |
|
958 |
self fileSizeStringFor:nil |
|
959 |
self fileSizeStringFor:10 |
|
960 |
self fileSizeStringFor:100 |
|
961 |
self fileSizeStringFor:1000 |
|
962 |
self fileSizeStringFor:8192 |
|
963 |
self fileSizeStringFor:10000 |
|
964 |
self fileSizeStringFor:100000 |
|
965 |
self fileSizeStringFor:1000000 |
|
966 |
self fileSizeStringFor:10000000 |
|
967 |
self fileSizeStringFor:100000000 |
|
968 |
self fileSizeStringFor:1000000000 |
|
969 |
self fileSizeStringFor:10000000000 |
|
970 |
self fileSizeStringFor:100000000000 |
|
971 |
self fileSizeStringFor:1000000000000 |
|
972 |
self fileSizeStringFor:10000000000000 |
|
973 |
self fileSizeStringFor:10000000000000000 |
|
974 |
self fileSizeStringFor:100000000000000000000 |
|
975 |
self fileSizeStringFor:1000000000000000000000 |
|
976 |
" |
|
977 |
||
978 |
"Modified: / 17-12-2013 / 16:55:17 / cg" |
|
979 |
"Modified (comment): / 18-12-2014 / 09:54:33 / sr" |
|
980 |
! |
|
981 |
||
1292 | 982 |
fileSizeFromString:fileSizeString |
3573 | 983 |
"given a fileSize string (i.e. a number followed by optional kB, MB, GB etc. or KiB, MiB, GiB,...). |
984 |
Notice that in 1998, IEC standardized the scales to 1000 for k,M,G,... and 1024 for Ki, Mi, Gi, etc. |
|
985 |
See https://en.wikipedia.org/wiki/Mebibyte. |
|
1292 | 986 |
Provided here since this is so common..." |
987 |
||
3573 | 988 |
|numericValue s scaleChar factor scale| |
1292 | 989 |
|
990 |
fileSizeString isEmpty ifTrue:[ |
|
991 |
^ 0 |
|
992 |
]. |
|
993 |
||
994 |
scale := 1. |
|
995 |
s := fileSizeString readStream. |
|
996 |
numericValue := Number readFrom:s onError:[0]. |
|
997 |
s skipSeparators. |
|
998 |
s atEnd ifFalse:[ |
|
999 |
scaleChar := s next asLowercase. |
|
3573 | 1000 |
factor := 1000. |
1001 |
s peek == $i ifTrue:[ |
|
1002 |
factor := 1024 |
|
1003 |
]. |
|
3486 | 1004 |
scaleChar == $k ifTrue:[ |
3573 | 1005 |
scale := factor |
1292 | 1006 |
] ifFalse:[ |
3486 | 1007 |
scaleChar == $m ifTrue:[ |
3573 | 1008 |
scale := factor * factor |
1292 | 1009 |
] ifFalse:[ |
3486 | 1010 |
scaleChar == $g ifTrue:[ |
3573 | 1011 |
scale := factor * factor * factor |
1292 | 1012 |
] ifFalse:[ |
3486 | 1013 |
scaleChar == $t ifTrue:[ |
3573 | 1014 |
scale := factor * factor * factor * factor |
3142 | 1015 |
] ifFalse:[ |
3486 | 1016 |
scaleChar == $p ifTrue:[ |
3573 | 1017 |
scale := factor * factor * factor * factor * factor |
3142 | 1018 |
] ifFalse:[ |
3486 | 1019 |
scaleChar == $e ifTrue:[ |
3573 | 1020 |
scale := factor * factor * factor * factor * factor * factor |
1021 |
] ifFalse:[ |
|
1022 |
scaleChar == $z ifTrue:[ |
|
1023 |
scale := factor * factor * factor * factor * factor * factor * factor |
|
1024 |
] ifFalse:[ |
|
1025 |
scaleChar == $y ifTrue:[ |
|
1026 |
scale := factor * factor * factor * factor * factor * factor * factor * factor |
|
1027 |
] |
|
1028 |
] |
|
3142 | 1029 |
] |
1030 |
] |
|
1292 | 1031 |
] |
1032 |
] |
|
1033 |
] |
|
1034 |
]. |
|
1035 |
]. |
|
1036 |
^ (numericValue*scale) asInteger |
|
1037 |
||
1038 |
" |
|
1039 |
self fileSizeFromString:'1234' |
|
1295 | 1040 |
self fileSizeFromString:'10 k' |
3573 | 1041 |
self fileSizeFromString:'10 kB' |
1042 |
self fileSizeFromString:'10 KiB' |
|
1295 | 1043 |
self fileSizeFromString:'0.6 k' |
1044 |
self fileSizeFromString:'1.8 M' |
|
3573 | 1045 |
self fileSizeFromString:'1.8 MB' |
1046 |
self fileSizeFromString:'1.8 MiB' |
|
1295 | 1047 |
self fileSizeFromString:'0.5 G' |
1048 |
self fileSizeFromString:'0.5 T' |
|
3142 | 1049 |
self fileSizeFromString:'0.5 P' |
1050 |
self fileSizeFromString:'0.5 E' |
|
1292 | 1051 |
" |
3142 | 1052 |
|
1053 |
"Modified (comment): / 17-12-2013 / 16:56:56 / cg" |
|
1292 | 1054 |
! |
1055 |
||
3575 | 1056 |
fileSizeSIStringFor:nBytes |
1057 |
"return a useful string for a size-in-bytes in SI standard notation; |
|
3573 | 1058 |
Scales by K (*1000), M (*1000*1000) or G (*1000*1000*1000) or even T |
1294 | 1059 |
as required and useful. |
3573 | 1060 |
Provided here since this is so common... |
1061 |
Notice: the scale is 1000 |
|
1062 |
the 1000 scale has been standardized in 1998 by IEC to MB, GB, etc. |
|
1063 |
See https://en.wikipedia.org/wiki/Mebibyte" |
|
1143 | 1064 |
|
3573 | 1065 |
^ self |
1066 |
unitStringFor:nBytes |
|
1067 |
scale:1000 |
|
3575 | 1068 |
rounded:true |
3576 | 1069 |
unitStrings:#('Byte' 'KB' 'MB' 'GB' 'TB' 'PB' 'EB' 'ZB' 'YB') |
3575 | 1070 |
|
1071 |
"/ |unitString n| |
|
1072 |
"/ |
|
1073 |
"/ nBytes isNil ifTrue:[ |
|
1074 |
"/ ^ '-' |
|
1075 |
"/ ]. |
|
1076 |
"/ |
|
1077 |
"/ nBytes < (1000 * 1024 * 1024 * 1024) ifTrue:[ |
|
1078 |
"/ nBytes < (1000 * 1024 * 1024) ifTrue:[ |
|
1079 |
"/ nBytes < (1000 * 1024) ifTrue:[ |
|
1080 |
"/ nBytes < 1000 ifTrue:[ |
|
1081 |
"/ n := nBytes. |
|
1082 |
"/ unitString := ' '. |
|
1083 |
"/ ] ifFalse:[ |
|
1084 |
"/ n := (nBytes / 1024) asFixedPoint:1. |
|
1085 |
"/ unitString := ' K' |
|
1086 |
"/ ] |
|
1087 |
"/ ] ifFalse:[ |
|
1088 |
"/ n := (nBytes / (1024 * 1024)) asFixedPoint:1. |
|
1089 |
"/ unitString := ' M' |
|
1090 |
"/ ]. |
|
1091 |
"/ ] ifFalse:[ |
|
1092 |
"/ n := (nBytes / 1024 / (1024 * 1024)) asFixedPoint:1. |
|
1093 |
"/ unitString := ' G' |
|
1094 |
"/ ] |
|
1095 |
"/ ] ifFalse:[ |
|
1096 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
1097 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
1098 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
1099 |
"/ unitString := ' E' |
|
1100 |
"/ ] ifFalse:[ |
|
1101 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / 1024) asFixedPoint:1. |
|
1102 |
"/ unitString := ' P' |
|
1103 |
"/ ] |
|
1104 |
"/ ] ifFalse:[ |
|
1105 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
1106 |
"/ unitString := ' T' |
|
1107 |
"/ ]. |
|
1108 |
"/ ]. |
|
1109 |
"/ |
|
1110 |
"/"/ n truncated = n ifTrue:[ |
|
1111 |
"/"/ n := n truncated |
|
1112 |
"/"/ ]. |
|
1113 |
"/ ^ n printString , unitString. |
|
1114 |
||
1115 |
" |
|
1116 |
self fileSizeStringFor:nil |
|
1117 |
self fileSizeStringFor:10 |
|
1118 |
self fileSizeStringFor:100 |
|
1119 |
self fileSizeStringFor:1000 |
|
1120 |
self fileSizeStringFor:8192 |
|
1121 |
self fileSizeStringFor:10000 |
|
1122 |
self fileSizeStringFor:100000 |
|
1123 |
self fileSizeStringFor:1000000 |
|
1124 |
self fileSizeStringFor:10000000 |
|
1125 |
self fileSizeStringFor:100000000 |
|
1126 |
self fileSizeStringFor:1000000000 |
|
1127 |
self fileSizeStringFor:10000000000 |
|
1128 |
self fileSizeStringFor:100000000000 |
|
1129 |
self fileSizeStringFor:1000000000000 |
|
1130 |
self fileSizeStringFor:10000000000000 |
|
1131 |
self fileSizeStringFor:10000000000000000 |
|
1132 |
self fileSizeStringFor:100000000000000000000 |
|
1133 |
self fileSizeStringFor:1000000000000000000000 |
|
1134 |
" |
|
1135 |
||
1136 |
"Modified: / 17-12-2013 / 16:55:17 / cg" |
|
1137 |
"Modified (comment): / 18-12-2014 / 09:54:33 / sr" |
|
1138 |
! |
|
1139 |
||
1140 |
fileSizeStringFor:nBytes |
|
1141 |
"return a useful string for a size-in-bytes. |
|
1142 |
Scales either by 1000 or 1024, depending on the user preferences. |
|
1143 |
See https://en.wikipedia.org/wiki/Mebibyte" |
|
1144 |
||
1145 |
^ self |
|
1146 |
unitStringFor:nBytes |
|
1147 |
scale:(UserPreferences current unitForFileSize) |
|
1148 |
rounded:true |
|
1149 |
unitStrings:(UserPreferences current unitStringsForFileSize) |
|
3457 | 1150 |
|
3573 | 1151 |
"/ |unitString n| |
1152 |
"/ |
|
1153 |
"/ nBytes isNil ifTrue:[ |
|
1154 |
"/ ^ '-' |
|
1346 | 1155 |
"/ ]. |
3573 | 1156 |
"/ |
1157 |
"/ nBytes < (1000 * 1024 * 1024 * 1024) ifTrue:[ |
|
1158 |
"/ nBytes < (1000 * 1024 * 1024) ifTrue:[ |
|
1159 |
"/ nBytes < (1000 * 1024) ifTrue:[ |
|
1160 |
"/ nBytes < 1000 ifTrue:[ |
|
1161 |
"/ n := nBytes. |
|
1162 |
"/ unitString := ' '. |
|
1163 |
"/ ] ifFalse:[ |
|
1164 |
"/ n := (nBytes / 1024) asFixedPoint:1. |
|
1165 |
"/ unitString := ' K' |
|
1166 |
"/ ] |
|
1167 |
"/ ] ifFalse:[ |
|
1168 |
"/ n := (nBytes / (1024 * 1024)) asFixedPoint:1. |
|
1169 |
"/ unitString := ' M' |
|
1170 |
"/ ]. |
|
1171 |
"/ ] ifFalse:[ |
|
1172 |
"/ n := (nBytes / 1024 / (1024 * 1024)) asFixedPoint:1. |
|
1173 |
"/ unitString := ' G' |
|
1174 |
"/ ] |
|
1175 |
"/ ] ifFalse:[ |
|
1176 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
1177 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
1178 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
1179 |
"/ unitString := ' E' |
|
1180 |
"/ ] ifFalse:[ |
|
1181 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / 1024) asFixedPoint:1. |
|
1182 |
"/ unitString := ' P' |
|
1183 |
"/ ] |
|
1184 |
"/ ] ifFalse:[ |
|
1185 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
1186 |
"/ unitString := ' T' |
|
1187 |
"/ ]. |
|
1188 |
"/ ]. |
|
1189 |
"/ |
|
1190 |
"/"/ n truncated = n ifTrue:[ |
|
1191 |
"/"/ n := n truncated |
|
1192 |
"/"/ ]. |
|
1193 |
"/ ^ n printString , unitString. |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1194 |
|
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1195 |
" |
3457 | 1196 |
self fileSizeStringFor:nil |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1197 |
self fileSizeStringFor:10 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1198 |
self fileSizeStringFor:100 |
1298
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1199 |
self fileSizeStringFor:1000 |
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1200 |
self fileSizeStringFor:8192 |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1201 |
self fileSizeStringFor:10000 |
1298
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1202 |
self fileSizeStringFor:100000 |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1203 |
self fileSizeStringFor:1000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1204 |
self fileSizeStringFor:10000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1205 |
self fileSizeStringFor:100000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1206 |
self fileSizeStringFor:1000000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1207 |
self fileSizeStringFor:10000000000 |
1293 | 1208 |
self fileSizeStringFor:100000000000 |
1209 |
self fileSizeStringFor:1000000000000 |
|
1210 |
self fileSizeStringFor:10000000000000 |
|
3141 | 1211 |
self fileSizeStringFor:10000000000000000 |
3142 | 1212 |
self fileSizeStringFor:100000000000000000000 |
1213 |
self fileSizeStringFor:1000000000000000000000 |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1214 |
" |
3141 | 1215 |
|
3142 | 1216 |
"Modified: / 17-12-2013 / 16:55:17 / cg" |
3457 | 1217 |
"Modified (comment): / 18-12-2014 / 09:54:33 / sr" |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1218 |
! |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1219 |
|
3120 | 1220 |
frequencyFromString:frequencyString |
1221 |
"given a frequency string (i.e. a number followed by optional kHz, Mhz or Hhz as required. |
|
1222 |
Provided here since this is so common..." |
|
1223 |
||
1224 |
|numericValue s scaleChar scale rest| |
|
1225 |
||
1226 |
frequencyString isEmpty ifTrue:[ |
|
1227 |
^ 0 |
|
1228 |
]. |
|
1229 |
||
1230 |
scale := 1. |
|
1231 |
s := frequencyString readStream. |
|
1232 |
numericValue := Number readFrom:s onError:[0]. |
|
1233 |
s skipSeparators. |
|
1234 |
s atEnd ifFalse:[ |
|
1235 |
scaleChar := s next asLowercase. |
|
3486 | 1236 |
scaleChar == $/ ifTrue:[ |
3120 | 1237 |
rest := s upToSeparator asLowercase. |
1238 |
(rest = 'm' or:[rest = 'min']) ifTrue:[ |
|
1239 |
scale := 1/60 |
|
1240 |
] ifFalse:[ |
|
1241 |
(rest = 'h' or:[rest = 'hr' or:[rest = 'hour']]) ifTrue:[ |
|
1242 |
scale := 1/60/60 |
|
1243 |
] ifFalse:[ |
|
1244 |
(rest = 'd' or:[rest = 'dy' or:[rest = 'day']]) ifTrue:[ |
|
1245 |
scale := 1/60/60/24 |
|
1246 |
]. |
|
1247 |
]. |
|
1248 |
]. |
|
1249 |
] ifFalse:[ |
|
3486 | 1250 |
scaleChar == $k ifTrue:[ |
3120 | 1251 |
scale := 1000 |
1252 |
] ifFalse:[ |
|
3486 | 1253 |
scaleChar == $m ifTrue:[ |
3120 | 1254 |
scale := 1000 * 1000 |
1255 |
] ifFalse:[ |
|
3487 | 1256 |
scaleChar == $g ifTrue:[ |
3120 | 1257 |
scale := 1000 * 1000 * 1000 |
1258 |
] ifFalse:[ |
|
3487 | 1259 |
scaleChar == $t ifTrue:[ |
3120 | 1260 |
scale := 1000 * 1000 * 1000 * 1000 |
1261 |
] |
|
1262 |
] |
|
1263 |
] |
|
1264 |
]. |
|
1265 |
] |
|
1266 |
]. |
|
1267 |
^ (numericValue*scale) |
|
1268 |
||
1269 |
" |
|
1270 |
self frequencyFromString:'1234' |
|
1271 |
self frequencyFromString:'10 kHz' |
|
1272 |
self frequencyFromString:'0.6 k' |
|
1273 |
self frequencyFromString:'1.8 Mhz' |
|
1274 |
self frequencyFromString:'0.5 Ghz' |
|
1275 |
self frequencyFromString:'0.5 Thz' |
|
3577 | 1276 |
self frequencyFromString:'1000/s' |
3120 | 1277 |
self frequencyFromString:'10/s' |
1278 |
self frequencyFromString:'10/min' |
|
1279 |
self frequencyFromString:'10/hr' |
|
1280 |
" |
|
1281 |
! |
|
1282 |
||
1283 |
frequencyStringFor:hertz |
|
1284 |
"return a useful string for a frequency; |
|
1285 |
Scales by K (*1000), M (*1000*1000) or G (*1000*1000*1000) or even T |
|
1286 |
as required and useful. |
|
1287 |
Provided here since this is so common..." |
|
1288 |
||
3573 | 1289 |
^ self |
1290 |
unitStringFor:hertz |
|
1291 |
scale:1000 |
|
3574 | 1292 |
rounded:false |
3576 | 1293 |
unitStrings:#('Hz' 'Khz' 'Mhz' 'Ghz' 'Thz' 'Phz' 'Ehz') |
3120 | 1294 |
|
1295 |
" |
|
3577 | 1296 |
self frequencyStringFor:0.1 |
3120 | 1297 |
self frequencyStringFor:10 |
1298 |
self frequencyStringFor:100 |
|
1299 |
self frequencyStringFor:1000 |
|
1300 |
self frequencyStringFor:8192 |
|
1301 |
self frequencyStringFor:10000 |
|
1302 |
self frequencyStringFor:100000 |
|
1303 |
self frequencyStringFor:1000000 |
|
1304 |
self frequencyStringFor:10000000 |
|
1305 |
self frequencyStringFor:100000000 |
|
1306 |
self frequencyStringFor:1000000000 |
|
1307 |
self frequencyStringFor:10000000000 |
|
1308 |
self frequencyStringFor:100000000000 |
|
1309 |
self frequencyStringFor:1000000000000 |
|
1310 |
self frequencyStringFor:10000000000000 |
|
1311 |
" |
|
1312 |
! |
|
1313 |
||
363 | 1314 |
inchToMillimeter:inches |
364 | 1315 |
"convert inches to mm. Made this an extra method, since its so common." |
363 | 1316 |
|
1317 |
^ inches * 25.4 |
|
1318 |
||
1319 |
"Created: 31.5.1996 / 13:37:31 / cg" |
|
364 | 1320 |
"Modified: 31.5.1996 / 18:08:14 / cg" |
363 | 1321 |
! |
1322 |
||
1323 |
millimeterToInch:mm |
|
364 | 1324 |
"convert mm to inches. Made this an extra method, since its so common." |
363 | 1325 |
|
1326 |
^ mm / 25.4 |
|
1327 |
||
1328 |
"Created: 31.5.1996 / 13:37:48 / cg" |
|
364 | 1329 |
"Modified: 31.5.1996 / 18:08:20 / cg" |
3573 | 1330 |
! |
1331 |
||
3574 | 1332 |
unitStringFor:nBytes scale:k rounded:rounded unitStrings:unitStrings |
3573 | 1333 |
"common helper for SI (1000-based) and binary (1024-based) human readable string generators. |
3574 | 1334 |
Notice that both forms are often encountered and useful. |
1335 |
If rounded is true, round to nearest integer" |
|
3573 | 1336 |
|
3576 | 1337 |
|n kN idx val unitString| |
3573 | 1338 |
|
1339 |
nBytes isNil ifTrue:[ |
|
1340 |
^ '-' |
|
1341 |
]. |
|
1342 |
||
1343 |
idx := 1. |
|
1344 |
n := nBytes. |
|
1345 |
kN := 1. |
|
1346 |
[ (idx < unitStrings size) and:[ nBytes >= (kN * k) ] ] whileTrue:[ |
|
1347 |
idx := idx + 1. |
|
1348 |
kN := kN * k. |
|
1349 |
]. |
|
3575 | 1350 |
|
1351 |
val := nBytes / kN. |
|
3576 | 1352 |
unitString := ' ',(unitStrings at:idx). |
1353 |
||
3575 | 1354 |
((rounded and:[val > 20]) or:[val isInteger]) ifTrue:[ |
3576 | 1355 |
^ (val rounded) printString , unitString |
3574 | 1356 |
]. |
3576 | 1357 |
^ (val asFixedPoint:1) printString , unitString |
3573 | 1358 |
|
1359 |
" |
|
1360 |
self fileSizeStringFor:nil |
|
1361 |
self fileSizeStringFor:10 |
|
1362 |
self fileSizeStringFor:100 |
|
3574 | 1363 |
self fileSizeStringFor:900 |
1364 |
self fileSizeStringFor:1100 |
|
1365 |
self fileSizeStringFor:1400 |
|
1366 |
self fileSizeStringFor:1499 |
|
1367 |
self fileSizeStringFor:1500 |
|
1368 |
||
3573 | 1369 |
self fileSizeStringFor:1000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
1370 |
self fileSizeStringFor:8192 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1371 |
self fileSizeStringFor:10000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1372 |
self fileSizeStringFor:100000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1373 |
self fileSizeStringFor:1000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1374 |
self fileSizeStringFor:10000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1375 |
self fileSizeStringFor:100000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1376 |
self fileSizeStringFor:1000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1377 |
self fileSizeStringFor:10000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1378 |
self fileSizeStringFor:100000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1379 |
self fileSizeStringFor:1000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1380 |
self fileSizeStringFor:10000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1381 |
self fileSizeStringFor:10000000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1382 |
self fileSizeStringFor:100000000000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1383 |
self fileSizeStringFor:1000000000000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1384 |
||
1385 |
self fileSizeStringFor:8192 scale:1024 unitStrings:#('' 'KiB' 'MiB' 'GiB' 'TiB' 'PiB' 'EiB' ) |
|
1386 |
self fileSizeStringFor:81920000 scale:1024 unitStrings:#('' 'KiB' 'MiB' 'GiB' 'TiB' 'PiB' 'EiB' ) |
|
1387 |
" |
|
1388 |
||
1389 |
"Modified: / 17-12-2013 / 16:55:17 / cg" |
|
1390 |
"Modified (comment): / 18-12-2014 / 09:54:33 / sr" |
|
363 | 1391 |
! ! |
1392 |
||
516 | 1393 |
!UnitConverter class methodsFor:'missing conversion'! |
1394 |
||
1395 |
noConversionFrom:sourceUnit to:destUnit |
|
1396 |
Transcript showCR:'no conversion ' , sourceUnit printString , ' -> ' , destUnit printString. |
|
1397 |
^ nil |
|
1398 |
||
1399 |
"Created: 29.3.1997 / 17:56:03 / cg" |
|
1400 |
! ! |
|
1401 |
||
363 | 1402 |
!UnitConverter class methodsFor:'private'! |
1403 |
||
4514 | 1404 |
addConversion:factorOrBlock from:sourceMetric to:destMetric |
363 | 1405 |
"add a conversion" |
1406 |
||
1407 |
|conversion| |
|
1408 |
||
1409 |
conversion := Conversions at:sourceMetric ifAbsent:nil. |
|
1410 |
conversion isNil ifTrue:[ |
|
601 | 1411 |
conversion := IdentityDictionary new. |
1412 |
Conversions at:sourceMetric put:conversion |
|
363 | 1413 |
]. |
4514 | 1414 |
conversion at:destMetric put:factorOrBlock. |
363 | 1415 |
^ conversion |
1416 |
||
1417 |
"Created: 31.5.1996 / 13:51:25 / cg" |
|
515 | 1418 |
"Modified: 29.3.1997 / 17:19:31 / cg" |
363 | 1419 |
! |
1420 |
||
1421 |
convertDirect:howMany from:sourceMetric to:destMetric |
|
1422 |
"given a value in sourceMetric (symbolic), try to convert it |
|
1423 |
to destMetric (symbolic); |
|
1424 |
Only direct conversions are tried; return nil, if the conversion is unknown." |
|
1425 |
||
1426 |
|conversion factor| |
|
1427 |
||
550 | 1428 |
Conversions isNil ifTrue:[ |
601 | 1429 |
self initializeConversions |
550 | 1430 |
]. |
1431 |
||
363 | 1432 |
conversion := Conversions at:sourceMetric ifAbsent:nil. |
1433 |
conversion isNil ifTrue:[^ nil]. |
|
1434 |
||
1435 |
factor := conversion at:destMetric ifAbsent:nil. |
|
1436 |
factor isNil ifTrue:[^ nil]. |
|
1437 |
||
364 | 1438 |
factor isNumber ifTrue:[ |
601 | 1439 |
^ howMany * factor |
364 | 1440 |
]. |
515 | 1441 |
^ factor value:howMany |
363 | 1442 |
|
1443 |
" |
|
1444 |
UnitConverter convertDirect:1 from:#inch to:#meter |
|
1445 |
UnitConverter convertDirect:1 from:#inch to:#millimeter |
|
515 | 1446 |
UnitConverter convertDirect:1 from:#degrees to:#fahrenheit |
363 | 1447 |
" |
1448 |
||
1449 |
"Created: 31.5.1996 / 13:54:33 / cg" |
|
550 | 1450 |
"Modified: 22.7.1997 / 13:57:52 / cg" |
363 | 1451 |
! |
1452 |
||
1453 |
unscaled:what |
|
1454 |
"given a unit, return the base and a factor as assoc, |
|
1455 |
or nil if not found" |
|
1456 |
||
550 | 1457 |
Conversions isNil ifTrue:[ |
2995 | 1458 |
self initializeConversions |
550 | 1459 |
]. |
1460 |
||
363 | 1461 |
Scaling keysAndValuesDo:[:name :factor | |
2995 | 1462 |
|rest| |
363 | 1463 |
|
2995 | 1464 |
(what startsWith:name) ifTrue:[ |
1465 |
rest := what withoutPrefix:name. |
|
1466 |
rest := rest asSymbolIfInterned. |
|
1467 |
rest notNil ifTrue:[ |
|
1468 |
^ rest -> factor. |
|
1469 |
] |
|
1470 |
]. |
|
363 | 1471 |
]. |
1472 |
^ nil |
|
1473 |
||
1474 |
" |
|
1475 |
UnitConverter unscaled:#millimeter |
|
1476 |
UnitConverter unscaled:#nanometer |
|
1477 |
UnitConverter unscaled:#kilometer |
|
1478 |
UnitConverter unscaled:#fuzzymeter |
|
1479 |
" |
|
1480 |
||
550 | 1481 |
"Modified: 22.7.1997 / 13:57:46 / cg" |
363 | 1482 |
! ! |
1483 |
||
3650 | 1484 |
!UnitConverter class methodsFor:'queries'! |
1485 |
||
1486 |
isUtilityClass |
|
1487 |
^ self == UnitConverter |
|
1488 |
! ! |
|
1489 |
||
363 | 1490 |
!UnitConverter class methodsFor:'documentation'! |
1491 |
||
1492 |
version |
|
3573 | 1493 |
^ '$Header$' |
2320 | 1494 |
! |
1495 |
||
1496 |
version_CVS |
|
3573 | 1497 |
^ '$Header$' |
363 | 1498 |
! ! |
2988
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
1499 |