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