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