author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Sat, 24 Oct 2009 16:48:19 +0100 | |
branch | jv |
changeset 17732 | a1892eeca6c0 |
parent 17711 | 39faaaf888b4 |
child 17734 | 406b1590afe8 |
permissions | -rw-r--r-- |
7443 | 1 |
" |
2 |
This is a Manchester Goodie. It is distributed freely on condition |
|
3 |
that you observe these conditions in respect of the whole Goodie, and on |
|
4 |
any significant part of it which is separately transmitted or stored: |
|
5 |
* You must ensure that every copy includes this notice, and that |
|
6 |
source and author(s) of the material are acknowledged. |
|
7 |
* These conditions must be imposed on anyone who receives a copy. |
|
8 |
* The material shall not be used for commercial gain without the prior |
|
9 |
written consent of the author(s). |
|
10 |
||
11 |
For more information about the Manchester Goodies Library (from which |
|
12 |
this file was distributed) send e-mail: |
|
13 |
To: goodies-lib@cs.man.ac.uk |
|
14 |
Subject: help |
|
15 |
||
16 |
This is an additional goody-class, which is NOT covered by the |
|
17 |
ST/X license. It has been packaged with the ST/X distribution to |
|
18 |
make your live easier instead. NO WARRANTY. |
|
19 |
" |
|
7452 | 20 |
"{ Package: 'stx:libbasic' }" |
1894 | 21 |
|
7443 | 22 |
MetaNumber subclass:#Infinity |
7548 | 23 |
instanceVariableNames:'' |
7469 | 24 |
classVariableNames:'InfNeg InfPos' |
7443 | 25 |
poolDictionaries:'' |
26 |
category:'Magnitude-Numbers' |
|
27 |
! |
|
265 | 28 |
|
7469 | 29 |
Infinity subclass:#NegativeInfinity |
30 |
instanceVariableNames:'' |
|
31 |
classVariableNames:'' |
|
32 |
poolDictionaries:'' |
|
33 |
privateIn:Infinity |
|
34 |
! |
|
35 |
||
36 |
Infinity subclass:#PositiveInfinity |
|
37 |
instanceVariableNames:'' |
|
38 |
classVariableNames:'' |
|
39 |
poolDictionaries:'' |
|
40 |
privateIn:Infinity |
|
41 |
! |
|
42 |
||
7443 | 43 |
Infinity comment:'I have two instances representing positive and negative infinity. |
265 | 44 |
|
45 |
Instance Variables :- |
|
303 | 46 |
positive <Boolean> : if true the instance represents positive |
47 |
infinity. if false, negative infinity' |
|
265 | 48 |
! |
49 |
||
7443 | 50 |
!Infinity class methodsFor:'documentation'! |
6501 | 51 |
|
52 |
copyright |
|
53 |
" |
|
54 |
This is a Manchester Goodie. It is distributed freely on condition |
|
55 |
that you observe these conditions in respect of the whole Goodie, and on |
|
56 |
any significant part of it which is separately transmitted or stored: |
|
57 |
* You must ensure that every copy includes this notice, and that |
|
58 |
source and author(s) of the material are acknowledged. |
|
59 |
* These conditions must be imposed on anyone who receives a copy. |
|
60 |
* The material shall not be used for commercial gain without the prior |
|
61 |
written consent of the author(s). |
|
62 |
||
63 |
For more information about the Manchester Goodies Library (from which |
|
64 |
this file was distributed) send e-mail: |
|
65 |
To: goodies-lib@cs.man.ac.uk |
|
66 |
Subject: help |
|
67 |
||
68 |
This is an additional goody-class, which is NOT covered by the |
|
69 |
ST/X license. It has been packaged with the ST/X distribution to |
|
70 |
make your live easier instead. NO WARRANTY. |
|
71 |
" |
|
72 |
! |
|
73 |
||
74 |
documentation |
|
75 |
" |
|
7469 | 76 |
This is a set of changes that adds infinity in the Number hierarchy. |
77 |
I have two instances representing positive and negative infinity. |
|
6501 | 78 |
|
79 |
Claus: fixed some minor bugs (args to errorUndefinedResult:) and some wrong comments. |
|
7469 | 80 |
Changed retry:coercing: to match ST/X's way of doing this |
6501 | 81 |
|
82 |
Instance Variables :- |
|
7469 | 83 |
positive <Boolean> : if true the instance represents positive |
84 |
infinity. if false, negative infinity |
|
6501 | 85 |
" |
86 |
! |
|
87 |
||
88 |
examples |
|
89 |
" |
|
90 |
||
7548 | 91 |
1 + Infinity positive |
6501 | 92 |
Infinity positive + 1 |
93 |
Infinity positive + Infinity positive |
|
94 |
||
7443 | 95 |
Infinity negative - 1 |
96 |
Infinity negative + Infinity negative |
|
97 |
Infinity negative + Infinity negative |
|
6501 | 98 |
|
7548 | 99 |
Infinity negative negated |
100 |
Infinity positive negated |
|
7443 | 101 |
|
7548 | 102 |
Infinity positive > Infinity negative |
103 |
Infinity negative > Infinity positive |
|
104 |
||
105 |
Infinity negative + Infinity positive -> raises an error |
|
106 |
Infinity negative - Infinity negative -> raises an error |
|
107 |
||
6501 | 108 |
" |
109 |
! |
|
110 |
||
111 |
info |
|
112 |
" |
|
7548 | 113 |
NAME infinity |
114 |
AUTHOR manchester |
|
115 |
FUNCTION Provides a class of infinities |
|
116 |
ST-VERSION 2.2 |
|
117 |
PREREQUISITES |
|
118 |
CONFLICTS |
|
119 |
DISTRIBUTION world |
|
120 |
VERSION 1 |
|
121 |
DATE 22 Jan 1989 |
|
122 |
SUMMARY |
|
123 |
This is a set of changes that implements infinity in the Number hierarchy. |
|
124 |
I obtained the original changes from the author of an article in comp.lang.smalltalk. |
|
125 |
I have just installed it in my image and I have found two small omissions |
|
126 |
which are corrected in what is below; there might be others. Arithmetic |
|
127 |
between infinities is not defined but magnitude comparisons are implemented. |
|
6501 | 128 |
" |
129 |
! ! |
|
130 |
||
7443 | 131 |
!Infinity class methodsFor:'instance creation'! |
265 | 132 |
|
7443 | 133 |
negative |
134 |
"Return the unique instance of negative infinity" |
|
265 | 135 |
|
7548 | 136 |
^InfNeg |
265 | 137 |
! |
138 |
||
7443 | 139 |
new |
140 |
self shouldNotImplement |
|
265 | 141 |
! |
142 |
||
7443 | 143 |
positive |
144 |
"Return the unique instance of positive infinity" |
|
265 | 145 |
|
7548 | 146 |
^InfPos |
265 | 147 |
! ! |
148 |
||
7443 | 149 |
!Infinity class methodsFor:'class initialization'! |
265 | 150 |
|
151 |
initialize |
|
7548 | 152 |
InfPos := PositiveInfinity basicNew. |
153 |
InfNeg := NegativeInfinity basicNew. |
|
265 | 154 |
|
7548 | 155 |
" |
156 |
Infinity initialize |
|
157 |
" |
|
265 | 158 |
! ! |
159 |
||
7443 | 160 |
!Infinity methodsFor:'arithmetic'! |
161 |
||
162 |
* aNumber |
|
163 |
"Multiply the receiver by the argument and answer with the result." |
|
164 |
||
165 |
aNumber isInfinite ifTrue: [ |
|
7452 | 166 |
self errorUndefinedResult: #*. |
167 |
^ self class NaN. |
|
7443 | 168 |
]. |
169 |
^self |
|
170 |
! |
|
171 |
||
172 |
+ aNumber |
|
173 |
"Add the receiver and the argument and answer with the result." |
|
174 |
||
175 |
(aNumber isInfinite and:[aNumber sign ~~ self sign]) ifTrue: [ |
|
7452 | 176 |
self errorUndefinedResult: #+. |
177 |
^ self class NaN |
|
7443 | 178 |
]. |
179 |
^self |
|
180 |
! |
|
181 |
||
182 |
- aNumber |
|
7469 | 183 |
"Subtracet aNumber from the receiver and answer the result." |
7443 | 184 |
|
185 |
(aNumber isInfinite and:[aNumber sign == self sign]) ifTrue: [ |
|
7469 | 186 |
self errorUndefinedResult: #-. |
187 |
^ self class NaN |
|
7443 | 188 |
]. |
189 |
^self |
|
190 |
! |
|
191 |
||
192 |
/ aNumber |
|
193 |
"Divide the receiver by the argument and answer the result." |
|
265 | 194 |
|
7443 | 195 |
(aNumber isInfinite or: [aNumber = 0]) ifTrue: [ |
7452 | 196 |
self errorUndefinedResult: #/. |
197 |
^ self class NaN. |
|
7443 | 198 |
]. |
199 |
^self |
|
200 |
! ! |
|
201 |
||
202 |
!Infinity methodsFor:'coercing'! |
|
203 |
||
204 |
generality |
|
205 |
"Infinities are more general than scalars, but not more general than |
|
206 |
vectors (e.g. Points)" |
|
207 |
||
208 |
^ 105 |
|
209 |
! |
|
265 | 210 |
|
7443 | 211 |
retryReverseOf: aSymbol with: aNumber |
7469 | 212 |
self obsoleteMethodWarning:'leftover - no longer used'. |
213 |
||
7443 | 214 |
(aSymbol == #* or: [aSymbol == #+]) ifTrue: [ |
7469 | 215 |
^self perform: aSymbol with: aNumber |
7443 | 216 |
]. |
7469 | 217 |
(aSymbol == #/ and: [aNumber isFinite]) ifTrue: [^ 0]. |
218 |
(aSymbol == #< and: [aNumber isFinite]) ifTrue: [^ self positive]. |
|
219 |
(aSymbol == #> and: [ aNumber isFinite ]) ifTrue: [^ self positive not]. |
|
220 |
(aSymbol == #= and: [ aNumber isFinite ])ifTrue: [ ^ false ]. |
|
7443 | 221 |
self errorUndefinedResult: aSymbol |
222 |
! ! |
|
223 |
||
224 |
!Infinity methodsFor:'comparing'! |
|
225 |
||
226 |
< aNumber |
|
7548 | 227 |
"Positive infinity is greater than any number other than positive infinity. |
7443 | 228 |
Analogously, negative infinity is less than any other number other |
229 |
than negative infinity" |
|
230 |
||
7548 | 231 |
aNumber == self ifTrue: [^false]. |
7469 | 232 |
^ self positive not |
7548 | 233 |
|
234 |
" |
|
235 |
Infinity positive < 0 |
|
236 |
Infinity positive < 1000 |
|
237 |
Infinity positive < -1000 |
|
238 |
||
239 |
Infinity positive < Infinity positive |
|
240 |
Infinity positive < Infinity negative |
|
241 |
||
242 |
0 < Infinity positive |
|
243 |
1000 < Infinity positive |
|
244 |
-1000 < Infinity positive |
|
245 |
Infinity negative < Infinity positive |
|
246 |
||
247 |
Infinity negative < 0 |
|
248 |
Infinity negative < 1000 |
|
249 |
Infinity negative < -1000 |
|
250 |
||
251 |
Infinity negative < Infinity negative |
|
252 |
Infinity negative < Infinity positive |
|
253 |
||
254 |
0 < Infinity negative |
|
255 |
1000 < Infinity negative |
|
256 |
-1000 < Infinity negative |
|
257 |
Infinity negative < Infinity positive |
|
258 |
" |
|
265 | 259 |
! |
260 |
||
7443 | 261 |
= aNumber |
7471 | 262 |
"return true, if the argument represents the same numeric value |
263 |
as the receiver, false otherwise." |
|
264 |
||
7548 | 265 |
aNumber == self ifTrue:[^ true]. |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
266 |
aNumber isNumber ifFalse:[^ false]. |
7548 | 267 |
"could be another infinity..." |
268 |
aNumber isFinite ifTrue:[^ false]. |
|
269 |
^ aNumber sign == self sign |
|
270 |
! |
|
271 |
||
272 |
> aNumber |
|
273 |
"Positive infinity is greater than any number other than positive infinity. |
|
274 |
Analogously, negative infinity is less than any other number other |
|
275 |
than negative infinity" |
|
276 |
||
277 |
aNumber == self ifTrue: [^false]. |
|
278 |
^ self positive |
|
279 |
||
280 |
" |
|
281 |
Infinity positive > 0 |
|
282 |
Infinity positive > 1000 |
|
283 |
Infinity positive > -1000 |
|
284 |
||
285 |
Infinity positive > Infinity positive |
|
286 |
Infinity positive > Infinity negative |
|
287 |
||
288 |
0 > Infinity positive |
|
289 |
1000 > Infinity positive |
|
290 |
-1000 > Infinity positive |
|
291 |
Infinity negative > Infinity positive |
|
292 |
||
293 |
Infinity negative > 0 |
|
294 |
Infinity negative > 1000 |
|
295 |
Infinity negative > -1000 |
|
296 |
||
297 |
Infinity negative > Infinity negative |
|
298 |
Infinity negative > Infinity positive |
|
299 |
||
300 |
0 > Infinity negative |
|
301 |
1000 > Infinity negative |
|
302 |
-1000 > Infinity negative |
|
303 |
Infinity negative > Infinity positive |
|
304 |
" |
|
7443 | 305 |
! ! |
306 |
||
307 |
!Infinity methodsFor:'double dispatching'! |
|
308 |
||
309 |
differenceFromSomeNumber:aNumber |
|
7469 | 310 |
"Sent from aNumber-self, if aNumber does not know how to handle this" |
7443 | 311 |
|
312 |
^ self negated |
|
313 |
! |
|
314 |
||
315 |
lessFromSomeNumber:aNumber |
|
7469 | 316 |
"Sent from aNumber < self, if aNumber does not know how to handle this" |
7548 | 317 |
|
318 |
aNumber isFinite ifTrue:[ |
|
319 |
^ self positive. |
|
320 |
]. |
|
321 |
||
7443 | 322 |
^ Number |
7469 | 323 |
raise: #undefinedResultSignal |
324 |
receiver: self |
|
325 |
selector: #lessFromSomeNumber: |
|
326 |
errorString: 'Cannot compare against NaN' |
|
7443 | 327 |
! |
328 |
||
329 |
productFromSomeNumber:aNumber |
|
7469 | 330 |
"Sent from aNumber*self, if aNumber does not know how to handle this" |
7443 | 331 |
|
332 |
aNumber sign >= 0 ifTrue:[ |
|
7469 | 333 |
^ self |
7443 | 334 |
]. |
335 |
^ self negated |
|
336 |
! |
|
337 |
||
338 |
quotientFromSomeNumber:aNumber |
|
339 |
"Return the quotient of the argument, aNumber and the receiver. |
|
340 |
Sent when aNumber does not know how to divide by the receiver." |
|
341 |
||
342 |
aNumber sign > 0 ifTrue:[ |
|
7452 | 343 |
^ self |
7443 | 344 |
]. |
345 |
aNumber sign < 0 ifTrue:[ |
|
7452 | 346 |
^ self negated |
7443 | 347 |
]. |
348 |
^ aNumber realNumber |
|
265 | 349 |
! |
350 |
||
7443 | 351 |
sumFromSomeNumber:aNumber |
7469 | 352 |
"Sent from aNumber+self, if aNumber does not know how to handle this" |
7443 | 353 |
|
354 |
^ self |
|
355 |
! ! |
|
356 |
||
357 |
!Infinity methodsFor:'errors'! |
|
358 |
||
359 |
errorUndefinedResult: messageName |
|
360 |
^ Number |
|
7452 | 361 |
raise: #undefinedResultSignal |
362 |
receiver: self |
|
363 |
selector: #lessFromSomeNumber: |
|
364 |
errorString: 'Undefined result in an Infinity ', messageName |
|
7443 | 365 |
! ! |
366 |
||
367 |
!Infinity methodsFor:'testing'! |
|
368 |
||
369 |
isFinite |
|
370 |
^false |
|
371 |
! |
|
372 |
||
373 |
isInfinite |
|
374 |
^true |
|
7469 | 375 |
! ! |
376 |
||
377 |
!Infinity::NegativeInfinity methodsFor:'arithmetic'! |
|
378 |
||
379 |
negated |
|
380 |
^ InfPos |
|
7470 | 381 |
! |
382 |
||
383 |
negative |
|
384 |
^ true |
|
7469 | 385 |
! ! |
386 |
||
387 |
!Infinity::NegativeInfinity methodsFor:'printing'! |
|
388 |
||
389 |
printOn: aStream |
|
390 |
aStream nextPutAll:'-INF' |
|
391 |
! ! |
|
392 |
||
393 |
!Infinity::NegativeInfinity methodsFor:'testing'! |
|
7443 | 394 |
|
395 |
sign |
|
7471 | 396 |
"return the sign of the receiver (1)" |
7443 | 397 |
|
398 |
^ -1 |
|
399 |
! ! |
|
400 |
||
7469 | 401 |
!Infinity::PositiveInfinity methodsFor:'arithmetic'! |
402 |
||
403 |
negated |
|
404 |
^ InfNeg |
|
7470 | 405 |
! |
406 |
||
407 |
negative |
|
408 |
^ false |
|
7469 | 409 |
! ! |
410 |
||
411 |
!Infinity::PositiveInfinity methodsFor:'printing'! |
|
412 |
||
413 |
printOn: aStream |
|
414 |
aStream nextPutAll:'INF' |
|
415 |
! ! |
|
416 |
||
417 |
!Infinity::PositiveInfinity methodsFor:'testing'! |
|
418 |
||
419 |
sign |
|
7471 | 420 |
"return the sign of the receiver (-1)" |
7469 | 421 |
|
422 |
^ 1 |
|
423 |
! ! |
|
424 |
||
7443 | 425 |
!Infinity class methodsFor:'documentation'! |
426 |
||
427 |
version |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
428 |
^ '$Id: Infinity.st 10473 2009-10-24 15:48:19Z vranyj1 $' |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
429 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
430 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
431 |
version_CVS |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
432 |
^ '§Header: /cvs/stx/stx/libbasic/Infinity.st,v 1.13 2009/10/12 19:34:30 cg Exp §' |
265 | 433 |
! ! |
434 |
||
435 |
Infinity initialize! |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
436 |