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