author | Stefan Vogel <sv@exept.de> |
Mon, 26 Jul 1999 11:06:13 +0200 | |
changeset 4453 | 721c9dd0e0c6 |
parent 4138 | c5030c520941 |
child 4995 | 9556c41783f5 |
permissions | -rw-r--r-- |
1 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
213 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
Magnitude subclass:#ArithmeticValue |
|
1172 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'DivisionByZeroSignal DomainErrorSignal OverflowSignal |
|
16 |
UnderflowSignal ArithmeticSignal AnyArithmeticSignal |
|
17 |
UnorderedSignal' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Magnitude-Numbers' |
|
1 | 20 |
! |
21 |
||
701 | 22 |
!ArithmeticValue class methodsFor:'documentation'! |
82 | 23 |
|
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
213 | 27 |
All Rights Reserved |
1 | 28 |
|
82 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
1 | 37 |
|
82 | 38 |
documentation |
39 |
" |
|
40 |
ArithmeticValue is an abstract superclass for all things responding to |
|
155 | 41 |
arithmetic messages. It was inserted into the hierarchy, to allow objects |
42 |
like matrices, functions etc. to share the arithmetic methods defined here. |
|
43 |
||
1283 | 44 |
[class variables:] |
45 |
ArithmeticSignal <Signal> parent of all arithmetic signals |
|
46 |
(never raised itself) |
|
82 | 47 |
|
1283 | 48 |
DomainErrorSignal <Signal> raised upon float errors |
49 |
(for example range in trigonometric) |
|
50 |
||
51 |
DivisionByZeroSignal <Signal> raised when division by 0 is attempted |
|
155 | 52 |
|
1283 | 53 |
OverflowSignal <Signal> raised on overflow/underflow conditions |
54 |
UnderflowSignal in float arithmetic. |
|
55 |
Notice: some OperatingSystems do not |
|
56 |
provide enough information for ST/X to |
|
57 |
extract the real reason for the floatException |
|
58 |
thus raising DomainErrorSignal in these cases. |
|
155 | 59 |
|
1295 | 60 |
[author:] |
61 |
Claus Gittinger |
|
62 |
||
1283 | 63 |
[See also:] |
64 |
Number |
|
82 | 65 |
" |
66 |
! ! |
|
1 | 67 |
|
701 | 68 |
!ArithmeticValue class methodsFor:'initialization'! |
1 | 69 |
|
70 |
initialize |
|
71 |
"setup the signals" |
|
72 |
||
54 | 73 |
DomainErrorSignal isNil ifTrue:[ |
4453
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
74 |
ArithmeticSignal := ErrorSignal newSignalMayProceed:true. |
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
75 |
ArithmeticSignal nameClass:self message:#arithmeticSignal. |
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
76 |
ArithmeticSignal notifierString:'arithmetic error'. |
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
77 |
|
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
78 |
DomainErrorSignal := ArithmeticSignal newSignal. |
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
79 |
DomainErrorSignal nameClass:self message:#domainErrorSignal. |
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
80 |
DomainErrorSignal notifierString:'domain error'. |
155 | 81 |
|
4453
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
82 |
DivisionByZeroSignal := ArithmeticSignal newSignal. |
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
83 |
DivisionByZeroSignal nameClass:self message:#divisionByZeroSignal. |
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
84 |
DivisionByZeroSignal notifierString:'division by zero'. |
1 | 85 |
|
4453
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
86 |
OverflowSignal := ArithmeticSignal newSignal. |
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
87 |
OverflowSignal nameClass:self message:#overflowSignal. |
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
88 |
OverflowSignal notifierString:'overflow'. |
1 | 89 |
|
4453
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
90 |
UnderflowSignal := ArithmeticSignal newSignal. |
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
91 |
UnderflowSignal nameClass:self message:#underflowSignal. |
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
92 |
UnderflowSignal notifierString:'underflow'. |
54 | 93 |
|
4453
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
94 |
UnorderedSignal := ArithmeticSignal newSignal. |
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
95 |
UnorderedSignal nameClass:self message:#unorderedSignal. |
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
96 |
UnorderedSignal notifierString:'unordered'. |
1 | 97 |
|
4453
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
98 |
AnyArithmeticSignal := SignalSet with:DomainErrorSignal |
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
99 |
with:DivisionByZeroSignal |
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
100 |
with:OverflowSignal |
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
101 |
with:UnderflowSignal |
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
102 |
with:UnorderedSignal. |
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
103 |
] |
345 | 104 |
|
4453
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
105 |
"Modified: / 26.7.1999 / 10:39:56 / stefan" |
1 | 106 |
! ! |
107 |
||
701 | 108 |
!ArithmeticValue class methodsFor:'Signal constants'! |
109 |
||
110 |
anyArithmeticSignal |
|
111 |
"return a signalSet with all possible arithmetic signals. |
|
112 |
OBSOLETE: this will vanish, since starting with 2.10.3, signal parents |
|
113 |
have been added." |
|
114 |
||
115 |
^ AnyArithmeticSignal |
|
116 |
! |
|
117 |
||
118 |
arithmeticSignal |
|
119 |
"return the parent of all arithmetic signals" |
|
120 |
||
121 |
^ ArithmeticSignal |
|
122 |
! |
|
123 |
||
124 |
divisionByZeroSignal |
|
125 |
"return the signal which is raised on division by zero" |
|
126 |
||
127 |
^ DivisionByZeroSignal |
|
128 |
! |
|
1 | 129 |
|
130 |
domainErrorSignal |
|
131 |
"return the signal which is raised on math errors |
|
132 |
(such as log of 0 etc.)" |
|
133 |
||
134 |
^ DomainErrorSignal |
|
135 |
! |
|
136 |
||
137 |
overflowSignal |
|
138 |
"return the signal which is raised on overflow conditions (in floats)" |
|
139 |
||
140 |
^ OverflowSignal |
|
141 |
! |
|
142 |
||
143 |
underflowSignal |
|
144 |
"return the signal which is raised on underflow conditions (in floats)" |
|
145 |
||
146 |
^ UnderflowSignal |
|
147 |
! |
|
148 |
||
345 | 149 |
unorderedSignal |
150 |
"return the signal which is raised when numbers are compared, |
|
151 |
for which no ordering is defined (for example: complex numbers)" |
|
152 |
||
3393
f07a758f8c69
oops - unorderedSignal did not
Claus Gittinger <cg@exept.de>
parents:
3134
diff
changeset
|
153 |
^ UnorderedSignal |
1 | 154 |
! ! |
155 |
||
701 | 156 |
!ArithmeticValue methodsFor:'arithmetic'! |
157 |
||
158 |
* something |
|
159 |
"return the product of the receiver and the argument" |
|
160 |
||
161 |
^ self subclassResponsibility |
|
162 |
! |
|
163 |
||
164 |
+ something |
|
165 |
"return the sum of the receiver and the argument" |
|
166 |
||
167 |
^ self subclassResponsibility |
|
168 |
! |
|
169 |
||
170 |
- something |
|
171 |
"return the difference of the receiver and the argument" |
|
172 |
||
173 |
^ self subclassResponsibility |
|
174 |
! |
|
175 |
||
176 |
/ something |
|
177 |
"return the quotient of the receiver and the argument" |
|
1 | 178 |
|
701 | 179 |
^ self subclassResponsibility |
180 |
! |
|
181 |
||
182 |
// something |
|
1881 | 183 |
"return the integer quotient of dividing the receiver by aNumber with |
184 |
truncation towards negative infinity." |
|
701 | 185 |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
186 |
"/ Note: '^ (self / something) floor' may lead to infinite recursion |
1172 | 187 |
|
188 |
^ self subclassResponsibility |
|
189 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
190 |
"Modified: / 5.11.1996 / 11:46:27 / cg" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
191 |
"Modified: / 12.2.1998 / 16:31:36 / stefan" |
701 | 192 |
! |
193 |
||
194 |
\\ something |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
195 |
"return the receiver modulo something. |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
196 |
The remainder has the same sign as something," |
1 | 197 |
|
701 | 198 |
^ self - ((self // something) * something) |
3134 | 199 |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
200 |
" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
201 |
0.9 \\ 0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
202 |
0.9 \\ -0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
203 |
-0.9 \\ 0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
204 |
-0.9 \\ -0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
205 |
" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
206 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
207 |
"Modified: / 12.2.1998 / 19:14:37 / stefan" |
701 | 208 |
! |
209 |
||
210 |
abs |
|
211 |
"return the absolute value of the receiver" |
|
212 |
||
213 |
(self negative) ifTrue:[^ self negated]. |
|
214 |
^ self |
|
215 |
! |
|
216 |
||
217 |
negated |
|
218 |
"return the receiver negated" |
|
219 |
||
220 |
^ self class zero - self |
|
1 | 221 |
! |
222 |
||
701 | 223 |
quo:something |
224 |
"Return the integer quotient of dividing the receiver by the argument |
|
225 |
with truncation towards zero." |
|
226 |
||
227 |
^ (self / something) truncated |
|
228 |
! |
|
229 |
||
230 |
reciprocal |
|
231 |
"return the receivers reciprocal" |
|
232 |
||
233 |
^ self class unity / self |
|
234 |
! |
|
1 | 235 |
|
701 | 236 |
rem:something |
237 |
"Return the integer remainder of dividing the receiver by the argument |
|
238 |
with truncation towards zero. |
|
239 |
The remainder has the same sign as the receiver." |
|
240 |
||
241 |
^ self - ((self quo:something) * something) |
|
242 |
! ! |
|
243 |
||
1888 | 244 |
!ArithmeticValue methodsFor:'coercing'! |
245 |
||
246 |
coerce:aNumber |
|
247 |
"convert aNumber into an instance of the receivers class and return it." |
|
248 |
||
249 |
^ self subclassResponsibility |
|
250 |
! |
|
251 |
||
252 |
generality |
|
253 |
"return a number giving the receivers generality, that number is |
|
254 |
used to convert one of the arguments in a mixed expression. |
|
255 |
The generality has to be defined in subclasses, |
|
256 |
such that gen(a) > gen(b) iff, conversion of b into a's class |
|
257 |
does not cut precision. For example, Integer has 40, Float has 80, |
|
258 |
meaning that if we convert a Float to an Integer, some precision may |
|
259 |
be lost. The generality is used by ArithmeticValue>>retry:coercing:, |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
260 |
which converts the lower-precision number to the higher precision |
1888 | 261 |
numbers class, when mixed-type arithmetic is performed." |
262 |
||
263 |
^ self subclassResponsibility |
|
264 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
265 |
"Modified: / 5.11.1996 / 15:05:30 / cg" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
266 |
"Modified: / 13.2.1998 / 15:36:01 / stefan" |
1888 | 267 |
! |
268 |
||
269 |
retry:aSymbol coercing:aNumber |
|
270 |
"arithmetic represented by the binary operator, aSymbol, |
|
271 |
could not be performed with the receiver and the argument, aNumber, |
|
272 |
because of the differences in representation. |
|
273 |
Coerce either the receiver or the argument, depending on which has higher |
|
274 |
generality, and try again. |
|
275 |
If the operation is compare for same value (=), return false if |
|
276 |
the argument is not a Number. |
|
277 |
If the generalities are the same, create an error message, since this |
|
278 |
means that a subclass has not been fully implemented." |
|
279 |
||
280 |
|hasGenerality myGenerality otherGenerality| |
|
281 |
||
282 |
hasGenerality := aNumber respondsTo:#generality. |
|
283 |
hasGenerality ifFalse:[ |
|
284 |
(aSymbol == #=) ifTrue:[ |
|
285 |
^ false |
|
286 |
]. |
|
287 |
(aSymbol == #~=) ifTrue:[ |
|
288 |
^ true |
|
289 |
]. |
|
290 |
^ self error:'retry:coercing: argument is not a number'. |
|
291 |
]. |
|
292 |
||
293 |
myGenerality := self generality. |
|
294 |
otherGenerality := aNumber generality. |
|
295 |
(myGenerality > otherGenerality) ifTrue:[ |
|
296 |
^ self perform:aSymbol with:(self coerce:aNumber) |
|
297 |
]. |
|
298 |
(myGenerality < otherGenerality) ifTrue:[ |
|
299 |
^ (aNumber coerce:self) perform:aSymbol with:aNumber |
|
300 |
]. |
|
301 |
self error:'retry:coercing: oops - same generality' |
|
302 |
||
303 |
"Modified: 5.11.1996 / 15:03:38 / cg" |
|
304 |
! ! |
|
305 |
||
701 | 306 |
!ArithmeticValue methodsFor:'comparing'! |
307 |
||
308 |
< something |
|
309 |
"return true, if the argument is greater than the receiver" |
|
310 |
||
311 |
^ self subclassResponsibility |
|
1 | 312 |
! |
313 |
||
701 | 314 |
<= something |
315 |
"return true, if the argument is greater or equal than the receiver" |
|
316 |
||
317 |
^ (something < self) not |
|
318 |
! |
|
319 |
||
320 |
> something |
|
321 |
"return true, if the argument is less than the receiver" |
|
322 |
||
323 |
^ something < self |
|
324 |
! |
|
325 |
||
326 |
>= something |
|
327 |
"return true, if the argument is less or equal than the receiver" |
|
1 | 328 |
|
701 | 329 |
^ (self < something) not |
1 | 330 |
! |
331 |
||
701 | 332 |
compare:arg ifLess:lessBlock ifEqual:equalBlock ifGreater:greaterBlock |
333 |
"three-way compare - thanks to Self for this idea. |
|
334 |
Can be redefined in subclasses to do it with a single comparison if |
|
335 |
comparison is expensive." |
|
336 |
||
337 |
self < arg ifTrue:[ |
|
338 |
^ lessBlock value |
|
339 |
]. |
|
340 |
self = arg ifTrue:[ |
|
341 |
^ equalBlock value |
|
342 |
]. |
|
343 |
^ greaterBlock value |
|
344 |
! ! |
|
345 |
||
346 |
!ArithmeticValue methodsFor:'converting'! |
|
347 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
348 |
asDouble |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
349 |
"ST80 compatibility: return our floats are the identical to ST80 doubles" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
350 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
351 |
^ self asFloat |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
352 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
353 |
"Created: / 13.2.1998 / 15:40:14 / stefan" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
354 |
! |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
355 |
|
1 | 356 |
asFloat |
357 |
"return a float with same value" |
|
358 |
||
359 |
^ self subclassResponsibility |
|
360 |
! |
|
361 |
||
362 |
asFraction |
|
363 |
"return a fraction with same value" |
|
364 |
||
365 |
^ self subclassResponsibility |
|
366 |
! |
|
367 |
||
701 | 368 |
asInteger |
369 |
"return an integer with same value - might truncate" |
|
370 |
||
371 |
^ self truncated |
|
372 |
! |
|
373 |
||
1200 | 374 |
asShortFloat |
375 |
"return a shortFloat with same value" |
|
376 |
||
377 |
^ self asFloat asShortFloat |
|
378 |
||
379 |
"Modified: 17.4.1996 / 12:21:35 / cg" |
|
380 |
! |
|
381 |
||
701 | 382 |
degreesToRadians |
383 |
"interpreting the receiver as radians, return the degrees" |
|
384 |
||
385 |
^ self asFloat degreesToRadians |
|
386 |
! |
|
387 |
||
388 |
radiansToDegrees |
|
389 |
"interpreting the receiver as degrees, return the radians" |
|
390 |
||
391 |
^ self asFloat radiansToDegrees |
|
1 | 392 |
! ! |
393 |
||
4138
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
394 |
!ArithmeticValue methodsFor:'destructive arithmethic'! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
395 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
396 |
*= aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
397 |
"Return the product of self multiplied by aNumber. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
398 |
Self MAY, but NEED NOT be changed to contain the product. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
399 |
So this method must be used: 'a := a *= 5'. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
400 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
401 |
to do optimisations" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
402 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
403 |
^ self * aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
404 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
405 |
"Created: / 28.4.1999 / 11:46:11 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
406 |
"Modified: / 28.4.1999 / 11:53:28 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
407 |
! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
408 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
409 |
+= aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
410 |
"Return the sum of self and aNumber. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
411 |
Self MAY, but NEED NOT be changed to contain the product. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
412 |
So this method must be used: 'a := a += 5'. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
413 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
414 |
to do optimisations" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
415 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
416 |
^ self + aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
417 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
418 |
"Created: / 28.4.1999 / 10:13:41 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
419 |
"Modified: / 28.4.1999 / 11:54:11 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
420 |
! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
421 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
422 |
-= aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
423 |
"Return the difference of self and aNumber. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
424 |
Self MAY, but NEED NOT be changed to contain the product. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
425 |
So this method must be used: 'a := a -= 5'. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
426 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
427 |
to do optimisations" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
428 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
429 |
^ self - aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
430 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
431 |
"Created: / 28.4.1999 / 10:13:58 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
432 |
"Modified: / 28.4.1999 / 11:54:37 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
433 |
! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
434 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
435 |
/= aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
436 |
"Return the quotient of self and aNumber. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
437 |
Self MAY, but NEED NOT be changed to contain the product. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
438 |
So this method must be used: 'a := a /= 5'. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
439 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
440 |
to do optimisations" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
441 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
442 |
^ self / aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
443 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
444 |
"Created: / 28.4.1999 / 11:46:22 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
445 |
"Modified: / 28.4.1999 / 11:55:06 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
446 |
! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
447 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
448 |
div2 |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
449 |
"Return the quotient of self divided by 2. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
450 |
Self MAY, but NEED NOT be changed to contain the product. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
451 |
So this method must be used: 'a := a div2. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
452 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
453 |
to do optimisations" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
454 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
455 |
^ self // 2 |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
456 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
457 |
"Created: / 28.4.1999 / 10:12:44 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
458 |
"Modified: / 28.4.1999 / 11:56:09 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
459 |
! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
460 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
461 |
mul2 |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
462 |
"Return the product of self multiplied by 2. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
463 |
Self MAY, but NEED NOT be changed to contain the product. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
464 |
So this method must be used: 'a := a mul2. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
465 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
466 |
to do optimisations" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
467 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
468 |
^ self * 2 |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
469 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
470 |
"Created: / 28.4.1999 / 10:12:55 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
471 |
"Modified: / 28.4.1999 / 11:56:38 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
472 |
! ! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
473 |
|
701 | 474 |
!ArithmeticValue methodsFor:'double dispatching'! |
1 | 475 |
|
1888 | 476 |
differenceFromFixedPoint:aFixedPoint |
477 |
"the receiver does not know how to subtract from a fixedPoint number - |
|
478 |
retry the operation by coercing to higher generality" |
|
479 |
||
480 |
^ aFixedPoint retry:#- coercing:self |
|
481 |
||
482 |
"Created: 5.11.1996 / 14:55:51 / cg" |
|
483 |
! |
|
484 |
||
701 | 485 |
differenceFromFloat:aFloat |
486 |
"the receiver does not know how to subtract from a float - |
|
487 |
retry the operation by coercing to higher generality" |
|
1 | 488 |
|
701 | 489 |
^ aFloat retry:#- coercing:self |
1 | 490 |
! |
491 |
||
701 | 492 |
differenceFromFraction:aFraction |
493 |
"the receiver does not know how to subtract from a fraction - |
|
494 |
retry the operation by coercing to higher generality" |
|
1 | 495 |
|
701 | 496 |
^ aFraction retry:#- coercing:self |
1 | 497 |
! |
498 |
||
701 | 499 |
differenceFromInteger:anInteger |
500 |
"the receiver does not know how to subtract from an integer - |
|
501 |
retry the operation by coercing to higher generality" |
|
1 | 502 |
|
701 | 503 |
^ anInteger retry:#- coercing:self |
1 | 504 |
! |
505 |
||
1200 | 506 |
differenceFromShortFloat:aShortFloat |
507 |
"the receiver does not know how to subtract from a shortFloat - |
|
508 |
retry the operation by coercing to higher generality" |
|
509 |
||
510 |
^ aShortFloat retry:#- coercing:self |
|
511 |
||
512 |
"Created: 17.4.1996 / 12:33:16 / cg" |
|
513 |
! |
|
514 |
||
1888 | 515 |
lessFromFixedPoint:aFixedPoint |
516 |
"the receiver does not know how to compare to a fixedPoint number - |
|
517 |
retry the operation by coercing to higher generality" |
|
518 |
||
519 |
^ aFixedPoint retry:#< coercing:self |
|
520 |
||
521 |
"Created: 5.11.1996 / 14:56:12 / cg" |
|
522 |
! |
|
523 |
||
701 | 524 |
lessFromFloat:aFloat |
525 |
"the receiver does not know how to compare to a float - |
|
526 |
retry the operation by coercing to higher generality" |
|
1 | 527 |
|
701 | 528 |
^ aFloat retry:#< coercing:self |
1 | 529 |
! |
530 |
||
701 | 531 |
lessFromFraction:aFraction |
532 |
"the receiver does not know how to compare to a fraction - |
|
533 |
retry the operation by coercing to higher generality" |
|
1 | 534 |
|
701 | 535 |
^ aFraction retry:#< coercing:self |
1 | 536 |
! |
537 |
||
701 | 538 |
lessFromInteger:anInteger |
539 |
"the receiver does not know how to compare to an integer - |
|
540 |
retry the operation by coercing to higher generality" |
|
1 | 541 |
|
701 | 542 |
^ anInteger retry:#< coercing:self |
1 | 543 |
! |
544 |
||
1200 | 545 |
lessFromShortFloat:aShortFloat |
546 |
"the receiver does not know how to compare to a shortFloat - |
|
547 |
retry the operation by coercing to higher generality" |
|
548 |
||
549 |
^ aShortFloat retry:#< coercing:self |
|
550 |
||
551 |
"Modified: 17.4.1996 / 12:33:33 / cg" |
|
552 |
! |
|
553 |
||
1888 | 554 |
productFromFixedPoint:aFixedPoint |
555 |
"the receiver does not know how to multiply a fixed point number - |
|
556 |
retry the operation by coercing to higher generality" |
|
557 |
||
558 |
^ aFixedPoint retry:#* coercing:self |
|
559 |
||
560 |
"Created: 5.11.1996 / 14:56:28 / cg" |
|
561 |
! |
|
562 |
||
701 | 563 |
productFromFloat:aFloat |
564 |
"the receiver does not know how to multiply a float - |
|
565 |
retry the operation by coercing to higher generality" |
|
1 | 566 |
|
701 | 567 |
^ aFloat retry:#* coercing:self |
1 | 568 |
! |
569 |
||
701 | 570 |
productFromFraction:aFraction |
571 |
"the receiver does not know how to multiply a fraction - |
|
572 |
retry the operation by coercing to higher generality" |
|
1 | 573 |
|
701 | 574 |
^ aFraction retry:#* coercing:self |
1 | 575 |
! |
576 |
||
701 | 577 |
productFromInteger:anInteger |
578 |
"the receiver does not know how to multiply an integer - |
|
579 |
retry the operation by coercing to higher generality" |
|
1 | 580 |
|
701 | 581 |
^ anInteger retry:#* coercing:self |
1 | 582 |
! |
583 |
||
1200 | 584 |
productFromShortFloat:aShortFloat |
585 |
"the receiver does not know how to multiply a shortFloat - |
|
586 |
retry the operation by coercing to higher generality" |
|
587 |
||
588 |
^ aShortFloat retry:#* coercing:self |
|
589 |
||
590 |
"Created: 17.4.1996 / 12:33:48 / cg" |
|
591 |
! |
|
592 |
||
1888 | 593 |
quotientFromFixedPoint:aFixedPoint |
594 |
"the receiver does not know how to divide a fixed point number - |
|
595 |
retry the operation by coercing to higher generality" |
|
596 |
||
597 |
^ aFixedPoint retry:#/ coercing:self |
|
598 |
||
599 |
"Created: 5.11.1996 / 14:56:41 / cg" |
|
600 |
! |
|
601 |
||
701 | 602 |
quotientFromFloat:aFloat |
603 |
"the receiver does not know how to divide a float - |
|
604 |
retry the operation by coercing to higher generality" |
|
1 | 605 |
|
701 | 606 |
^ aFloat retry:#/ coercing:self |
1 | 607 |
! |
608 |
||
701 | 609 |
quotientFromFraction:aFraction |
610 |
"the receiver does not know how to divide a fraction - |
|
611 |
retry the operation by coercing to higher generality" |
|
1 | 612 |
|
701 | 613 |
^ aFraction retry:#/ coercing:self |
1 | 614 |
! |
615 |
||
701 | 616 |
quotientFromInteger:anInteger |
617 |
"the receiver does not know how to divide an integer - |
|
1 | 618 |
retry the operation by coercing to higher generality" |
619 |
||
701 | 620 |
^ anInteger retry:#/ coercing:self |
1 | 621 |
! |
622 |
||
1200 | 623 |
quotientFromShortFloat:aShortFloat |
624 |
"the receiver does not know how to divide a shortFloat - |
|
625 |
retry the operation by coercing to higher generality" |
|
626 |
||
627 |
^ aShortFloat retry:#/ coercing:self |
|
628 |
||
629 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
630 |
! |
|
631 |
||
1888 | 632 |
sumFromFixedPoint:aFixedPoint |
633 |
"the receiver does not know how to add a fixed point number - |
|
634 |
retry the operation by coercing to higher generality" |
|
635 |
||
636 |
^ aFixedPoint retry:#+ coercing:self |
|
637 |
||
638 |
"Created: 5.11.1996 / 14:56:56 / cg" |
|
639 |
! |
|
640 |
||
1 | 641 |
sumFromFloat:aFloat |
642 |
"the receiver does not know how to add a float - |
|
643 |
retry the operation by coercing to higher generality" |
|
644 |
||
645 |
^ aFloat retry:#+ coercing:self |
|
646 |
! |
|
647 |
||
648 |
sumFromFraction:aFraction |
|
649 |
"the receiver does not know how to add a fraction - |
|
650 |
retry the operation by coercing to higher generality" |
|
651 |
||
652 |
^ aFraction retry:#+ coercing:self |
|
653 |
! |
|
654 |
||
701 | 655 |
sumFromInteger:anInteger |
656 |
"the receiver does not know how to add an integer - |
|
1 | 657 |
retry the operation by coercing to higher generality" |
658 |
||
701 | 659 |
^ anInteger retry:#+ coercing:self |
1200 | 660 |
! |
661 |
||
662 |
sumFromShortFloat:aShortFloat |
|
663 |
"the receiver does not know how to add a shortFloat - |
|
664 |
retry the operation by coercing to higher generality" |
|
665 |
||
666 |
^ aShortFloat retry:#+ coercing:self |
|
667 |
||
668 |
"Created: 17.4.1996 / 12:34:10 / cg" |
|
1 | 669 |
! ! |
670 |
||
671 |
!ArithmeticValue methodsFor:'misc math'! |
|
672 |
||
673 |
exp |
|
674 |
"return e ^ receiver" |
|
675 |
||
676 |
^ self asFloat exp |
|
677 |
! |
|
678 |
||
701 | 679 |
floorLog:radix |
680 |
"return the logarithm truncated as an integer" |
|
681 |
||
682 |
^ (self log:radix) floor |
|
683 |
! |
|
684 |
||
1 | 685 |
ln |
686 |
"return the natural logarithm of the receiver" |
|
687 |
||
688 |
^ self asFloat ln |
|
689 |
! |
|
690 |
||
691 |
log |
|
692 |
"return log base 10 of the receiver" |
|
693 |
||
694 |
^ self log:10 |
|
695 |
! |
|
696 |
||
697 |
log:aNumber |
|
698 |
"return log base aNumber of the receiver" |
|
699 |
||
700 |
^ self ln / aNumber ln |
|
701 |
! |
|
702 |
||
703 |
raisedTo:aNumber |
|
704 |
"return the receiver raised to aNumber" |
|
705 |
||
706 |
aNumber = 0 ifTrue:[^ 1]. |
|
707 |
aNumber = 1 ifTrue:[^ self]. |
|
708 |
aNumber isInteger ifTrue:[ |
|
213 | 709 |
^ self raisedToInteger:aNumber |
1 | 710 |
]. |
711 |
^ self asFloat raisedTo:aNumber |
|
712 |
! |
|
713 |
||
714 |
raisedToInteger:anInteger |
|
715 |
"return the receiver raised to anInteger" |
|
716 |
||
717 |
|count result| |
|
718 |
||
719 |
result := self coerce:1. |
|
720 |
count := anInteger abs. |
|
721 |
count timesRepeat:[result := result * self]. |
|
722 |
(anInteger < 0) ifTrue:[ |
|
213 | 723 |
^ 1 / result |
1 | 724 |
]. |
725 |
^ result |
|
701 | 726 |
! |
727 |
||
728 |
sqrt |
|
729 |
"return the square root of the receiver" |
|
730 |
||
731 |
^ self asFloat sqrt |
|
732 |
! |
|
733 |
||
734 |
squared |
|
735 |
"return receiver * receiver" |
|
736 |
||
737 |
^ self * self |
|
738 |
! ! |
|
739 |
||
740 |
!ArithmeticValue methodsFor:'queries'! |
|
741 |
||
742 |
respondsToArithmetic |
|
743 |
"return true, if the receiver responds to arithmetic messages" |
|
744 |
||
745 |
^ true |
|
746 |
! ! |
|
747 |
||
748 |
!ArithmeticValue methodsFor:'testing'! |
|
749 |
||
750 |
denominator |
|
751 |
"return the denominator of the receiver" |
|
752 |
||
753 |
^ 1 |
|
754 |
! |
|
755 |
||
756 |
even |
|
757 |
"return true if the receiver is divisible by 2" |
|
758 |
||
759 |
^ self truncated asInteger even |
|
760 |
! |
|
761 |
||
3635 | 762 |
isComplex |
763 |
"Answer whether the receiver has an imaginary part" |
|
764 |
||
765 |
^ false |
|
766 |
||
767 |
"Modified: / 9.7.1998 / 10:19:27 / cg" |
|
768 |
! |
|
769 |
||
701 | 770 |
negative |
771 |
"return true, if the receiver is < 0" |
|
772 |
||
773 |
" this would lead to infinite recursion ... |
|
774 |
^ (self < 0) |
|
775 |
" |
|
776 |
^ self subclassResponsibility |
|
777 |
! |
|
778 |
||
779 |
numerator |
|
780 |
"return the numerator of the receiver." |
|
781 |
||
782 |
^ self |
|
783 |
! |
|
784 |
||
785 |
odd |
|
786 |
"return true if the receiver is not divisible by 2" |
|
787 |
||
788 |
^ self even not |
|
789 |
! |
|
790 |
||
791 |
positive |
|
792 |
"return true, if the receiver is >= 0" |
|
793 |
||
794 |
^ self negative not |
|
795 |
! |
|
796 |
||
797 |
sign |
|
798 |
"return the sign of the receiver" |
|
799 |
||
800 |
(self < 0) ifTrue:[^ -1]. |
|
801 |
(self > 0) ifTrue:[^ 1]. |
|
802 |
^ 0 |
|
803 |
! |
|
804 |
||
805 |
strictlyPositive |
|
806 |
"return true, if the receiver is > 0" |
|
807 |
||
808 |
^ (self > 0) |
|
1 | 809 |
! ! |
810 |
||
811 |
!ArithmeticValue methodsFor:'trigonometric'! |
|
812 |
||
813 |
arcCos |
|
814 |
"return the arccosine of the receiver (in radians)" |
|
815 |
||
816 |
^ self asFloat arcCos |
|
817 |
! |
|
818 |
||
819 |
arcSin |
|
820 |
"return the arcsine of the receiver (in radians)" |
|
821 |
||
822 |
^ self asFloat arcSin |
|
823 |
! |
|
824 |
||
825 |
arcTan |
|
826 |
"return the arctangens of the receiver (in radians)" |
|
827 |
||
828 |
^ self asFloat arcTan |
|
701 | 829 |
! |
830 |
||
831 |
cos |
|
832 |
"return the cosine of the receiver (interpreted as radians)" |
|
833 |
||
834 |
^ self asFloat cos |
|
835 |
! |
|
836 |
||
837 |
sin |
|
838 |
"return the sine of the receiver (interpreted as radians)" |
|
839 |
||
840 |
^ self asFloat sin |
|
841 |
! |
|
842 |
||
843 |
tan |
|
844 |
"return the tangens of the receiver (interpreted as radians)" |
|
845 |
||
846 |
^ self asFloat tan |
|
1 | 847 |
! ! |
848 |
||
701 | 849 |
!ArithmeticValue methodsFor:'truncation & rounding'! |
1 | 850 |
|
701 | 851 |
ceiling |
852 |
"return the integer nearest the receiver towards positive infinity." |
|
1 | 853 |
|
701 | 854 |
|anInteger| |
1 | 855 |
|
701 | 856 |
anInteger := self // 1. "truncates towards negative infinity" |
857 |
anInteger = self ifTrue:[^ anInteger]. |
|
858 |
^ anInteger + 1 |
|
1 | 859 |
! |
860 |
||
701 | 861 |
floor |
862 |
"return the receiver truncated towards negative infinity" |
|
1 | 863 |
|
701 | 864 |
^ self // 1 |
1 | 865 |
! |
866 |
||
701 | 867 |
roundTo:aNumber |
868 |
"return the receiver rounded to multiples of aNumber" |
|
1 | 869 |
|
701 | 870 |
^ (self / aNumber) rounded * aNumber |
871 |
! |
|
872 |
||
873 |
rounded |
|
874 |
"return the integer nearest the receiver" |
|
875 |
||
1881 | 876 |
self negative ifTrue:[ |
877 |
^ (self - 0.5) ceiling |
|
878 |
]. |
|
701 | 879 |
^ (self + 0.5) floor |
1881 | 880 |
|
881 |
"Modified: 5.11.1996 / 11:31:59 / cg" |
|
1 | 882 |
! |
883 |
||
701 | 884 |
truncateTo:aNumber |
885 |
"return the receiver truncated to multiples of aNumber" |
|
1 | 886 |
|
701 | 887 |
^ ((self / aNumber) floor * aNumber) asInteger |
1 | 888 |
! |
889 |
||
701 | 890 |
truncated |
891 |
"return the receiver truncated towards zero" |
|
1 | 892 |
|
701 | 893 |
self negative ifTrue:[ |
894 |
^ self ceiling |
|
895 |
]. |
|
896 |
^ self floor |
|
897 |
! ! |
|
1 | 898 |
|
701 | 899 |
!ArithmeticValue class methodsFor:'documentation'! |
900 |
||
901 |
version |
|
4453
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
902 |
^ '$Header: /cvs/stx/stx/libbasic/ArithmeticValue.st,v 1.31 1999-07-26 09:06:13 stefan Exp $' |
1 | 903 |
! ! |
701 | 904 |
ArithmeticValue initialize! |