24955
|
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
|
24955
|
137 |
|
|
138 |
"
|
|
139 |
Infinity negative negated
|
|
140 |
Infinity negative abs
|
|
141 |
Infinity positive = Float infinity
|
|
142 |
Infinity negative = Float negativeInfinity
|
|
143 |
Infinity negative = Float infinity
|
|
144 |
"
|
265
|
145 |
!
|
|
146 |
|
16037
|
147 |
negative:aBoolean
|
|
148 |
"Return either instance of negative infinity"
|
|
149 |
|
|
150 |
^ aBoolean ifTrue:[InfNeg] ifFalse:[InfPos].
|
|
151 |
!
|
|
152 |
|
7443
|
153 |
new
|
16037
|
154 |
"only my two singleton instances are allowed;
|
|
155 |
get either via Infinity positive or Infinity negative"
|
|
156 |
|
7443
|
157 |
self shouldNotImplement
|
265
|
158 |
!
|
|
159 |
|
7443
|
160 |
positive
|
|
161 |
"Return the unique instance of positive infinity"
|
265
|
162 |
|
7548
|
163 |
^InfPos
|
265
|
164 |
! !
|
|
165 |
|
7443
|
166 |
!Infinity class methodsFor:'class initialization'!
|
265
|
167 |
|
|
168 |
initialize
|
16037
|
169 |
"initialize my two singleton instances"
|
|
170 |
|
7548
|
171 |
InfPos := PositiveInfinity basicNew.
|
|
172 |
InfNeg := NegativeInfinity basicNew.
|
265
|
173 |
|
7548
|
174 |
"
|
|
175 |
Infinity initialize
|
|
176 |
"
|
265
|
177 |
! !
|
|
178 |
|
18917
|
179 |
!Infinity class methodsFor:'queries'!
|
|
180 |
|
|
181 |
isAbstract
|
|
182 |
^ self == Infinity
|
|
183 |
! !
|
|
184 |
|
7443
|
185 |
!Infinity methodsFor:'arithmetic'!
|
|
186 |
|
|
187 |
* aNumber
|
|
188 |
"Multiply the receiver by the argument and answer with the result."
|
|
189 |
|
|
190 |
aNumber isInfinite ifTrue: [
|
7452
|
191 |
self errorUndefinedResult: #*.
|
|
192 |
^ self class NaN.
|
7443
|
193 |
].
|
|
194 |
^self
|
|
195 |
!
|
|
196 |
|
|
197 |
+ aNumber
|
|
198 |
"Add the receiver and the argument and answer with the result."
|
|
199 |
|
|
200 |
(aNumber isInfinite and:[aNumber sign ~~ self sign]) ifTrue: [
|
7452
|
201 |
self errorUndefinedResult: #+.
|
|
202 |
^ self class NaN
|
7443
|
203 |
].
|
|
204 |
^self
|
|
205 |
!
|
|
206 |
|
|
207 |
- aNumber
|
21294
|
208 |
"Subtract aNumber from the receiver and answer the result."
|
7443
|
209 |
|
|
210 |
(aNumber isInfinite and:[aNumber sign == self sign]) ifTrue: [
|
7469
|
211 |
self errorUndefinedResult: #-.
|
|
212 |
^ self class NaN
|
7443
|
213 |
].
|
|
214 |
^self
|
|
215 |
!
|
|
216 |
|
|
217 |
/ aNumber
|
|
218 |
"Divide the receiver by the argument and answer the result."
|
265
|
219 |
|
7443
|
220 |
(aNumber isInfinite or: [aNumber = 0]) ifTrue: [
|
7452
|
221 |
self errorUndefinedResult: #/.
|
|
222 |
^ self class NaN.
|
7443
|
223 |
].
|
|
224 |
^self
|
|
225 |
! !
|
|
226 |
|
|
227 |
!Infinity methodsFor:'coercing'!
|
|
228 |
|
|
229 |
generality
|
|
230 |
"Infinities are more general than scalars, but not more general than
|
|
231 |
vectors (e.g. Points)"
|
|
232 |
|
|
233 |
^ 105
|
|
234 |
! !
|
|
235 |
|
|
236 |
!Infinity methodsFor:'comparing'!
|
|
237 |
|
|
238 |
< aNumber
|
7548
|
239 |
"Positive infinity is greater than any number other than positive infinity.
|
7443
|
240 |
Analogously, negative infinity is less than any other number other
|
|
241 |
than negative infinity"
|
|
242 |
|
7548
|
243 |
aNumber == self ifTrue: [^false].
|
7469
|
244 |
^ self positive not
|
7548
|
245 |
|
|
246 |
"
|
|
247 |
Infinity positive < 0
|
|
248 |
Infinity positive < 1000
|
|
249 |
Infinity positive < -1000
|
|
250 |
|
|
251 |
Infinity positive < Infinity positive
|
|
252 |
Infinity positive < Infinity negative
|
|
253 |
|
|
254 |
0 < Infinity positive
|
|
255 |
1000 < Infinity positive
|
|
256 |
-1000 < Infinity positive
|
|
257 |
Infinity negative < Infinity positive
|
|
258 |
|
|
259 |
Infinity negative < 0
|
|
260 |
Infinity negative < 1000
|
|
261 |
Infinity negative < -1000
|
|
262 |
|
|
263 |
Infinity negative < Infinity negative
|
|
264 |
Infinity negative < Infinity positive
|
|
265 |
|
|
266 |
0 < Infinity negative
|
|
267 |
1000 < Infinity negative
|
|
268 |
-1000 < Infinity negative
|
|
269 |
Infinity negative < Infinity positive
|
|
270 |
"
|
265
|
271 |
!
|
|
272 |
|
7443
|
273 |
= aNumber
|
7471
|
274 |
"return true, if the argument represents the same numeric value
|
|
275 |
as the receiver, false otherwise."
|
|
276 |
|
7548
|
277 |
aNumber == self ifTrue:[^ true].
|
12246
|
278 |
aNumber isNumber ifFalse:[^ false].
|
7548
|
279 |
"could be another infinity..."
|
|
280 |
aNumber isFinite ifTrue:[^ false].
|
|
281 |
^ aNumber sign == self sign
|
|
282 |
!
|
|
283 |
|
|
284 |
> aNumber
|
|
285 |
"Positive infinity is greater than any number other than positive infinity.
|
|
286 |
Analogously, negative infinity is less than any other number other
|
|
287 |
than negative infinity"
|
|
288 |
|
|
289 |
aNumber == self ifTrue: [^false].
|
|
290 |
^ self positive
|
|
291 |
|
|
292 |
"
|
|
293 |
Infinity positive > 0
|
|
294 |
Infinity positive > 1000
|
|
295 |
Infinity positive > -1000
|
|
296 |
|
|
297 |
Infinity positive > Infinity positive
|
|
298 |
Infinity positive > Infinity negative
|
|
299 |
|
|
300 |
0 > Infinity positive
|
|
301 |
1000 > Infinity positive
|
|
302 |
-1000 > Infinity positive
|
|
303 |
Infinity negative > Infinity positive
|
|
304 |
|
|
305 |
Infinity negative > 0
|
|
306 |
Infinity negative > 1000
|
|
307 |
Infinity negative > -1000
|
|
308 |
|
|
309 |
Infinity negative > Infinity negative
|
|
310 |
Infinity negative > Infinity positive
|
|
311 |
|
|
312 |
0 > Infinity negative
|
|
313 |
1000 > Infinity negative
|
|
314 |
-1000 > Infinity negative
|
|
315 |
Infinity negative > Infinity positive
|
|
316 |
"
|
7443
|
317 |
! !
|
|
318 |
|
|
319 |
!Infinity methodsFor:'double dispatching'!
|
|
320 |
|
|
321 |
differenceFromSomeNumber:aNumber
|
7469
|
322 |
"Sent from aNumber-self, if aNumber does not know how to handle this"
|
7443
|
323 |
|
|
324 |
^ self negated
|
|
325 |
!
|
|
326 |
|
21213
|
327 |
equalFromSomeNumber:aNumber
|
|
328 |
"Sent from aNumber = self, if aNumber does not know how to handle this.
|
|
329 |
Return true if aNumber = self."
|
|
330 |
|
|
331 |
^ aNumber isInfinite and:[self sign == aNumber sign]
|
|
332 |
!
|
|
333 |
|
7443
|
334 |
lessFromSomeNumber:aNumber
|
18856
|
335 |
"Sent from aNumber < self, if aNumber does not know how to handle this.
|
|
336 |
Return true if aNumber < self."
|
7548
|
337 |
|
|
338 |
aNumber isFinite ifTrue:[
|
|
339 |
^ self positive.
|
|
340 |
].
|
|
341 |
|
7443
|
342 |
^ Number
|
7469
|
343 |
raise: #undefinedResultSignal
|
|
344 |
receiver: self
|
|
345 |
selector: #lessFromSomeNumber:
|
|
346 |
errorString: 'Cannot compare against NaN'
|
7443
|
347 |
!
|
|
348 |
|
|
349 |
productFromSomeNumber:aNumber
|
7469
|
350 |
"Sent from aNumber*self, if aNumber does not know how to handle this"
|
7443
|
351 |
|
|
352 |
aNumber sign >= 0 ifTrue:[
|
7469
|
353 |
^ self
|
7443
|
354 |
].
|
|
355 |
^ self negated
|
|
356 |
!
|
|
357 |
|
|
358 |
quotientFromSomeNumber:aNumber
|
|
359 |
"Return the quotient of the argument, aNumber and the receiver.
|
|
360 |
Sent when aNumber does not know how to divide by the receiver."
|
|
361 |
|
|
362 |
aNumber sign > 0 ifTrue:[
|
7452
|
363 |
^ self
|
7443
|
364 |
].
|
|
365 |
aNumber sign < 0 ifTrue:[
|
7452
|
366 |
^ self negated
|
7443
|
367 |
].
|
|
368 |
^ aNumber realNumber
|
265
|
369 |
!
|
|
370 |
|
7443
|
371 |
sumFromSomeNumber:aNumber
|
7469
|
372 |
"Sent from aNumber+self, if aNumber does not know how to handle this"
|
7443
|
373 |
|
|
374 |
^ self
|
|
375 |
! !
|
|
376 |
|
|
377 |
!Infinity methodsFor:'errors'!
|
|
378 |
|
|
379 |
errorUndefinedResult: messageName
|
|
380 |
^ Number
|
7452
|
381 |
raise: #undefinedResultSignal
|
|
382 |
receiver: self
|
|
383 |
selector: #lessFromSomeNumber:
|
|
384 |
errorString: 'Undefined result in an Infinity ', messageName
|
7443
|
385 |
! !
|
|
386 |
|
|
387 |
!Infinity methodsFor:'testing'!
|
|
388 |
|
|
389 |
isFinite
|
24708
|
390 |
"return true, if the receiver is a finite float (not NaN and not +/-INF)"
|
|
391 |
|
7443
|
392 |
^false
|
|
393 |
!
|
|
394 |
|
|
395 |
isInfinite
|
24708
|
396 |
"return true, if the receiver is an infinite number (+Inf or -Inf)"
|
|
397 |
|
7443
|
398 |
^true
|
7469
|
399 |
! !
|
|
400 |
|
|
401 |
!Infinity::NegativeInfinity methodsFor:'arithmetic'!
|
|
402 |
|
|
403 |
negated
|
|
404 |
^ InfPos
|
|
405 |
! !
|
|
406 |
|
16037
|
407 |
!Infinity::NegativeInfinity methodsFor:'comparing'!
|
|
408 |
|
|
409 |
hash
|
|
410 |
"return an Integer useful as a hash key for the receiver."
|
|
411 |
|
|
412 |
^ 17467 "/ any arbitrary value
|
|
413 |
! !
|
|
414 |
|
7469
|
415 |
!Infinity::NegativeInfinity methodsFor:'printing'!
|
|
416 |
|
|
417 |
printOn: aStream
|
|
418 |
aStream nextPutAll:'-INF'
|
|
419 |
! !
|
|
420 |
|
|
421 |
!Infinity::NegativeInfinity methodsFor:'testing'!
|
7443
|
422 |
|
24955
|
423 |
negative
|
|
424 |
^ true
|
|
425 |
!
|
|
426 |
|
|
427 |
positive
|
|
428 |
"return true, if the receiver is greater or equal to zero (not negative)"
|
|
429 |
|
|
430 |
^ false
|
|
431 |
|
|
432 |
"Created: / 21-06-2017 / 14:02:18 / cg"
|
|
433 |
"Modified (comment): / 28-05-2019 / 05:55:45 / Claus Gittinger"
|
|
434 |
!
|
|
435 |
|
7443
|
436 |
sign
|
7471
|
437 |
"return the sign of the receiver (1)"
|
7443
|
438 |
|
|
439 |
^ -1
|
|
440 |
! !
|
|
441 |
|
7469
|
442 |
!Infinity::PositiveInfinity methodsFor:'arithmetic'!
|
|
443 |
|
|
444 |
negated
|
|
445 |
^ InfNeg
|
|
446 |
! !
|
|
447 |
|
16037
|
448 |
!Infinity::PositiveInfinity methodsFor:'comparing'!
|
|
449 |
|
|
450 |
hash
|
|
451 |
"return an Integer useful as a hash key for the receiver."
|
|
452 |
|
|
453 |
^ 17471 "/ any arbitrary value
|
|
454 |
! !
|
|
455 |
|
7469
|
456 |
!Infinity::PositiveInfinity methodsFor:'printing'!
|
|
457 |
|
|
458 |
printOn: aStream
|
|
459 |
aStream nextPutAll:'INF'
|
|
460 |
! !
|
|
461 |
|
|
462 |
!Infinity::PositiveInfinity methodsFor:'testing'!
|
|
463 |
|
24955
|
464 |
negative
|
|
465 |
^ false
|
|
466 |
!
|
|
467 |
|
|
468 |
positive
|
|
469 |
"return true, if the receiver is greater or equal to zero (not negative)"
|
|
470 |
|
|
471 |
^ true
|
|
472 |
|
|
473 |
"Created: / 21-06-2017 / 14:02:28 / cg"
|
|
474 |
"Modified (comment): / 28-05-2019 / 05:55:48 / Claus Gittinger"
|
|
475 |
!
|
|
476 |
|
7469
|
477 |
sign
|
7471
|
478 |
"return the sign of the receiver (-1)"
|
7469
|
479 |
|
|
480 |
^ 1
|
|
481 |
! !
|
|
482 |
|
7443
|
483 |
!Infinity class methodsFor:'documentation'!
|
|
484 |
|
|
485 |
version
|
18856
|
486 |
^ '$Header$'
|
12246
|
487 |
!
|
|
488 |
|
|
489 |
version_CVS
|
18856
|
490 |
^ '$Header$'
|
265
|
491 |
! !
|
|
492 |
|
15404
|
493 |
|
265
|
494 |
Infinity initialize!
|