author | Claus Gittinger <cg@exept.de> |
Tue, 17 Jun 2003 11:12:22 +0200 | |
changeset 7385 | 852e84340d2b |
parent 6501 | 51344e42c38c |
child 7443 | 3407302dc4d9 |
permissions | -rw-r--r-- |
5287
b3b0d0e3ce98
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
1944
diff
changeset
|
1 |
"{ Package: 'stx:goodies' }" |
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 |
||
6501 | 88 |
!Infinity class methodsFor: 'documentation'! |
89 |
||
90 |
copyright |
|
91 |
" |
|
92 |
This is a Manchester Goodie. It is distributed freely on condition |
|
93 |
that you observe these conditions in respect of the whole Goodie, and on |
|
94 |
any significant part of it which is separately transmitted or stored: |
|
95 |
* You must ensure that every copy includes this notice, and that |
|
96 |
source and author(s) of the material are acknowledged. |
|
97 |
* These conditions must be imposed on anyone who receives a copy. |
|
98 |
* The material shall not be used for commercial gain without the prior |
|
99 |
written consent of the author(s). |
|
100 |
||
101 |
For more information about the Manchester Goodies Library (from which |
|
102 |
this file was distributed) send e-mail: |
|
103 |
To: goodies-lib@cs.man.ac.uk |
|
104 |
Subject: help |
|
105 |
||
106 |
This is an additional goody-class, which is NOT covered by the |
|
107 |
ST/X license. It has been packaged with the ST/X distribution to |
|
108 |
make your live easier instead. NO WARRANTY. |
|
109 |
" |
|
110 |
! |
|
111 |
||
112 |
documentation |
|
113 |
" |
|
114 |
This is a set of changes that implements infinity in the Number hierarchy. |
|
115 |
I obtained the original changes from the author of an article in comp.lang.smalltalk. |
|
116 |
Arithmetic between infinities is not defined but magnitude comparisons are implemented. |
|
117 |
||
118 |
Claus: fixed some minor bugs (args to errorUndefinedResult:) and some wrong comments. |
|
119 |
Changed retry:coercing: to match ST/X |
|
120 |
||
121 |
I have two instances representing positive and negative infinity. |
|
122 |
||
123 |
Instance Variables :- |
|
124 |
positive <Boolean> : if true the instance represents positive |
|
125 |
infinity. if false, negative infinity |
|
126 |
" |
|
127 |
! |
|
128 |
||
129 |
examples |
|
130 |
" |
|
131 |
||
132 |
1 + Infinity positive |
|
133 |
Infinity positive + 1 |
|
134 |
Infinity positive + Infinity positive |
|
135 |
||
136 |
Infinity negative - 1 |
|
137 |
Infinity negative + Infinity negative |
|
138 |
Infinity negative + Infinity negative |
|
139 |
||
140 |
" |
|
141 |
! |
|
142 |
||
143 |
info |
|
144 |
" |
|
145 |
NAME infinity |
|
146 |
AUTHOR manchester |
|
147 |
FUNCTION Provides a class of infinities |
|
148 |
ST-VERSION 2.2 |
|
149 |
PREREQUISITES |
|
150 |
CONFLICTS |
|
151 |
DISTRIBUTION world |
|
152 |
VERSION 1 |
|
153 |
DATE 22 Jan 1989 |
|
154 |
SUMMARY |
|
155 |
This is a set of changes that implements infinity in the Number hierarchy. |
|
156 |
I obtained the original changes from the author of an article in comp.lang.smalltalk. |
|
157 |
I have just installed it in my image and I have found two small omissions |
|
158 |
which are corrected in what is below; there might be others. Arithmetic |
|
159 |
between infinities is not defined but magnitude comparisons are implemented. |
|
160 |
" |
|
161 |
! ! |
|
162 |
||
265 | 163 |
!Infinity methodsFor: 'arithmetic'! |
164 |
||
165 |
* aNumber |
|
303 | 166 |
"Multiply the receiver by the argument and answer with the result." |
265 | 167 |
|
303 | 168 |
aNumber isInfinite ifTrue: [ |
169 |
self errorUndefinedResult: #* |
|
170 |
]. |
|
171 |
^self |
|
265 | 172 |
! |
173 |
||
174 |
+ aNumber |
|
303 | 175 |
"Add the receiver by the argument and answer with the result." |
265 | 176 |
|
303 | 177 |
(aNumber isInfinite and: [aNumber ~~ self]) ifTrue: [ |
178 |
self errorUndefinedResult: #+ |
|
179 |
]. |
|
180 |
^self |
|
265 | 181 |
! |
182 |
||
183 |
- aNumber |
|
303 | 184 |
"subtracet aNumber from the receiver answer with the result." |
265 | 185 |
|
303 | 186 |
(aNumber isInfinite) ifTrue: [ |
187 |
self errorUndefinedResult: #- |
|
188 |
]. |
|
189 |
^self |
|
265 | 190 |
! |
191 |
||
192 |
/ aNumber |
|
303 | 193 |
"Divide the receiver by the argument and answer with the result." |
265 | 194 |
|
303 | 195 |
(aNumber isInfinite or: [aNumber = 0]) ifTrue: [ |
196 |
self errorUndefinedResult: #/ |
|
197 |
]. |
|
198 |
^self |
|
265 | 199 |
! ! |
200 |
||
201 |
!Infinity methodsFor: 'comparing'! |
|
202 |
||
203 |
< aNumber |
|
303 | 204 |
"Positive infinity is greater than any number than positive infinity. |
205 |
Analogously, negative infinity is less than any other number other |
|
206 |
than negative infinity" |
|
265 | 207 |
|
303 | 208 |
aNumber == self ifTrue: [^false]. |
209 |
^ positive not! |
|
265 | 210 |
|
211 |
= aNumber |
|
303 | 212 |
^aNumber == self |
265 | 213 |
! ! |
214 |
||
215 |
!Infinity methodsFor: 'testing'! |
|
216 |
||
217 |
isFinite |
|
303 | 218 |
^false |
265 | 219 |
! |
220 |
||
221 |
isInfinite |
|
303 | 222 |
^true |
265 | 223 |
! ! |
224 |
||
225 |
!Infinity methodsFor: 'coercing'! |
|
226 |
||
227 |
generality |
|
303 | 228 |
"Infinities are more general than scalars, but not more general than |
229 |
vectors (e.g. Points)" |
|
230 |
^85 |
|
265 | 231 |
! |
232 |
||
233 |
retryReverseOf: aSymbol with: aNumber |
|
303 | 234 |
(aSymbol == #* or: [aSymbol == #+]) ifTrue: [ |
235 |
^self perform: aSymbol with: aNumber |
|
236 |
]. |
|
237 |
(aSymbol == #/ and: [aNumber isFinite]) ifTrue: [^0]. |
|
238 |
(aSymbol == #< and: [aNumber isFinite]) ifTrue: [^positive]. |
|
239 |
(aSymbol == #> and: [ aNumber isFinite ]) ifTrue: [^positive not]. |
|
240 |
(aSymbol == #= and: [ aNumber isFinite ])ifTrue: [ ^false ]. |
|
241 |
self errorUndefinedResult: aSymbol |
|
265 | 242 |
! ! |
243 |
||
244 |
!Infinity methodsFor: 'printing'! |
|
245 |
||
246 |
printOn: aStream |
|
303 | 247 |
aStream nextPutAll: self class name. |
248 |
aStream nextPutAll:(positive |
|
249 |
ifTrue: [' positive'] |
|
250 |
ifFalse: [' negative']) |
|
265 | 251 |
! ! |
252 |
||
253 |
!Infinity methodsFor: 'errors'! |
|
254 |
||
255 |
errorUndefinedResult: messageName |
|
303 | 256 |
self error: 'Undefined result in an Infinity ', messageName |
265 | 257 |
! ! |
258 |
||
259 |
!Infinity methodsFor: 'private'! |
|
260 |
||
261 |
setPositive: aBoolean |
|
303 | 262 |
positive := aBoolean |
265 | 263 |
! ! |
264 |
||
265 |
!Infinity class methodsFor: 'class initialization'! |
|
266 |
||
267 |
initialize |
|
303 | 268 |
"Infinity initialize" |
265 | 269 |
|
303 | 270 |
PositiveInfinity := self basicNew setPositive: true. |
271 |
NegativeInfinity := self basicNew setPositive: false |
|
265 | 272 |
! ! |
273 |
||
274 |
!Infinity class methodsFor: 'instance creation'! |
|
275 |
||
276 |
negative |
|
303 | 277 |
"Return the unique instance of negative infinity" |
265 | 278 |
|
303 | 279 |
^NegativeInfinity |
265 | 280 |
! |
281 |
||
282 |
new |
|
303 | 283 |
self shouldNotImplement |
265 | 284 |
! |
285 |
||
286 |
positive |
|
303 | 287 |
"Return the unique instance of positive infinity" |
265 | 288 |
|
303 | 289 |
^PositiveInfinity |
265 | 290 |
! ! |
291 |
||
292 |
Infinity initialize! |