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