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