1944
|
1 |
"{ Package: 'goodies/Numeric-Infinite' }"
|
1894
|
2 |
|
265
|
3 |
" NAME infinity
|
303
|
4 |
AUTHOR manchester
|
|
5 |
FUNCTION Provides a class of infinities
|
|
6 |
ST-VERSION 2.2
|
|
7 |
PREREQUISITES
|
|
8 |
CONFLICTS
|
|
9 |
DISTRIBUTION world
|
|
10 |
VERSION 1
|
|
11 |
DATE 22 Jan 1989
|
265
|
12 |
SUMMARY
|
|
13 |
This is a set of changes that implements infinity in the Number hierarchy.
|
|
14 |
I obtained the original changes from the author of an article in comp.lang.smalltalk.
|
|
15 |
I have just installed it in my image and I have found two small omissions
|
|
16 |
which are corrected in what is below; there might be others. Arithmetic
|
|
17 |
between infinities is not defined but magnitude comparisons are implemented.
|
|
18 |
|
|
19 |
Claus: fixed some minor bugs (args to errorUndefinedResult:) and some
|
|
20 |
wrong comments.
|
|
21 |
Changed retry:coercing: to match ST/X
|
|
22 |
"!
|
|
23 |
|
|
24 |
!Point methodsFor: 'testing'!
|
|
25 |
|
|
26 |
isFinite
|
303
|
27 |
^x isFinite and: [y isFinite]!
|
265
|
28 |
|
|
29 |
isInfinite
|
303
|
30 |
^x isInfinite or: [y isInfinite]! !
|
265
|
31 |
|
|
32 |
!Number methodsFor: 'testing'!
|
|
33 |
|
|
34 |
isFinite
|
303
|
35 |
^true!
|
265
|
36 |
|
|
37 |
isInfinite
|
303
|
38 |
^false! !
|
265
|
39 |
|
|
40 |
!Number methodsFor: 'coercing'!
|
|
41 |
|
|
42 |
retry: aSymbol coercing: aNumber
|
|
43 |
"Arithmetic represented by the symbol, aSymbol,
|
|
44 |
could not be performed with the receiver and the argument,
|
|
45 |
aNumber, because of the differences in representation. Coerce either
|
|
46 |
the receiver or the argument, depending on which has higher generality, and
|
|
47 |
try again. If the symbol is the equals sign, answer false if the argument
|
|
48 |
is not a Number. If the generalities are the same, create an error message."
|
|
49 |
|
|
50 |
|myGenerality otherGenerality|
|
|
51 |
|
|
52 |
(aSymbol == #=) ifTrue:[
|
303
|
53 |
(aNumber respondsTo:#generality) ifFalse:[^ false]
|
265
|
54 |
] ifFalse:[
|
303
|
55 |
(aNumber respondsTo:#generality) ifFalse:[
|
|
56 |
self error:'retry:coercing: argument is not a number'.
|
|
57 |
^ self
|
|
58 |
]
|
265
|
59 |
].
|
|
60 |
myGenerality := self generality.
|
|
61 |
otherGenerality := aNumber generality.
|
|
62 |
(myGenerality > otherGenerality) ifTrue:[
|
303
|
63 |
^ self perform:aSymbol with:(self coerce:aNumber)
|
265
|
64 |
].
|
|
65 |
(myGenerality < otherGenerality) ifTrue:[
|
303
|
66 |
aNumber isInfinite ifTrue: [
|
|
67 |
^ aNumber retryReverseOf:aSymbol with:self
|
|
68 |
].
|
|
69 |
^ (aNumber coerce:self) perform:aSymbol with:aNumber
|
265
|
70 |
].
|
|
71 |
self error:'retry:coercing: oops - same generality'
|
|
72 |
! !
|
|
73 |
|
|
74 |
Number subclass: #Infinity
|
303
|
75 |
instanceVariableNames: 'positive '
|
|
76 |
classVariableNames: 'NegativeInfinity PositiveInfinity '
|
|
77 |
poolDictionaries: ''
|
|
78 |
category: 'Magnitude-Numbers'!
|
265
|
79 |
|
|
80 |
Infinity comment:
|
|
81 |
'I have two instances representing positive and negative infinity.
|
|
82 |
|
|
83 |
Instance Variables :-
|
303
|
84 |
positive <Boolean> : if true the instance represents positive
|
|
85 |
infinity. if false, negative infinity'
|
265
|
86 |
!
|
|
87 |
|
|
88 |
!Infinity methodsFor: 'arithmetic'!
|
|
89 |
|
|
90 |
* aNumber
|
303
|
91 |
"Multiply the receiver by the argument and answer with the result."
|
265
|
92 |
|
303
|
93 |
aNumber isInfinite ifTrue: [
|
|
94 |
self errorUndefinedResult: #*
|
|
95 |
].
|
|
96 |
^self
|
265
|
97 |
!
|
|
98 |
|
|
99 |
+ aNumber
|
303
|
100 |
"Add the receiver by the argument and answer with the result."
|
265
|
101 |
|
303
|
102 |
(aNumber isInfinite and: [aNumber ~~ self]) ifTrue: [
|
|
103 |
self errorUndefinedResult: #+
|
|
104 |
].
|
|
105 |
^self
|
265
|
106 |
!
|
|
107 |
|
|
108 |
- aNumber
|
303
|
109 |
"subtracet aNumber from the receiver answer with the result."
|
265
|
110 |
|
303
|
111 |
(aNumber isInfinite) ifTrue: [
|
|
112 |
self errorUndefinedResult: #-
|
|
113 |
].
|
|
114 |
^self
|
265
|
115 |
!
|
|
116 |
|
|
117 |
/ aNumber
|
303
|
118 |
"Divide the receiver by the argument and answer with the result."
|
265
|
119 |
|
303
|
120 |
(aNumber isInfinite or: [aNumber = 0]) ifTrue: [
|
|
121 |
self errorUndefinedResult: #/
|
|
122 |
].
|
|
123 |
^self
|
265
|
124 |
! !
|
|
125 |
|
|
126 |
!Infinity methodsFor: 'comparing'!
|
|
127 |
|
|
128 |
< aNumber
|
303
|
129 |
"Positive infinity is greater than any number than positive infinity.
|
|
130 |
Analogously, negative infinity is less than any other number other
|
|
131 |
than negative infinity"
|
265
|
132 |
|
303
|
133 |
aNumber == self ifTrue: [^false].
|
|
134 |
^ positive not!
|
265
|
135 |
|
|
136 |
= aNumber
|
303
|
137 |
^aNumber == self
|
265
|
138 |
! !
|
|
139 |
|
|
140 |
!Infinity methodsFor: 'testing'!
|
|
141 |
|
|
142 |
isFinite
|
303
|
143 |
^false
|
265
|
144 |
!
|
|
145 |
|
|
146 |
isInfinite
|
303
|
147 |
^true
|
265
|
148 |
! !
|
|
149 |
|
|
150 |
!Infinity methodsFor: 'coercing'!
|
|
151 |
|
|
152 |
generality
|
303
|
153 |
"Infinities are more general than scalars, but not more general than
|
|
154 |
vectors (e.g. Points)"
|
|
155 |
^85
|
265
|
156 |
!
|
|
157 |
|
|
158 |
retryReverseOf: aSymbol with: aNumber
|
303
|
159 |
(aSymbol == #* or: [aSymbol == #+]) ifTrue: [
|
|
160 |
^self perform: aSymbol with: aNumber
|
|
161 |
].
|
|
162 |
(aSymbol == #/ and: [aNumber isFinite]) ifTrue: [^0].
|
|
163 |
(aSymbol == #< and: [aNumber isFinite]) ifTrue: [^positive].
|
|
164 |
(aSymbol == #> and: [ aNumber isFinite ]) ifTrue: [^positive not].
|
|
165 |
(aSymbol == #= and: [ aNumber isFinite ])ifTrue: [ ^false ].
|
|
166 |
self errorUndefinedResult: aSymbol
|
265
|
167 |
! !
|
|
168 |
|
|
169 |
!Infinity methodsFor: 'printing'!
|
|
170 |
|
|
171 |
printOn: aStream
|
303
|
172 |
aStream nextPutAll: self class name.
|
|
173 |
aStream nextPutAll:(positive
|
|
174 |
ifTrue: [' positive']
|
|
175 |
ifFalse: [' negative'])
|
265
|
176 |
! !
|
|
177 |
|
|
178 |
!Infinity methodsFor: 'errors'!
|
|
179 |
|
|
180 |
errorUndefinedResult: messageName
|
303
|
181 |
self error: 'Undefined result in an Infinity ', messageName
|
265
|
182 |
! !
|
|
183 |
|
|
184 |
!Infinity methodsFor: 'private'!
|
|
185 |
|
|
186 |
setPositive: aBoolean
|
303
|
187 |
positive := aBoolean
|
265
|
188 |
! !
|
|
189 |
|
|
190 |
!Infinity class methodsFor: 'class initialization'!
|
|
191 |
|
|
192 |
initialize
|
303
|
193 |
"Infinity initialize"
|
265
|
194 |
|
303
|
195 |
PositiveInfinity := self basicNew setPositive: true.
|
|
196 |
NegativeInfinity := self basicNew setPositive: false
|
265
|
197 |
! !
|
|
198 |
|
|
199 |
!Infinity class methodsFor: 'instance creation'!
|
|
200 |
|
|
201 |
negative
|
303
|
202 |
"Return the unique instance of negative infinity"
|
265
|
203 |
|
303
|
204 |
^NegativeInfinity
|
265
|
205 |
!
|
|
206 |
|
|
207 |
new
|
303
|
208 |
self shouldNotImplement
|
265
|
209 |
!
|
|
210 |
|
|
211 |
positive
|
303
|
212 |
"Return the unique instance of positive infinity"
|
265
|
213 |
|
303
|
214 |
^PositiveInfinity
|
265
|
215 |
! !
|
|
216 |
|
|
217 |
Infinity initialize!
|
|
218 |
|
|
219 |
|
|
220 |
"COPYRIGHT.
|
|
221 |
The above file is a Manchester Goodie protected by copyright.
|
|
222 |
These conditions are imposed on the whole Goodie, and on any significant
|
|
223 |
part of it which is separately transmitted or stored:
|
303
|
224 |
* You must ensure that every copy includes this notice, and that
|
|
225 |
source and author(s) of the material are acknowledged.
|
|
226 |
* These conditions must be imposed on anyone who receives a copy.
|
|
227 |
* The material shall not be used for commercial gain without the prior
|
|
228 |
written consent of the author(s).
|
265
|
229 |
Further information on the copyright conditions may be obtained by
|
|
230 |
sending electronic mail:
|
303
|
231 |
To: goodies-lib@cs.man.ac.uk
|
|
232 |
Subject: copyright
|
265
|
233 |
or by writing to The Smalltalk Goodies Library Manager, Dept of
|
|
234 |
Computer Science, The University, Manchester M13 9PL, UK
|
|
235 |
|
|
236 |
(C) Copyright 1992 University of Manchester
|
|
237 |
For more information about the Manchester Goodies Library (from which
|
|
238 |
this file was distributed) send e-mail:
|
303
|
239 |
To: goodies-lib@cs.man.ac.uk
|
|
240 |
Subject: help
|
265
|
241 |
"!
|