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