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