-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInfinity.class.st
More file actions
227 lines (179 loc) · 4.82 KB
/
Copy pathInfinity.class.st
File metadata and controls
227 lines (179 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"
The real +infinity or -infinity.
For some examples try these:
Infinity positive * Infinity negative.
Infinity positive * 2.
Infinity positive - 7811234871239847.
Infinity negative / -199.
Infinity positive reciprocal.
Infinity positive > Infinity negative.
Infinity negative < -19238479182374598172349871234.
Infinity negative > 0.
Infinity negative min: Infinity positive.
The following are examples of undeterminations (they produce an error):
Infinity positive + Infinity negative.
Infinity positive * 0.
Infinity positive / Infinity positive.
Infinity positive raisedToInteger: 0.
"
Class {
#name : #Infinity,
#superclass : #Magnitude,
#instVars : [
'sign'
],
#category : #'Mathematics-Kernel-Support'
}
{ #category : #'instance creation' }
Infinity class >> negative [
"Answer a new instance of the receiver representing -infinity."
^ self sign: -1
]
{ #category : #'instance creation' }
Infinity class >> positive [
"Answer a new instance of the receiver representing +infinity."
^ self sign: 1
]
{ #category : #'instance creation' }
Infinity class >> projective [
"Answer a new instance of the receiver representing the projective (unsigned) infinity."
^ self sign: 0
]
{ #category : #'instance creation' }
Infinity class >> sign: anInteger [
"Answer a new instance of the receiver with sign anInteger."
^ self new sign: anInteger
]
{ #category : #arithmetic }
Infinity >> * anObject [
"Answer the product of the receiver by the argument."
anObject = 0 ifTrue: [^ self errorUndetermined].
^ self class sign: self sign * anObject sign
]
{ #category : #arithmetic }
Infinity >> + anObject [
"Answer the sum of the receiver and the argument."
(anObject isInfinity and: [self sign ~= anObject sign])
ifTrue: [^ self errorUndetermined].
^ self
]
{ #category : #arithmetic }
Infinity >> - anObject [
"Answer the difference between the receiver and the argument."
^ self + anObject negated
]
{ #category : #arithmetic }
Infinity >> / anObject [
"Answer the division of the receiver by the argument."
^ self * anObject reciprocal
]
{ #category : #comparing }
Infinity >> < anObject [
anObject isInfinity ifTrue: [^ self sign < anObject sign].
^ self negative
]
{ #category : #comparing }
Infinity >> <= aMagnitude [
^ self < aMagnitude
]
{ #category : #comparing }
Infinity >> = anObject [
^ self class == anObject class and: [self sign = anObject sign]
]
{ #category : #comparing }
Infinity >> > anObject [
anObject isInfinity ifTrue: [^ self sign > anObject sign].
^ self positive
]
{ #category : #comparing }
Infinity >> >= aMagnitude [
^ self > aMagnitude
]
{ #category : #converting }
Infinity >> adaptToNumber: rcvr andSend: selector [
selector == #+ ifTrue:[^self + rcvr].
selector == #* ifTrue:[^self * rcvr].
selector == #- ifTrue:[^self negated + rcvr].
selector == #/ ifTrue:[^ rcvr isInfinite ifTrue: [self errorUndetermined] ifFalse: [0]].
selector == #> ifTrue:[^self < rcvr].
selector == #>= ifTrue:[^self <= rcvr].
selector == #< ifTrue:[^self > rcvr].
selector == #<= ifTrue:[^self >= rcvr].
^super adaptToNumber: rcvr andSend: selector
]
{ #category : #private }
Infinity >> errorUndetermined [
^ self error: 'undetermined'
]
{ #category : #comparing }
Infinity >> hash [
^ self sign hash + self class hash
]
{ #category : #testing }
Infinity >> isInfinite [
^ true
]
{ #category : #testing }
Infinity >> isInfinity [
^ true
]
{ #category : #testing }
Infinity >> isZero [
^ false
]
{ #category : #arithmetic }
Infinity >> negated [
"Answer a copy of the receiver with the sign changed."
^ self class sign: self sign negated
]
{ #category : #testing }
Infinity >> negative [
"Answer true if the receiver is negative."
^ self sign negative
]
{ #category : #testing }
Infinity >> positive [
"Answer true if the receiver is positive."
^ self sign positive
]
{ #category : #printing }
Infinity >> printOn: aStream [
self negative ifTrue: [aStream nextPut: $-].
"aStream nextPut: Character infinity"
aStream nextPutAll: 'infinity'
]
{ #category : #arithmetic }
Infinity >> raisedTo: aNumber [
aNumber isInteger ifTrue: [^ self raisedToInteger: aNumber].
^ DomainError signal
]
{ #category : #arithmetic }
Infinity >> raisedToInteger: anInteger [
anInteger = 0 ifTrue: [^ self errorUndetermined].
anInteger negative ifTrue: [^ 0].
^ anInteger odd ifTrue: [self] ifFalse: [self negated]
]
{ #category : #arithmetic }
Infinity >> reciprocal [
"Answer zero. (1 / self)"
^ 0
]
{ #category : #accessing }
Infinity >> sign [
"Answer the sign of the receiver."
^ sign
]
{ #category : #'accessing-private' }
Infinity >> sign: anInteger [
sign := anInteger
]
{ #category : #arithmetic }
Infinity >> squared [
"Answer the square of the receiver."
^ self class positive
]
{ #category : #testing }
Infinity >> strictlyPositive [
"Answer true if the receiver is strictly positive."
^ self positive
]