author | Claus Gittinger <cg@exept.de> |
Thu, 07 Aug 1997 14:10:26 +0200 | |
changeset 556 | fdf0932239a0 |
parent 555 | 14f0caf53e2f |
child 564 | 9d5d54a11553 |
permissions | -rw-r--r-- |
363 | 1 |
Object subclass:#UnitConverter |
2 |
instanceVariableNames:'' |
|
516 | 3 |
classVariableNames:'Conversions Scaling Aliases Constants' |
363 | 4 |
poolDictionaries:'' |
5 |
category:'Magnitude-General' |
|
6 |
! |
|
7 |
||
8 |
!UnitConverter class methodsFor:'documentation'! |
|
9 |
||
10 |
documentation |
|
11 |
" |
|
12 |
In order to collect various unit conversions into one central |
|
13 |
manageable (and especially: browsable) place, all previously |
|
14 |
spread knowledge about metric (and other) conversions have been |
|
15 |
brought into this class. |
|
16 |
||
17 |
This class is purely functional, abstract and has no instances; |
|
18 |
all queries are via class protocol. |
|
19 |
Choosing the Magnitudes category as its home is arbitrary. |
|
20 |
||
515 | 21 |
The supported units are setup in the classes initialize method; |
364 | 22 |
supported are: |
23 |
meter - inch - feet - foot - yard |
|
24 |
meter2 - acre - 'german-ar' 'german-hektar' are |
|
25 |
liter - gallon - barrel - floz |
|
515 | 26 |
celsius - fahrenheit |
364 | 27 |
and many others. |
28 |
||
515 | 29 |
The converter does a recursive search for a conversion; |
30 |
thus, if there is a conversion from #inch to #millimeter and another |
|
31 |
from #millimeter to #kilometer, conversion from #inch to #kilometer |
|
32 |
is found automatically. |
|
33 |
||
363 | 34 |
[author:] |
35 |
Claus Gittinger |
|
36 |
||
37 |
see also: |
|
38 |
examples |
|
39 |
/usr/share/lib/unittab (if present on your system) |
|
40 |
" |
|
41 |
! |
|
42 |
||
43 |
examples |
|
44 |
" |
|
45 |
ever wanted to know, how many floz's are there in a european |
|
46 |
Coce bottle ? |
|
47 |
[exBegin] |
|
48 |
Transcript showCR: |
|
49 |
(UnitConverter convert:1 from:#liter to:#floz) |
|
50 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
51 |
|
363 | 52 |
or, how many square-meters an acre is: |
53 |
[exBegin] |
|
54 |
Transcript showCR: |
|
516 | 55 |
(UnitConverter convert:1 from:#acre to:#'meter^2') |
363 | 56 |
[exEnd] |
57 |
||
516 | 58 |
|
515 | 59 |
or europeans might want to know, what those |
60 |
fahrenheit numbers mean in an US weather report ;-): |
|
61 |
[exBegin] |
|
62 |
Transcript showCR:( |
|
63 |
(UnitConverter convert:80 from:#fahrenheit to:#celsius) |
|
64 |
asFixedPoint:1) |
|
65 |
[exEnd] |
|
66 |
||
516 | 67 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
68 |
how fast do I drive ? :-) |
516 | 69 |
[exBegin] |
70 |
Transcript showCR:( |
|
71 |
UnitConverter convert:200 from:#'km/hr' to:#'mile/hr') |
|
72 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
73 |
how fast does Chris drive ? :-) |
516 | 74 |
[exBegin] |
75 |
Transcript showCR:( |
|
76 |
UnitConverter convert:65 from:#'mile/hr' to:#'km/hr') |
|
77 |
[exEnd] |
|
78 |
||
79 |
||
80 |
calories or joule ? |
|
81 |
[exBegin] |
|
82 |
Transcript showCR:( |
|
83 |
UnitConverter convert:0.18 from:#'kilocalorie' to:#'kilojoule') |
|
84 |
[exEnd] |
|
85 |
[exBegin] |
|
86 |
Transcript showCR:( |
|
87 |
UnitConverter convert:2000 from:#'kilocalorie' to:#'kilojoule') |
|
88 |
[exEnd] |
|
89 |
||
90 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
91 |
distances: |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
92 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
93 |
[exBegin] |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
94 |
Transcript showCR:( |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
95 |
UnitConverter convert:1 from:#'lightsecond' to:#'kilometer') |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
96 |
[exEnd] |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
97 |
thats the same: |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
98 |
[exBegin] |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
99 |
Transcript showCR:( |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
100 |
UnitConverter convert:1 from:#'lightspeed*s' to:#'kilometer') |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
101 |
[exEnd] |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
102 |
a days travel ... |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
103 |
[exBegin] |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
104 |
Transcript showCR:( |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
105 |
UnitConverter convert:1 from:#'lightspeed*dy' to:#'kilometer') |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
106 |
[exEnd] |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
107 |
a year travel ... |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
108 |
[exBegin] |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
109 |
Transcript showCR:( |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
110 |
UnitConverter convert:1 from:#'lightyear' to:#'kilometer') |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
111 |
[exEnd] |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
112 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
113 |
|
363 | 114 |
real estate buyers might want to know, how many acres |
115 |
a german ar is: |
|
116 |
[exBegin] |
|
117 |
Transcript showCR: |
|
118 |
(UnitConverter convert:1 from:#'german-ar' to:#acre) |
|
119 |
[exEnd] |
|
516 | 120 |
- or how many square-feet are there in your living room: |
364 | 121 |
[exBegin] |
122 |
Transcript showCR: |
|
516 | 123 |
(UnitConverter convert:100 from:#'meter^2' to:#'foot^2') |
364 | 124 |
[exEnd] |
516 | 125 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
126 |
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
|
127 |
(roughly, since a teaspoon is not a standard unit) |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
128 |
[exBegin] |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
129 |
Transcript showCR: |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
130 |
(UnitConverter convert:1 from:#'meter^3' to:#teaspoon) |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
131 |
[exEnd] |
365 | 132 |
how wide is a US page in inches: |
133 |
[exBegin] |
|
134 |
Transcript showCR: |
|
135 |
(UnitConverter convert:1 from:#'letterW' to:#inch) |
|
136 |
[exEnd] |
|
516 | 137 |
- in millimeter: |
365 | 138 |
[exBegin] |
139 |
Transcript showCR: |
|
140 |
(UnitConverter convert:1 from:#'letterW' to:#mm) |
|
141 |
[exEnd] |
|
516 | 142 |
|
143 |
||
365 | 144 |
the height of a US page in inches: |
145 |
[exBegin] |
|
146 |
Transcript showCR: |
|
147 |
(UnitConverter convert:1 from:#'letterH' to:#inch) |
|
148 |
[exEnd] |
|
516 | 149 |
- in millimeter: |
365 | 150 |
[exBegin] |
151 |
Transcript showCR: |
|
152 |
(UnitConverter convert:1 from:#'letterH' to:#mm) |
|
153 |
[exEnd] |
|
516 | 154 |
|
155 |
||
365 | 156 |
the same for european A4 standard page: |
157 |
[exBegin] |
|
158 |
Transcript showCR: |
|
159 |
(UnitConverter convert:1 from:#'a4H' to:#mm) |
|
160 |
[exEnd] |
|
516 | 161 |
[exBegin] |
162 |
Transcript showCR: |
|
163 |
(UnitConverter convert:1 from:#'a4H' to:#inch) |
|
164 |
[exEnd] |
|
363 | 165 |
" |
166 |
! ! |
|
167 |
||
168 |
!UnitConverter class methodsFor:'initialization'! |
|
169 |
||
550 | 170 |
initializeConversions |
363 | 171 |
"initialize common conversions" |
172 |
||
173 |
Scaling := IdentityDictionary new. |
|
174 |
Conversions := IdentityDictionary new. |
|
175 |
Aliases := IdentityDictionary new. |
|
516 | 176 |
Constants := IdentityDictionary new. |
363 | 177 |
|
515 | 178 |
Scaling at:#exa put:1000000000000000000. |
179 |
Scaling at:#peta put:1000000000000000. |
|
363 | 180 |
Scaling at:#tera put:1000000000000. |
181 |
Scaling at:#giga put:1000000000. |
|
182 |
Scaling at:#mega put:1000000. |
|
515 | 183 |
Scaling at:#myria put:10000. |
363 | 184 |
Scaling at:#kilo put:1000. |
515 | 185 |
Scaling at:#hecto put:100. |
363 | 186 |
Scaling at:#deci put:(1/10). |
187 |
Scaling at:#centi put:(1/100). |
|
188 |
Scaling at:#milli put:(1/1000). |
|
189 |
Scaling at:#micro put:(1/1000000). |
|
190 |
Scaling at:#nano put:(1/1000000000). |
|
191 |
Scaling at:#pico put:(1/1000000000000). |
|
515 | 192 |
Scaling at:#femto put:(1/1000000000000000). |
193 |
Scaling at:#atto put:(1/1000000000000000000). |
|
363 | 194 |
|
516 | 195 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
196 |
"/ ---------- velocity ------------- |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
197 |
|
556 | 198 |
Constants at:#lightspeed put:#(2.997925E8 #'m/s'). |
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
199 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
200 |
|
363 | 201 |
"/ -------------- length ------------- |
202 |
||
203 |
Aliases at:#km put:#kilometer. |
|
204 |
Aliases at:#m put:#meter. |
|
205 |
Aliases at:#dm put:#decimeter. |
|
206 |
Aliases at:#cm put:#centimeter. |
|
207 |
Aliases at:#mm put:#millimeter. |
|
208 |
Aliases at:#micron put:#micrometer. |
|
209 |
Aliases at:#nm put:#nanometer. |
|
210 |
Aliases at:#angstrom put:#decinanometer. |
|
211 |
||
212 |
"/ US |
|
213 |
self addConversion:12 from:#foot to:#inch. |
|
214 |
self addConversion:3 from:#yard to:#foot. |
|
215 |
self addConversion:5280 from:#mile to:#foot. |
|
515 | 216 |
self addConversion:0.001 from:#inch to:#mil. |
363 | 217 |
|
218 |
Aliases at:#feet put:#foot. |
|
219 |
Aliases at:#ft put:#foot. |
|
220 |
Aliases at:#yd put:#yard. |
|
221 |
Aliases at:#mi put:#mile. |
|
222 |
||
223 |
"/ inch to millimeter |
|
224 |
self addConversion:(25.4/1000) from:#inch to:#meter. |
|
225 |
||
365 | 226 |
"/ nautic |
227 |
self addConversion:1852 from:#'nautical-mile' to:#meter. |
|
228 |
||
556 | 229 |
Aliases at:#lightsecond put:#'lightspeed*s'. |
230 |
Aliases at:#lightyear put:#'lightspeed*yr'. |
|
231 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
232 |
self addConversion:((Constants at:#lightspeed) at:1) from:#'lightspeed*s' to:#'meter'. |
385 | 233 |
|
516 | 234 |
"/ ---------- time ------------- |
235 |
||
236 |
self addConversion:60 from:#min to:#s. |
|
237 |
self addConversion:60 from:#hr to:#min. |
|
238 |
self addConversion:24 from:#dy to:#hr. |
|
239 |
self addConversion:365.24219879 from:#yr to:#dy. |
|
240 |
||
241 |
||
385 | 242 |
"/ ---------- printing ------------- |
365 | 243 |
|
363 | 244 |
"/ inch to (roughly) a typesetter point |
245 |
self addConversion:(1/72) from:#point to:#inch. |
|
246 |
||
247 |
"/ point to twips; 20 twips (as in Rich-Text-Format) make a point |
|
248 |
self addConversion:(1/20) from:#twip to:#point. |
|
249 |
||
365 | 250 |
"/ US paper |
251 |
self addConversion:11 from:#'letter-page-height' to:#inch. |
|
252 |
self addConversion:8.5 from:#'letter-page-width' to:#inch. |
|
253 |
self addConversion:8.5 from:#'letter-landscape-page-height' to:#inch. |
|
254 |
self addConversion:11 from:#'letter-landscape-page-width' to:#inch. |
|
255 |
||
385 | 256 |
self addConversion:14 from:#'legal-page-height' to:#inch. |
257 |
self addConversion:8.5 from:#'legal-page-width' to:#inch. |
|
258 |
self addConversion:8.5 from:#'legal-landscape-page-height' to:#inch. |
|
259 |
self addConversion:14 from:#'legal-landscape-page-width' to:#inch. |
|
260 |
||
261 |
self addConversion:11 from:#'ledger-page-height' to:#inch. |
|
262 |
self addConversion:17 from:#'ledger-page-width' to:#inch. |
|
263 |
self addConversion:17 from:#'ledger-landscape-page-height' to:#inch. |
|
264 |
self addConversion:11 from:#'ledger-landscape-page-width' to:#inch. |
|
265 |
||
365 | 266 |
"/ European paper |
267 |
self addConversion:840 from:#'a1-page-height' to:#millimeter. |
|
268 |
self addConversion:592 from:#'a1-page-width' to:#millimeter. |
|
269 |
self addConversion:592 from:#'a1-landscape-page-height' to:#millimeter. |
|
270 |
self addConversion:840 from:#'a1-landscape-page-width' to:#millimeter. |
|
271 |
||
272 |
self addConversion:592 from:#'a2-page-height' to:#millimeter. |
|
273 |
self addConversion:420 from:#'a2-page-width' to:#millimeter. |
|
274 |
self addConversion:420 from:#'a2-landscape-page-height' to:#millimeter. |
|
275 |
self addConversion:592 from:#'a2-landscape-page-width' to:#millimeter. |
|
276 |
||
277 |
self addConversion:420 from:#'a3-page-height' to:#millimeter. |
|
278 |
self addConversion:296 from:#'a3-page-width' to:#millimeter. |
|
279 |
self addConversion:296 from:#'a3-landscape-page-height' to:#millimeter. |
|
280 |
self addConversion:420 from:#'a3-landscape-page-width' to:#millimeter. |
|
281 |
||
282 |
self addConversion:296 from:#'a4-page-height' to:#millimeter. |
|
283 |
self addConversion:210 from:#'a4-page-width' to:#millimeter. |
|
284 |
self addConversion:210 from:#'a4-landscape-page-height' to:#millimeter. |
|
285 |
self addConversion:296 from:#'a4-landscape-page-width' to:#millimeter. |
|
286 |
||
287 |
self addConversion:210 from:#'a5-page-height' to:#millimeter. |
|
288 |
self addConversion:148 from:#'a5-page-width' to:#millimeter. |
|
289 |
self addConversion:148 from:#'a5-landscape-page-height' to:#millimeter. |
|
290 |
self addConversion:210 from:#'a5-landscape-page-width' to:#millimeter. |
|
291 |
||
292 |
self addConversion:148 from:#'a6-page-height' to:#millimeter. |
|
293 |
self addConversion:105 from:#'a6-page-width' to:#millimeter. |
|
294 |
self addConversion:105 from:#'a6-landscape-page-height' to:#millimeter. |
|
295 |
self addConversion:148 from:#'a6-landscape-page-width' to:#millimeter. |
|
296 |
||
385 | 297 |
self addConversion:257 from:#'b5-page-height' to:#millimeter. |
298 |
self addConversion:182 from:#'b5-page-width' to:#millimeter. |
|
299 |
self addConversion:182 from:#'b5-landscape-page-height' to:#millimeter. |
|
300 |
self addConversion:257 from:#'b5-landscape-page-width' to:#millimeter. |
|
301 |
||
302 |
||
365 | 303 |
Aliases at:#letterW put:#'letter-page-width'. |
304 |
Aliases at:#letterH put:#'letter-page-height'. |
|
385 | 305 |
Aliases at:#legalW put:#'legal-page-width'. |
306 |
Aliases at:#legalH put:#'legal-page-height'. |
|
307 |
Aliases at:#ledgerW put:#'ledger-page-width'. |
|
308 |
Aliases at:#ledgerH put:#'ledger-page-height'. |
|
365 | 309 |
Aliases at:#a1W put:#'a1-page-width'. |
310 |
Aliases at:#a1H put:#'a1-page-height'. |
|
311 |
Aliases at:#a2W put:#'a2-page-width'. |
|
312 |
Aliases at:#a2H put:#'a2-page-height'. |
|
313 |
Aliases at:#a3W put:#'a3-page-width'. |
|
314 |
Aliases at:#a3H put:#'a3-page-height'. |
|
315 |
Aliases at:#a4W put:#'a4-page-width'. |
|
316 |
Aliases at:#a4H put:#'a4-page-height'. |
|
317 |
Aliases at:#a5W put:#'a5-page-width'. |
|
318 |
Aliases at:#a5H put:#'a5-page-height'. |
|
319 |
Aliases at:#a6W put:#'a6-page-width'. |
|
320 |
Aliases at:#a6H put:#'a6-page-height'. |
|
385 | 321 |
Aliases at:#b5W put:#'b5-page-width'. |
322 |
Aliases at:#b5H put:#'b5-page-height'. |
|
365 | 323 |
|
324 |
Aliases at:#letterlW put:#'letter-landscape-page-width'. |
|
325 |
Aliases at:#letterlH put:#'letter-landscape-page-height'. |
|
385 | 326 |
Aliases at:#legallW put:#'legal-landscape-page-width'. |
327 |
Aliases at:#legallH put:#'legal-landscape-page-height'. |
|
328 |
Aliases at:#ledgerlW put:#'ledger-landscape-page-width'. |
|
329 |
Aliases at:#ledgerlH put:#'ledger-landscape-page-height'. |
|
365 | 330 |
Aliases at:#a1lW put:#'a1-landscape-page-width'. |
331 |
Aliases at:#a1lH put:#'a1-landscape-page-height'. |
|
332 |
Aliases at:#a2lW put:#'a2-landscape-page-width'. |
|
333 |
Aliases at:#a2lH put:#'a2-landscape-page-height'. |
|
334 |
Aliases at:#a3lW put:#'a3-landscape-page-width'. |
|
335 |
Aliases at:#a3lH put:#'a3-landscape-page-height'. |
|
336 |
Aliases at:#a4lW put:#'a4-landscape-page-width'. |
|
337 |
Aliases at:#a4lH put:#'a4-landscape-page-height'. |
|
338 |
Aliases at:#a5lW put:#'a5-landscape-page-width'. |
|
339 |
Aliases at:#a5lH put:#'a5-landscape-page-height'. |
|
340 |
Aliases at:#a6lW put:#'a6-landscape-page-width'. |
|
341 |
Aliases at:#a6lH put:#'a6-landscape-page-height'. |
|
385 | 342 |
Aliases at:#b5lW put:#'b5-landscape-page-width'. |
343 |
Aliases at:#b5lH put:#'b5-landscape-page-height'. |
|
363 | 344 |
|
516 | 345 |
|
363 | 346 |
"/ ---------------- area ------------------- |
347 |
||
348 |
"/ US |
|
516 | 349 |
self addConversion:4840 from:#acre to:#'yard^2'. |
363 | 350 |
|
516 | 351 |
self addConversion:100 from:#are to:#'meter^2'. |
363 | 352 |
self addConversion:100 from:#hectare to:#are. |
353 |
||
354 |
"/ german area - add your countries, and return to me ... |
|
355 |
Aliases at:#'german-ar' put:#are. |
|
356 |
Aliases at:#'german-hektar' put:#hectare. |
|
357 |
||
516 | 358 |
|
363 | 359 |
"/ ---------------- liquid ---------------- |
360 |
||
516 | 361 |
self addConversion:231 from:#gallon to:#'inch^3'. |
363 | 362 |
self addConversion:(1/4) from:#quart to:#gallon. "/ well - at least here, |
363 |
"/ thats also 1/4th of a good wine ;-) |
|
364 |
self addConversion:(1/2) from:#pint to:#quart. |
|
365 |
self addConversion:(1/16) from:#floz to:#pint. |
|
366 |
self addConversion:(1/8) from:#fldr to:#floz. |
|
367 |
||
368 |
self addConversion:42 from:#barrel to:#gallon. |
|
369 |
||
516 | 370 |
Aliases at:#cc put:#'cm^3'. |
363 | 371 |
Aliases at:#liter put:#kilocc. |
372 |
Aliases at:#ml put:#milliliter. |
|
373 |
Aliases at:#gal put:#gallon. |
|
374 |
Aliases at:#qt put:#quart. |
|
375 |
||
516 | 376 |
|
363 | 377 |
"/ ---------------- mass ---------------- |
515 | 378 |
|
363 | 379 |
self addConversion:453.59237 from:#lb to:#gram. |
380 |
self addConversion:205 from:#carat to:#milligram. |
|
381 |
||
382 |
Aliases at:#oz put:#ounce. |
|
383 |
||
384 |
Aliases at:#gm put:#gram. |
|
385 |
Aliases at:#kg put:#kilogram. |
|
386 |
||
387 |
"/ well, a pint of beer is not always: |
|
516 | 388 |
self addConversion:277.420 from:#'british-gallon' to:#'in^3'. |
363 | 389 |
self addConversion:(1/4) from:#'british-quart' to:#'british-gallon'. |
390 |
self addConversion:(1/2) from:#'british-pint' to:#'british-quart'. |
|
391 |
self addConversion:(1/16) from:#'british-floz' to:#'british-pint'. |
|
392 |
||
516 | 393 |
|
363 | 394 |
"/ ---------------- energy ---------------- |
395 |
||
396 |
Aliases at:#cal put:#calorie. |
|
516 | 397 |
Aliases at:#nt put:#newton. |
398 |
Aliases at:#joule put:#'nt*m'. |
|
363 | 399 |
|
516 | 400 |
self addConversion:4.1868 from:#calorie to:#joule. |
401 |
||
363 | 402 |
|
403 |
"/ ---------------- cooking ---------------- |
|
404 |
||
405 |
self addConversion:4 from:#tablespoon to:#fldr. |
|
406 |
self addConversion:(4/3) from:#teaspoon to:#fldr. |
|
407 |
||
516 | 408 |
|
515 | 409 |
"/ ---------------- temperature ---------------- |
410 |
||
411 |
self addConversion:[:d | d * 1.8 + 32] from:#celsius to:#fahrenheit. |
|
412 |
self addConversion:[:f | f - 32 / 1.8] from:#fahrenheit to:#celsius. |
|
413 |
||
363 | 414 |
" |
415 |
UnitConverter initialize |
|
416 |
" |
|
417 |
||
550 | 418 |
"Created: 22.7.1997 / 13:56:40 / cg" |
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
419 |
"Modified: 6.8.1997 / 17:16:00 / cg" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
420 |
! ! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
421 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
422 |
!UnitConverter class methodsFor:'accessing'! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
423 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
424 |
scalings |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
425 |
"return the set of known scaling prefixes" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
426 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
427 |
^ Scaling keys |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
428 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
429 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
430 |
UnitConverter scalings |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
431 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
432 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
433 |
"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
|
434 |
"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
|
435 |
! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
436 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
437 |
units |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
438 |
"return the set of known units" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
439 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
440 |
|setOfUnits| |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
441 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
442 |
setOfUnits := IdentitySet new. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
443 |
Conversions keysAndValuesDo:[:srcUnit :conversionInfo | |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
444 |
setOfUnits add:srcUnit. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
445 |
conversionInfo keysDo:[:targetUnit | |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
446 |
setOfUnits add:targetUnit |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
447 |
] |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
448 |
]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
449 |
^ setOfUnits |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
450 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
451 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
452 |
UnitConverter units |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
453 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
454 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
455 |
"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
|
456 |
"Modified: 6.8.1997 / 16:57:47 / cg" |
363 | 457 |
! ! |
458 |
||
459 |
!UnitConverter class methodsFor:'conversions'! |
|
460 |
||
461 |
convert:howMany from:sourceUnit to:destUnit |
|
462 |
"given a value in sourceUnit (symbolic), try to convert it |
|
463 |
to destUnit (symbolic); return nil, if the conversion is |
|
464 |
unknown." |
|
465 |
||
516 | 466 |
|u conversions alias rslt sU dU const val unit |
467 |
i suNumerator suDenominator duNumerator duDenominator uN uD |
|
468 |
sF1 sF2 dF1 dF2 |
|
469 |
s d| |
|
363 | 470 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
471 |
"/ Transcript showCR:('try ' , sourceUnit , '->' , destUnit). |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
472 |
|
550 | 473 |
Conversions isNil ifTrue:[ |
474 |
self initializeConversions |
|
475 |
]. |
|
476 |
||
363 | 477 |
"/ somehow, recursion must end ... |
478 |
sourceUnit == destUnit ifTrue:[ |
|
479 |
^ howMany |
|
480 |
]. |
|
481 |
||
369 | 482 |
sourceUnit isSymbol ifFalse:[ |
516 | 483 |
s := sourceUnit withoutSeparators. |
484 |
sU := s asSymbolIfInterned. |
|
485 |
sU isNil ifTrue:[ |
|
486 |
(s startsWith:$() ifTrue:[ |
|
487 |
(s endsWith:$)) ifTrue:[ |
|
488 |
s := s copyFrom:2 to:(s size - 1). |
|
489 |
^ self convert:howMany from:s to:destUnit |
|
490 |
] |
|
491 |
]. |
|
492 |
^ nil |
|
493 |
]. |
|
369 | 494 |
^ self convert:howMany from:sU to:destUnit |
495 |
]. |
|
496 |
destUnit isSymbol ifFalse:[ |
|
516 | 497 |
d := destUnit withoutSeparators. |
498 |
dU := d asSymbolIfInterned. |
|
499 |
dU isNil ifTrue:[ |
|
500 |
(d startsWith:$() ifTrue:[ |
|
501 |
(d endsWith:$)) ifTrue:[ |
|
502 |
d := d copyFrom:2 to:(d size - 1). |
|
503 |
^ self convert:howMany from:sourceUnit to:d |
|
504 |
] |
|
505 |
]. |
|
506 |
^ nil |
|
507 |
]. |
|
369 | 508 |
^ self convert:howMany from:sourceUnit to:dU |
509 |
]. |
|
510 |
||
363 | 511 |
"/ first, get rid of scalers ... |
512 |
||
513 |
u := self unscaled:sourceUnit. |
|
514 |
u notNil ifTrue:[ |
|
515 |
^ self convert:(howMany*(u value)) from:(u key) to:destUnit |
|
516 |
]. |
|
517 |
||
518 |
u := self unscaled:destUnit. |
|
519 |
u notNil ifTrue:[ |
|
520 |
^ self convert:(howMany/(u value)) from:sourceUnit to:(u key) |
|
521 |
]. |
|
522 |
||
523 |
"/ and of aliases ... |
|
524 |
||
525 |
alias := Aliases at:sourceUnit ifAbsent:nil. |
|
526 |
alias notNil ifTrue:[ |
|
527 |
^ self convert:howMany from:alias to:destUnit |
|
528 |
]. |
|
529 |
alias := Aliases at:destUnit ifAbsent:nil. |
|
530 |
alias notNil ifTrue:[ |
|
531 |
^ self convert:howMany from:sourceUnit to:alias |
|
532 |
]. |
|
533 |
||
516 | 534 |
"/ any constants ? |
535 |
||
536 |
(Constants includesKey:sourceUnit) ifTrue:[ |
|
537 |
const := Constants at:sourceUnit. |
|
538 |
val := const at:1. |
|
539 |
unit := const at:2. |
|
540 |
^ self convert:(howMany*val) from:unit to:destUnit |
|
541 |
]. |
|
542 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
543 |
"/ compounds (^ , / or *) are very naively parsed |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
544 |
"/ 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
|
545 |
"/ 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
|
546 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
547 |
|
363 | 548 |
"/ working with squares or cubics ? |
549 |
||
516 | 550 |
((sourceUnit endsWith:'^2') and:[destUnit endsWith:'^2']) ifTrue:[ |
551 |
sU := (sourceUnit copyWithoutLast:2) asSymbolIfInterned. |
|
552 |
dU := (destUnit copyWithoutLast:2) asSymbolIfInterned. |
|
363 | 553 |
(sU notNil and:[dU notNil]) ifTrue:[ |
554 |
^ (self convert:(howMany sqrt) from:sU to:dU) squared |
|
555 |
]. |
|
516 | 556 |
^ self noConversionFrom:sourceUnit to:destUnit. |
363 | 557 |
]. |
516 | 558 |
((sourceUnit endsWith:'^3') and:[destUnit endsWith:'^3']) ifTrue:[ |
559 |
sU := (sourceUnit copyWithoutLast:2) asSymbolIfInterned. |
|
560 |
dU := (destUnit copyWithoutLast:2) asSymbolIfInterned. |
|
363 | 561 |
(sU notNil and:[dU notNil]) ifTrue:[ |
562 |
^ (self convert:(howMany raisedTo:(1/3)) from:sU to:dU) raisedTo:3 |
|
563 |
]. |
|
516 | 564 |
^ self noConversionFrom:sourceUnit to:destUnit. |
565 |
]. |
|
566 |
||
567 |
"/ working with fractions ? |
|
568 |
||
569 |
((sourceUnit includes:$/) and:[destUnit includes:$/]) ifTrue:[ |
|
570 |
"/ look for a constant conversion factor |
|
571 |
i := sourceUnit indexOf:$/. |
|
572 |
suNumerator := sourceUnit copyTo:(i - 1). |
|
573 |
suDenominator := sourceUnit copyFrom:(i + 1). |
|
574 |
i := destUnit indexOf:$/. |
|
575 |
duNumerator := destUnit copyTo:(i - 1). |
|
576 |
duDenominator := destUnit copyFrom:(i + 1). |
|
577 |
||
578 |
uN := self convert:howMany from:suNumerator to:duNumerator. |
|
579 |
uN notNil ifTrue:[ |
|
580 |
uD := self convert:1 from:suDenominator to:duDenominator. |
|
581 |
uD notNil ifTrue:[ |
|
582 |
^ uN / uD |
|
583 |
] |
|
584 |
]. |
|
585 |
]. |
|
586 |
||
587 |
"/ working with products ? |
|
588 |
||
589 |
((sourceUnit includes:$*) and:[destUnit includes:$*]) ifTrue:[ |
|
590 |
i := sourceUnit indexOf:$*. |
|
591 |
sF1 := sourceUnit copyTo:(i - 1). |
|
592 |
sF2 := sourceUnit copyFrom:(i + 1). |
|
593 |
i := destUnit indexOf:$*. |
|
594 |
dF1 := destUnit copyTo:(i - 1). |
|
595 |
dF2 := destUnit copyFrom:(i + 1). |
|
596 |
||
597 |
u := self convert:howMany from:sF1 to:dF1. |
|
598 |
u notNil ifTrue:[ |
|
599 |
u := self convert:u from:sF2 to:dF2. |
|
600 |
u notNil ifTrue:[ |
|
601 |
^ u |
|
602 |
] |
|
603 |
]. |
|
363 | 604 |
]. |
605 |
||
606 |
"/ the real work comes here ... |
|
607 |
||
608 |
"/ is there a direct conversion in the dataBase ? |
|
609 |
||
610 |
rslt := self convertDirect:howMany from:sourceUnit to:destUnit. |
|
611 |
rslt notNil ifTrue:[^ rslt]. |
|
612 |
||
613 |
"/ try inverse conversion ... |
|
614 |
||
615 |
rslt := self convertDirect:1 from:destUnit to:sourceUnit. |
|
616 |
rslt notNil ifTrue:[ ^ howMany / rslt]. |
|
617 |
||
516 | 618 |
"/ here's the deep recursion ... |
363 | 619 |
|
620 |
||
621 |
"/ try indirect conversion from source |
|
622 |
||
623 |
conversions := Conversions at:sourceUnit ifAbsent:nil. |
|
624 |
conversions notNil ifTrue:[ |
|
625 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 | |
|
626 |
|factor2| |
|
627 |
||
628 |
factor2 := self convert:factor1 from:intermediateUnit to:destUnit. |
|
629 |
factor2 notNil ifTrue:[^ factor2 * howMany]. |
|
630 |
]. |
|
631 |
]. |
|
632 |
||
633 |
"/ try indirect conversion from dest |
|
634 |
||
635 |
conversions := Conversions at:destUnit ifAbsent:nil. |
|
636 |
conversions notNil ifTrue:[ |
|
637 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 | |
|
638 |
|factor2| |
|
639 |
||
640 |
factor2 := self convert:(factor1) from:intermediateUnit to:sourceUnit. |
|
641 |
factor2 notNil ifTrue:[^ howMany / factor2]. |
|
642 |
]. |
|
643 |
]. |
|
644 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
645 |
"/ 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
|
646 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
647 |
(sourceUnit includes:$*) ifTrue:[ |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
648 |
i := sourceUnit indexOf:$*. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
649 |
sF1 := sourceUnit copyTo:(i - 1). |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
650 |
sF2 := sourceUnit copyFrom:(i + 1). |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
651 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
652 |
"/ see what we have ... |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
653 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
654 |
self units do:[:aUnit | |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
655 |
|pref iUnit factor2 rslt| |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
656 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
657 |
pref := sF1 , '*'. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
658 |
(aUnit startsWith:pref) ifTrue:[ |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
659 |
"/ ok; want a/b -> x |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
660 |
"/ found a/c -> any |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
661 |
"/ what about c->b ? |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
662 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
663 |
iUnit := aUnit copyFrom:pref size + 1. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
664 |
factor2 := self convert:1 from:sF2 to:iUnit. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
665 |
factor2 notNil ifTrue:[ |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
666 |
"/ good ... |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
667 |
rslt := self convert:(factor2 * howMany) |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
668 |
from:aUnit to:destUnit. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
669 |
rslt notNil ifTrue:[^ rslt]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
670 |
] |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
671 |
] |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
672 |
]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
673 |
]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
674 |
|
516 | 675 |
^ self noConversionFrom:sourceUnit to:destUnit. |
363 | 676 |
|
677 |
"direct - how many meters are there in two inches: |
|
678 |
UnitConverter convert:2 from:#inch to:#meter |
|
515 | 679 |
UnitConverter convert:37 from:#degrees to:#fahrenheit |
363 | 680 |
|
681 |
reverse - how many inches are there in one meter |
|
682 |
UnitConverter convert:1 from:#meter to:#inch |
|
683 |
||
684 |
with alias: |
|
685 |
UnitConverter convert:1 from:#inch to:#m |
|
686 |
UnitConverter convert:1 from:#inch to:#mm |
|
687 |
UnitConverter convert:1 from:#inch to:#millimeter |
|
688 |
UnitConverter convert:1 from:#inch to:#nanometer |
|
689 |
UnitConverter convert:1 from:#mm to:#km |
|
690 |
UnitConverter convert:1 from:#km to:#foot |
|
691 |
||
692 |
indirect: |
|
693 |
UnitConverter convert:1 from:#mm to:#twip |
|
694 |
UnitConverter convert:1 from:#inch to:#twip |
|
369 | 695 |
UnitConverter convert:1 from:'letterH' to:#point |
696 |
UnitConverter convert:1 from:'letterlH' to:#point |
|
364 | 697 |
|
698 |
UnitConverter convert:5 from:#barrel to:#liter |
|
699 |
UnitConverter convert:10 from:#kilogram to:#carat |
|
700 |
||
701 |
UnitConverter convert:1 from:#liter to:#floz |
|
363 | 702 |
" |
703 |
||
704 |
"Created: 31.5.1996 / 16:23:38 / cg" |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
705 |
"Modified: 6.8.1997 / 18:10:22 / cg" |
363 | 706 |
! |
707 |
||
708 |
inchToMillimeter:inches |
|
364 | 709 |
"convert inches to mm. Made this an extra method, since its so common." |
363 | 710 |
|
711 |
^ inches * 25.4 |
|
712 |
||
713 |
"Created: 31.5.1996 / 13:37:31 / cg" |
|
364 | 714 |
"Modified: 31.5.1996 / 18:08:14 / cg" |
363 | 715 |
! |
716 |
||
717 |
millimeterToInch:mm |
|
364 | 718 |
"convert mm to inches. Made this an extra method, since its so common." |
363 | 719 |
|
720 |
^ mm / 25.4 |
|
721 |
||
722 |
"Created: 31.5.1996 / 13:37:48 / cg" |
|
364 | 723 |
"Modified: 31.5.1996 / 18:08:20 / cg" |
363 | 724 |
! ! |
725 |
||
516 | 726 |
!UnitConverter class methodsFor:'missing conversion'! |
727 |
||
728 |
noConversionFrom:sourceUnit to:destUnit |
|
729 |
Transcript showCR:'no conversion ' , sourceUnit printString , ' -> ' , destUnit printString. |
|
730 |
^ nil |
|
731 |
||
732 |
"Created: 29.3.1997 / 17:56:03 / cg" |
|
733 |
! ! |
|
734 |
||
363 | 735 |
!UnitConverter class methodsFor:'private'! |
736 |
||
737 |
addConversion:factor from:sourceMetric to:destMetric |
|
738 |
"add a conversion" |
|
739 |
||
740 |
|conversion| |
|
741 |
||
742 |
conversion := Conversions at:sourceMetric ifAbsent:nil. |
|
743 |
conversion isNil ifTrue:[ |
|
744 |
conversion := IdentityDictionary new. |
|
745 |
Conversions at:sourceMetric put:conversion |
|
746 |
]. |
|
747 |
conversion at:destMetric put:factor. |
|
748 |
^ conversion |
|
749 |
||
750 |
"Created: 31.5.1996 / 13:51:25 / cg" |
|
515 | 751 |
"Modified: 29.3.1997 / 17:19:31 / cg" |
363 | 752 |
! |
753 |
||
754 |
convertDirect:howMany from:sourceMetric to:destMetric |
|
755 |
"given a value in sourceMetric (symbolic), try to convert it |
|
756 |
to destMetric (symbolic); |
|
757 |
Only direct conversions are tried; return nil, if the conversion is unknown." |
|
758 |
||
759 |
|conversion factor| |
|
760 |
||
550 | 761 |
Conversions isNil ifTrue:[ |
762 |
self initializeConversions |
|
763 |
]. |
|
764 |
||
363 | 765 |
conversion := Conversions at:sourceMetric ifAbsent:nil. |
766 |
conversion isNil ifTrue:[^ nil]. |
|
767 |
||
768 |
factor := conversion at:destMetric ifAbsent:nil. |
|
769 |
factor isNil ifTrue:[^ nil]. |
|
770 |
||
364 | 771 |
factor isNumber ifTrue:[ |
772 |
^ howMany * factor |
|
773 |
]. |
|
515 | 774 |
^ factor value:howMany |
363 | 775 |
|
776 |
" |
|
777 |
UnitConverter convertDirect:1 from:#inch to:#meter |
|
778 |
UnitConverter convertDirect:1 from:#inch to:#millimeter |
|
515 | 779 |
UnitConverter convertDirect:1 from:#degrees to:#fahrenheit |
363 | 780 |
" |
781 |
||
782 |
"Created: 31.5.1996 / 13:54:33 / cg" |
|
550 | 783 |
"Modified: 22.7.1997 / 13:57:52 / cg" |
363 | 784 |
! |
785 |
||
786 |
unscaled:what |
|
787 |
"given a unit, return the base and a factor as assoc, |
|
788 |
or nil if not found" |
|
789 |
||
550 | 790 |
Conversions isNil ifTrue:[ |
791 |
self initializeConversions |
|
792 |
]. |
|
793 |
||
363 | 794 |
Scaling keysAndValuesDo:[:name :factor | |
795 |
|rest| |
|
796 |
||
797 |
(what startsWith:name) ifTrue:[ |
|
798 |
rest := what copyFrom:(name size+1). |
|
799 |
rest := rest asSymbolIfInterned. |
|
800 |
rest notNil ifTrue:[ |
|
801 |
^ rest -> factor. |
|
802 |
] |
|
803 |
]. |
|
804 |
]. |
|
805 |
^ nil |
|
806 |
||
807 |
" |
|
808 |
UnitConverter unscaled:#millimeter |
|
809 |
UnitConverter unscaled:#nanometer |
|
810 |
UnitConverter unscaled:#kilometer |
|
811 |
UnitConverter unscaled:#fuzzymeter |
|
812 |
" |
|
813 |
||
550 | 814 |
"Modified: 22.7.1997 / 13:57:46 / cg" |
363 | 815 |
! ! |
816 |
||
817 |
!UnitConverter class methodsFor:'documentation'! |
|
818 |
||
819 |
version |
|
556 | 820 |
^ '$Header: /cvs/stx/stx/libbasic2/UnitConverter.st,v 1.12 1997-08-07 12:10:26 cg Exp $' |
363 | 821 |
! ! |