author | Claus Gittinger <cg@exept.de> |
Tue, 01 Dec 2009 23:08:29 +0100 | |
changeset 12576 | 47985f127034 |
parent 11735 | 60aa644effbe |
child 12578 | 6cb12eb8eb59 |
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 |
" |
|
5948 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
1 | 14 |
Magnitude subclass:#ArithmeticValue |
1172 | 15 |
instanceVariableNames:'' |
16 |
classVariableNames:'DivisionByZeroSignal DomainErrorSignal OverflowSignal |
|
6342
fbbe62d5b17c
Remove obsolete (since years) AnyArithmeticSignal definitions.
Stefan Vogel <sv@exept.de>
parents:
6156
diff
changeset
|
17 |
UnderflowSignal ArithmeticSignal UnorderedSignal |
fbbe62d5b17c
Remove obsolete (since years) AnyArithmeticSignal definitions.
Stefan Vogel <sv@exept.de>
parents:
6156
diff
changeset
|
18 |
ConversionErrorSignal' |
1172 | 19 |
poolDictionaries:'' |
20 |
category:'Magnitude-Numbers' |
|
1 | 21 |
! |
22 |
||
701 | 23 |
!ArithmeticValue class methodsFor:'documentation'! |
82 | 24 |
|
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
213 | 28 |
All Rights Reserved |
1 | 29 |
|
82 | 30 |
This software is furnished under a license and may be used |
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
1 | 38 |
|
82 | 39 |
documentation |
40 |
" |
|
41 |
ArithmeticValue is an abstract superclass for all things responding to |
|
155 | 42 |
arithmetic messages. It was inserted into the hierarchy, to allow objects |
43 |
like matrices, functions etc. to share the arithmetic methods defined here. |
|
44 |
||
5955 | 45 |
Notice, that what used to be signals are now exception classes - the class |
46 |
variables and signal accessors remain here for backward compatibility. |
|
47 |
||
1283 | 48 |
[class variables:] |
10326 | 49 |
ArithmeticSignal <Error> parent of all arithmetic signals |
50 |
(never raised itself) |
|
51 |
New: now a reference to ArithmeticError |
|
82 | 52 |
|
10326 | 53 |
DomainErrorSignal <Error> raised upon float errors |
54 |
(for example range in trigonometric) |
|
55 |
New: now a reference to DomainError |
|
1283 | 56 |
|
10326 | 57 |
DivisionByZeroSignal <Error> raised when division by 0 is attempted |
58 |
New: now a reference to ZeroDivide |
|
155 | 59 |
|
10326 | 60 |
OverflowSignal <Error> raised on overflow/underflow conditions |
61 |
UnderflowSignal in float arithmetic. |
|
62 |
Notice: some OperatingSystems do not |
|
63 |
provide enough information for ST/X to |
|
64 |
extract the real reason for the floatException |
|
65 |
thus raising DomainErrorSignal in these cases. |
|
155 | 66 |
|
1295 | 67 |
[author:] |
10326 | 68 |
Claus Gittinger |
1295 | 69 |
|
1283 | 70 |
[See also:] |
10326 | 71 |
Number |
82 | 72 |
" |
73 |
! ! |
|
1 | 74 |
|
701 | 75 |
!ArithmeticValue class methodsFor:'Signal constants'! |
76 |
||
77 |
arithmeticSignal |
|
78 |
"return the parent of all arithmetic signals" |
|
79 |
||
7403 | 80 |
^ ArithmeticError |
701 | 81 |
! |
82 |
||
83 |
divisionByZeroSignal |
|
84 |
"return the signal which is raised on division by zero" |
|
85 |
||
7403 | 86 |
^ ZeroDivide |
701 | 87 |
! |
1 | 88 |
|
89 |
domainErrorSignal |
|
90 |
"return the signal which is raised on math errors |
|
91 |
(such as log of 0 etc.)" |
|
92 |
||
7403 | 93 |
^ DomainError |
94 |
! |
|
95 |
||
96 |
imaginaryResultSignal |
|
97 |
"return the signal which is raised when an imaginary result would be created |
|
98 |
(such as when taking the sqrt of a negative number)" |
|
99 |
||
100 |
^ ImaginaryResultError |
|
1 | 101 |
! |
102 |
||
7440 | 103 |
operationNotPossibleSignal |
104 |
^ Error |
|
105 |
! |
|
106 |
||
1 | 107 |
overflowSignal |
108 |
"return the signal which is raised on overflow conditions (in floats)" |
|
109 |
||
7403 | 110 |
^ OverflowError |
1 | 111 |
! |
112 |
||
5955 | 113 |
rangeErrorSignal |
114 |
"return the parent of the overflow/underflow signals" |
|
115 |
||
116 |
^ RangeError |
|
117 |
! |
|
118 |
||
7440 | 119 |
undefinedResultSignal |
120 |
^ Error |
|
121 |
! |
|
122 |
||
1 | 123 |
underflowSignal |
124 |
"return the signal which is raised on underflow conditions (in floats)" |
|
125 |
||
7403 | 126 |
^ UnderflowError |
1 | 127 |
! |
128 |
||
345 | 129 |
unorderedSignal |
10326 | 130 |
"return the signal which is raised when numbers are compared, |
345 | 131 |
for which no ordering is defined (for example: complex numbers)" |
132 |
||
7403 | 133 |
^ UnorderedNumbersError |
1 | 134 |
! ! |
135 |
||
7440 | 136 |
!ArithmeticValue class methodsFor:'class initialization'! |
137 |
||
138 |
initialize |
|
139 |
"setup the signals" |
|
140 |
||
141 |
"/ notice that we now use class based exceptions; |
|
142 |
"/ however, for backward compatibility, the class variables |
|
143 |
"/ referring to those are kept for a while, for subclass compatibility. |
|
144 |
"/ Please convert your code to access those via the Signal-constants class |
|
145 |
"/ messages (accesors) instead of referring to the class variables. |
|
146 |
||
147 |
ArithmeticSignal := ArithmeticError. |
|
148 |
DomainErrorSignal := DomainError. |
|
149 |
DivisionByZeroSignal := ZeroDivide. |
|
150 |
UnorderedSignal := UnorderedNumbersError. |
|
151 |
ConversionErrorSignal := ConversionError. |
|
152 |
OverflowSignal := OverflowError. |
|
153 |
UnderflowSignal := UnderflowError. |
|
154 |
! ! |
|
155 |
||
156 |
!ArithmeticValue class methodsFor:'constants'! |
|
157 |
||
158 |
NaN |
|
8850 | 159 |
"return the constant NaN (not a Number)." |
160 |
||
7440 | 161 |
^ NotANumber new |
162 |
! |
|
163 |
||
164 |
infinity |
|
8850 | 165 |
"return something which represents infinity (for my instances)" |
166 |
||
7440 | 167 |
^ Infinity positive |
168 |
! |
|
169 |
||
7538
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
170 |
nan |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
171 |
"VW compatibility" |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
172 |
|
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
173 |
^ self NaN |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
174 |
! |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
175 |
|
7440 | 176 |
negativeInfinity |
8850 | 177 |
"return something which represents negative infinity (for my instances)" |
178 |
||
7440 | 179 |
^ Infinity negative |
180 |
! |
|
181 |
||
7726 | 182 |
unity |
11467 | 183 |
"return something which represents the unity element (for my instances). |
184 |
That is the neutral element for multiplication." |
|
8850 | 185 |
|
7726 | 186 |
self subclassResponsibility |
187 |
! |
|
188 |
||
7440 | 189 |
zero |
11467 | 190 |
"return something which represents the zero element (for my instances). |
191 |
That is the neutral element for addition." |
|
8850 | 192 |
|
7440 | 193 |
self subclassResponsibility |
194 |
! ! |
|
195 |
||
8892 | 196 |
!ArithmeticValue class methodsFor:'queries'! |
197 |
||
198 |
isAbstract |
|
11227 | 199 |
"Return if this class is an abstract class. |
200 |
True is returned for ArithmeticValue here; false for subclasses. |
|
201 |
Abstract subclasses must redefine again." |
|
202 |
||
8892 | 203 |
^ self == ArithmeticValue |
204 |
! ! |
|
205 |
||
12576 | 206 |
!ArithmeticValue methodsFor:'arithmethic destructive'! |
207 |
||
208 |
*= aNumber |
|
209 |
"Return the product of self multiplied by aNumber. |
|
210 |
The receiver MAY, but NEED NOT be changed to contain the product. |
|
211 |
So this method must be used as: 'a := a *= 5'. |
|
212 |
This method can be redefined for constructed datatypes to do optimisations" |
|
213 |
||
214 |
^ self * aNumber |
|
215 |
||
216 |
"Created: / 28.4.1999 / 11:46:11 / stefan" |
|
217 |
"Modified: / 28.4.1999 / 11:53:28 / stefan" |
|
218 |
! |
|
219 |
||
220 |
+= aNumber |
|
221 |
"Return the sum of self and aNumber. |
|
222 |
The receiver MAY, but NEED NOT be changed to contain the sum. |
|
223 |
So this method must be used as: 'a := a += 5'. |
|
224 |
This method can be redefined for constructed datatypes to do optimisations" |
|
225 |
||
226 |
^ self + aNumber |
|
227 |
||
228 |
"Created: / 28.4.1999 / 10:13:41 / stefan" |
|
229 |
"Modified: / 28.4.1999 / 11:54:11 / stefan" |
|
230 |
! |
|
231 |
||
232 |
-= aNumber |
|
233 |
"Return the difference of self and aNumber. |
|
234 |
The receiver MAY, but NEED NOT be changed to contain the difference. |
|
235 |
So this method must be used as: 'a := a -= 5'. |
|
236 |
This method can be redefined for constructed datatypes to do optimisations" |
|
237 |
||
238 |
^ self - aNumber |
|
239 |
||
240 |
"Created: / 28.4.1999 / 10:13:58 / stefan" |
|
241 |
"Modified: / 28.4.1999 / 11:54:37 / stefan" |
|
242 |
! |
|
243 |
||
244 |
/= aNumber |
|
245 |
"Return the quotient of self and aNumber. |
|
246 |
The receiver MAY, but NEED NOT be changed to contain the quotient. |
|
247 |
So this method must be used as: 'a := a /= 5'. |
|
248 |
This method can be redefined for constructed datatypes to do optimisations" |
|
249 |
||
250 |
^ self / aNumber |
|
251 |
||
252 |
"Created: / 28.4.1999 / 11:46:22 / stefan" |
|
253 |
"Modified: / 28.4.1999 / 11:55:06 / stefan" |
|
254 |
! |
|
255 |
||
256 |
div2 |
|
257 |
"Return the quotient of self divided by 2. |
|
258 |
The receiver MAY, but NEED NOT be changed to contain the result. |
|
259 |
So this method must be used as: 'a := a div2. |
|
260 |
This method can be redefined for constructed datatypes to do optimisations" |
|
261 |
||
262 |
^ self // 2 |
|
263 |
||
264 |
"Created: / 28.4.1999 / 10:12:44 / stefan" |
|
265 |
"Modified: / 28.4.1999 / 11:56:09 / stefan" |
|
266 |
! |
|
267 |
||
268 |
mul2 |
|
269 |
"Return the product of self multiplied by 2. |
|
270 |
The receiver MAY, but NEED NOT be changed to contain the result. |
|
271 |
So this method must be used as: 'a := a mul2. |
|
272 |
This method can be redefined for constructed datatypes to do optimisations" |
|
273 |
||
274 |
^ self * 2 |
|
275 |
||
276 |
"Created: / 28.4.1999 / 10:12:55 / stefan" |
|
277 |
"Modified: / 28.4.1999 / 11:56:38 / stefan" |
|
278 |
! ! |
|
279 |
||
701 | 280 |
!ArithmeticValue methodsFor:'arithmetic'! |
281 |
||
282 |
* something |
|
11735 | 283 |
"return the product of the receiver and the argument." |
701 | 284 |
|
285 |
^ self subclassResponsibility |
|
286 |
! |
|
287 |
||
288 |
+ something |
|
289 |
"return the sum of the receiver and the argument" |
|
290 |
||
291 |
^ self subclassResponsibility |
|
292 |
! |
|
293 |
||
294 |
- something |
|
295 |
"return the difference of the receiver and the argument" |
|
296 |
||
297 |
^ self subclassResponsibility |
|
298 |
! |
|
299 |
||
300 |
/ something |
|
301 |
"return the quotient of the receiver and the argument" |
|
1 | 302 |
|
701 | 303 |
^ self subclassResponsibility |
304 |
! |
|
305 |
||
306 |
// something |
|
1881 | 307 |
"return the integer quotient of dividing the receiver by aNumber with |
308 |
truncation towards negative infinity." |
|
701 | 309 |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
310 |
"/ Note: '^ (self / something) floor' may lead to infinite recursion |
1172 | 311 |
|
312 |
^ self subclassResponsibility |
|
313 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
314 |
"Modified: / 5.11.1996 / 11:46:27 / cg" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
315 |
"Modified: / 12.2.1998 / 16:31:36 / stefan" |
701 | 316 |
! |
317 |
||
318 |
\\ something |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
319 |
"return the receiver modulo something. |
6067 | 320 |
The remainder has the same sign as something. |
321 |
The following is always true: |
|
10326 | 322 |
(receiver // something) * something + (receiver \\ something) = receiver |
6067 | 323 |
" |
1 | 324 |
|
701 | 325 |
^ self - ((self // something) * something) |
3134 | 326 |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
327 |
" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
328 |
0.9 \\ 0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
329 |
0.9 \\ -0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
330 |
-0.9 \\ 0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
331 |
-0.9 \\ -0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
332 |
" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
333 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
334 |
"Modified: / 12.2.1998 / 19:14:37 / stefan" |
701 | 335 |
! |
336 |
||
337 |
abs |
|
338 |
"return the absolute value of the receiver" |
|
339 |
||
340 |
(self negative) ifTrue:[^ self negated]. |
|
341 |
^ self |
|
342 |
! |
|
343 |
||
10326 | 344 |
dist:arg |
8757 | 345 |
"return the distance between arg and the receiver." |
346 |
||
347 |
^ (arg - self) abs |
|
348 |
||
349 |
" |
|
10326 | 350 |
(1%1) dist:(0%0) |
351 |
(1@1) dist:(0@0) |
|
352 |
(1) dist:(0) |
|
8757 | 353 |
" |
354 |
! |
|
355 |
||
701 | 356 |
negated |
357 |
"return the receiver negated" |
|
358 |
||
359 |
^ self class zero - self |
|
1 | 360 |
! |
361 |
||
701 | 362 |
quo:something |
363 |
"Return the integer quotient of dividing the receiver by the argument |
|
6067 | 364 |
with truncation towards zero. |
365 |
The following is always true: |
|
10326 | 366 |
(receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver |
6067 | 367 |
" |
701 | 368 |
|
369 |
^ (self / something) truncated |
|
370 |
! |
|
371 |
||
372 |
reciprocal |
|
373 |
"return the receivers reciprocal" |
|
374 |
||
375 |
^ self class unity / self |
|
376 |
! |
|
1 | 377 |
|
701 | 378 |
rem:something |
379 |
"Return the integer remainder of dividing the receiver by the argument |
|
380 |
with truncation towards zero. |
|
6067 | 381 |
The remainder has the same sign as the receiver. |
382 |
The following is always true: |
|
10326 | 383 |
(receiver quo: something) * something + (receiver rem: something) = receiver |
6067 | 384 |
" |
701 | 385 |
|
386 |
^ self - ((self quo:something) * something) |
|
7440 | 387 |
! |
388 |
||
389 |
uncheckedDivide:aNumber |
|
390 |
"return the quotient of the receiver and the argument, aNumber. |
|
7471 | 391 |
Do not check for divide by zero (return NaN or Infinity). |
7440 | 392 |
This operation is provided for emulators of other languages/semantics, |
393 |
where no exception is raised for these results (i.e. Java). |
|
394 |
Its only defined if the arguments type is the same as the receivers." |
|
395 |
||
396 |
aNumber isZero ifTrue:[ |
|
10326 | 397 |
self isZero ifTrue:[^ self class NaN]. |
398 |
self negative ifTrue:[^ self class negativeInfinity]. |
|
399 |
^ self class infinity. |
|
7440 | 400 |
]. |
401 |
^ self / aNumber |
|
701 | 402 |
! ! |
403 |
||
5238 | 404 |
!ArithmeticValue methodsFor:'coercing & converting'! |
1888 | 405 |
|
406 |
coerce:aNumber |
|
11725 | 407 |
"convert the argument aNumber into an instance of the receivers class and return it." |
1888 | 408 |
|
409 |
^ self subclassResponsibility |
|
410 |
! |
|
411 |
||
412 |
generality |
|
413 |
"return a number giving the receivers generality, that number is |
|
10326 | 414 |
used to convert one of the arguments in a mixed expression. |
1888 | 415 |
The generality has to be defined in subclasses, |
10326 | 416 |
such that gen(a) > gen(b) iff, conversion of b into a's class |
1888 | 417 |
does not cut precision. For example, Integer has 40, Float has 80, |
418 |
meaning that if we convert a Float to an Integer, some precision may |
|
419 |
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
|
420 |
which converts the lower-precision number to the higher precision |
1888 | 421 |
numbers class, when mixed-type arithmetic is performed." |
10326 | 422 |
|
1888 | 423 |
^ self subclassResponsibility |
424 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
425 |
"Modified: / 5.11.1996 / 15:05:30 / cg" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
426 |
"Modified: / 13.2.1998 / 15:36:01 / stefan" |
1888 | 427 |
! |
428 |
||
429 |
retry:aSymbol coercing:aNumber |
|
430 |
"arithmetic represented by the binary operator, aSymbol, |
|
10326 | 431 |
could not be performed with the receiver and the argument, aNumber, |
432 |
because of the differences in representation. |
|
433 |
Coerce either the receiver or the argument, depending on which has higher |
|
434 |
generality, and try again. |
|
1888 | 435 |
If the operation is compare for same value (=), return false if |
10326 | 436 |
the argument is not a Number. |
1888 | 437 |
If the generalities are the same, create an error message, since this |
438 |
means that a subclass has not been fully implemented." |
|
439 |
||
440 |
|hasGenerality myGenerality otherGenerality| |
|
441 |
||
442 |
hasGenerality := aNumber respondsTo:#generality. |
|
443 |
hasGenerality ifFalse:[ |
|
10326 | 444 |
(aSymbol == #=) ifTrue:[ |
445 |
^ false |
|
446 |
]. |
|
447 |
(aSymbol == #~=) ifTrue:[ |
|
448 |
^ true |
|
449 |
]. |
|
450 |
^ self error:'retry:coercing: argument is not a number'. |
|
1888 | 451 |
]. |
452 |
||
453 |
myGenerality := self generality. |
|
454 |
otherGenerality := aNumber generality. |
|
455 |
(myGenerality > otherGenerality) ifTrue:[ |
|
10326 | 456 |
^ self perform:aSymbol with:(self coerce:aNumber) |
1888 | 457 |
]. |
458 |
(myGenerality < otherGenerality) ifTrue:[ |
|
10326 | 459 |
^ (aNumber coerce:self) perform:aSymbol with:aNumber |
1888 | 460 |
]. |
10324 | 461 |
self error:'retry:coercing: oops - same generality; retry should not happen' |
1888 | 462 |
|
463 |
"Modified: 5.11.1996 / 15:03:38 / cg" |
|
464 |
! ! |
|
465 |
||
701 | 466 |
!ArithmeticValue methodsFor:'converting'! |
467 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
468 |
asDouble |
7429 | 469 |
"ST80 compatibility: return a double with receivers value. |
470 |
our floats are the identical to ST80 doubles" |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
471 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
472 |
^ self asFloat |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
473 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
474 |
"Created: / 13.2.1998 / 15:40:14 / stefan" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
475 |
! |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
476 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
477 |
asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
478 |
"return the receiver as fixedPoint number. |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
479 |
Q: what should the scale be here ?" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
480 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
481 |
^ self asFraction asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
482 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
483 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
484 |
0.3 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
485 |
0.5 asFixedPoint |
10326 | 486 |
(1/5) asFloat asFixedPoint |
487 |
(1/3) asFloat asFixedPoint |
|
488 |
(2/3) asFloat asFixedPoint |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
489 |
(1/8) asFloat asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
490 |
3.14159 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
491 |
0.0000001 asFraction |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
492 |
0.0000001 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
493 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
494 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
495 |
"Modified: / 25.10.1997 / 15:36:54 / cg" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
496 |
! |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
497 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
498 |
asFixedPoint:scale |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
499 |
"return the receiver as fixedPoint number with the given |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
500 |
number of post-decimal-digits." |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
501 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
502 |
^ self asFraction asFixedPoint:scale |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
503 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
504 |
" |
10326 | 505 |
0.3 asFixedPoint:4 |
506 |
0.3 asFixedPoint:3 |
|
507 |
0.3 asFixedPoint:2 |
|
508 |
0.3 asFixedPoint:1 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
509 |
0.3 asFixedPoint:0 |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
510 |
|
10326 | 511 |
0.5 asFixedPoint:3 |
512 |
(1/5) asFloat asFixedPoint:1 |
|
513 |
(1/8) asFloat asFixedPoint:1 |
|
514 |
1.0 asFixedPoint:2 |
|
515 |
3.14159 asFixedPoint:2 |
|
516 |
3.14159 asFixedPoint:3 |
|
517 |
(3.14159 asFixedPoint:2) asFixedPoint:5 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
518 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
519 |
! |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
520 |
|
1 | 521 |
asFloat |
522 |
"return a float with same value" |
|
523 |
||
524 |
^ self subclassResponsibility |
|
525 |
! |
|
526 |
||
5986 | 527 |
asFloatD |
528 |
"return a double precision float with same value. |
|
529 |
Added for ANSI compatibility" |
|
530 |
||
531 |
^ self asFloat |
|
532 |
||
533 |
"Created: / 7.9.2001 / 13:36:48 / cg" |
|
534 |
"Modified: / 7.9.2001 / 13:38:10 / cg" |
|
535 |
! |
|
536 |
||
537 |
asFloatE |
|
538 |
"return a single precision float with same value. |
|
539 |
Added for ANSI compatibility" |
|
540 |
||
541 |
^ self asShortFloat |
|
542 |
||
543 |
"Created: / 7.9.2001 / 13:37:06 / cg" |
|
544 |
"Modified: / 7.9.2001 / 13:38:15 / cg" |
|
545 |
! |
|
546 |
||
547 |
asFloatQ |
|
548 |
"return a quad precision float with same value. |
|
549 |
Added for ANSI compatibility" |
|
550 |
||
551 |
^ self asLongFloat |
|
552 |
||
553 |
"Created: / 7.9.2001 / 13:38:44 / cg" |
|
554 |
! |
|
555 |
||
1 | 556 |
asFraction |
557 |
"return a fraction with same value" |
|
558 |
||
559 |
^ self subclassResponsibility |
|
560 |
! |
|
561 |
||
701 | 562 |
asInteger |
563 |
"return an integer with same value - might truncate" |
|
564 |
||
565 |
^ self truncated |
|
566 |
! |
|
567 |
||
7378 | 568 |
asLimitedPrecisionReal |
569 |
"return a float of any precision with same value" |
|
570 |
||
571 |
^ self asFloat |
|
572 |
! |
|
573 |
||
5986 | 574 |
asLongFloat |
575 |
"return a longFloat with same value" |
|
576 |
||
10315 | 577 |
"WARNING: could loose precision here, if not redefined in concrete classes which |
578 |
have more than float precision (i.e. LargeIntegers and Fractions)" |
|
7355 | 579 |
|
10315 | 580 |
^ self asFloat asLongFloat "/ subclassResponsibility |
5986 | 581 |
|
582 |
"Modified: / 17.4.1996 / 12:21:35 / cg" |
|
583 |
"Created: / 7.9.2001 / 13:39:31 / cg" |
|
584 |
! |
|
585 |
||
5987 | 586 |
asScaledDecimal:scale |
587 |
"return a fixedPoint approximating the receivers value" |
|
588 |
||
589 |
^ self asFixedPoint:scale |
|
590 |
||
591 |
" |
|
592 |
1.234 asScaledDecimal:2 |
|
593 |
" |
|
594 |
||
595 |
"Created: / 7.9.2001 / 13:46:26 / cg" |
|
596 |
"Modified: / 7.9.2001 / 13:46:44 / cg" |
|
597 |
! |
|
598 |
||
1200 | 599 |
asShortFloat |
600 |
"return a shortFloat with same value" |
|
601 |
||
602 |
^ self asFloat asShortFloat |
|
603 |
||
604 |
"Modified: 17.4.1996 / 12:21:35 / cg" |
|
605 |
! |
|
606 |
||
701 | 607 |
degreesToRadians |
608 |
"interpreting the receiver as radians, return the degrees" |
|
609 |
||
610 |
^ self asFloat degreesToRadians |
|
611 |
! |
|
612 |
||
613 |
radiansToDegrees |
|
614 |
"interpreting the receiver as degrees, return the radians" |
|
615 |
||
616 |
^ self asFloat radiansToDegrees |
|
1 | 617 |
! ! |
618 |
||
701 | 619 |
!ArithmeticValue methodsFor:'double dispatching'! |
1 | 620 |
|
7355 | 621 |
differenceFromComplex:aComplex |
622 |
"the receiver does not know how to subtract from a complex - |
|
623 |
retry the operation by coercing to higher generality" |
|
624 |
||
625 |
^ aComplex retry:#- coercing:self |
|
626 |
! |
|
627 |
||
1888 | 628 |
differenceFromFixedPoint:aFixedPoint |
629 |
"the receiver does not know how to subtract from a fixedPoint number - |
|
630 |
retry the operation by coercing to higher generality" |
|
631 |
||
632 |
^ aFixedPoint retry:#- coercing:self |
|
633 |
||
634 |
"Created: 5.11.1996 / 14:55:51 / cg" |
|
635 |
! |
|
636 |
||
701 | 637 |
differenceFromFloat:aFloat |
638 |
"the receiver does not know how to subtract from a float - |
|
639 |
retry the operation by coercing to higher generality" |
|
1 | 640 |
|
701 | 641 |
^ aFloat retry:#- coercing:self |
1 | 642 |
! |
643 |
||
701 | 644 |
differenceFromFraction:aFraction |
645 |
"the receiver does not know how to subtract from a fraction - |
|
646 |
retry the operation by coercing to higher generality" |
|
1 | 647 |
|
701 | 648 |
^ aFraction retry:#- coercing:self |
1 | 649 |
! |
650 |
||
701 | 651 |
differenceFromInteger:anInteger |
652 |
"the receiver does not know how to subtract from an integer - |
|
653 |
retry the operation by coercing to higher generality" |
|
1 | 654 |
|
701 | 655 |
^ anInteger retry:#- coercing:self |
1 | 656 |
! |
657 |
||
7440 | 658 |
differenceFromLargeFloat:aLargeFloat |
659 |
"the receiver does not know how to subtract from a largeFloat - |
|
660 |
retry the operation by coercing to higher generality" |
|
661 |
||
662 |
^ aLargeFloat retry:#- coercing:self |
|
663 |
! |
|
664 |
||
7399 | 665 |
differenceFromLongFloat:aLongFloat |
666 |
"the receiver does not know how to subtract from a longFloat - |
|
667 |
retry the operation by coercing to higher generality" |
|
668 |
||
669 |
^ aLongFloat retry:#- coercing:self |
|
670 |
||
671 |
"Created: 17.4.1996 / 12:33:16 / cg" |
|
672 |
! |
|
673 |
||
1200 | 674 |
differenceFromShortFloat:aShortFloat |
675 |
"the receiver does not know how to subtract from a shortFloat - |
|
676 |
retry the operation by coercing to higher generality" |
|
677 |
||
678 |
^ aShortFloat retry:#- coercing:self |
|
679 |
||
680 |
"Created: 17.4.1996 / 12:33:16 / cg" |
|
681 |
! |
|
682 |
||
7355 | 683 |
equalFromComplex:aComplex |
684 |
"the receiver does not know how to compare to a complex number - |
|
685 |
retry the operation by coercing to higher generality" |
|
686 |
||
687 |
^ aComplex retry:#= coercing:self |
|
688 |
! |
|
689 |
||
690 |
equalFromFixedPoint:aFixedPoint |
|
691 |
"the receiver does not know how to compare to a fixed point - |
|
692 |
retry the operation by coercing to higher generality" |
|
693 |
||
694 |
^ aFixedPoint retry:#= coercing:self |
|
695 |
! |
|
696 |
||
697 |
equalFromFloat:aFloat |
|
698 |
"the receiver does not know how to compare to a float - |
|
699 |
retry the operation by coercing to higher generality" |
|
700 |
||
701 |
^ aFloat retry:#= coercing:self |
|
702 |
! |
|
703 |
||
704 |
equalFromFraction:aFraction |
|
705 |
"the receiver does not know how to compare to a fraction - |
|
706 |
retry the operation by coercing to higher generality" |
|
707 |
||
708 |
^ aFraction retry:#= coercing:self |
|
709 |
! |
|
710 |
||
711 |
equalFromInteger:anInteger |
|
712 |
"the receiver does not know how to compare to an integer - |
|
713 |
retry the operation by coercing to higher generality" |
|
714 |
||
715 |
^ anInteger retry:#= coercing:self |
|
716 |
! |
|
717 |
||
7440 | 718 |
equalFromLargeFloat:aLargeFloat |
719 |
"the receiver does not know how to compare to a large float - |
|
720 |
retry the operation by coercing to higher generality" |
|
721 |
||
722 |
^ aLargeFloat retry:#= coercing:self |
|
723 |
! |
|
724 |
||
7355 | 725 |
equalFromLongFloat:aLongFloat |
726 |
"the receiver does not know how to compare to a long float - |
|
727 |
retry the operation by coercing to higher generality" |
|
728 |
||
729 |
^ aLongFloat retry:#= coercing:self |
|
730 |
! |
|
731 |
||
732 |
equalFromShortFloat:aShortFloat |
|
733 |
"the receiver does not know how to compare to a short float - |
|
734 |
retry the operation by coercing to higher generality" |
|
735 |
||
736 |
^ aShortFloat retry:#= coercing:self |
|
737 |
! |
|
738 |
||
1888 | 739 |
lessFromFixedPoint:aFixedPoint |
740 |
"the receiver does not know how to compare to a fixedPoint number - |
|
741 |
retry the operation by coercing to higher generality" |
|
742 |
||
743 |
^ aFixedPoint retry:#< coercing:self |
|
744 |
||
745 |
"Created: 5.11.1996 / 14:56:12 / cg" |
|
746 |
! |
|
747 |
||
701 | 748 |
lessFromFloat:aFloat |
749 |
"the receiver does not know how to compare to a float - |
|
750 |
retry the operation by coercing to higher generality" |
|
1 | 751 |
|
701 | 752 |
^ aFloat retry:#< coercing:self |
1 | 753 |
! |
754 |
||
701 | 755 |
lessFromFraction:aFraction |
756 |
"the receiver does not know how to compare to a fraction - |
|
757 |
retry the operation by coercing to higher generality" |
|
1 | 758 |
|
701 | 759 |
^ aFraction retry:#< coercing:self |
1 | 760 |
! |
761 |
||
701 | 762 |
lessFromInteger:anInteger |
763 |
"the receiver does not know how to compare to an integer - |
|
764 |
retry the operation by coercing to higher generality" |
|
1 | 765 |
|
701 | 766 |
^ anInteger retry:#< coercing:self |
1 | 767 |
! |
768 |
||
7440 | 769 |
lessFromLargeFloat:aLargeFloat |
770 |
"the receiver does not know how to compare to a largeFloat - |
|
771 |
retry the operation by coercing to higher generality" |
|
772 |
||
773 |
^ aLargeFloat retry:#< coercing:self |
|
774 |
! |
|
775 |
||
7399 | 776 |
lessFromLongFloat:aLongFloat |
777 |
"the receiver does not know how to compare to a longFloat - |
|
778 |
retry the operation by coercing to higher generality" |
|
779 |
||
780 |
^ aLongFloat retry:#< coercing:self |
|
781 |
||
782 |
"Modified: 17.4.1996 / 12:33:33 / cg" |
|
783 |
! |
|
784 |
||
1200 | 785 |
lessFromShortFloat:aShortFloat |
786 |
"the receiver does not know how to compare to a shortFloat - |
|
787 |
retry the operation by coercing to higher generality" |
|
788 |
||
789 |
^ aShortFloat retry:#< coercing:self |
|
790 |
||
791 |
"Modified: 17.4.1996 / 12:33:33 / cg" |
|
792 |
! |
|
793 |
||
7355 | 794 |
productFromComplex:aComplex |
795 |
"the receiver does not know how to multiply a complex - |
|
796 |
retry the operation by coercing to higher generality" |
|
797 |
||
798 |
^ aComplex retry:#* coercing:self |
|
799 |
! |
|
800 |
||
1888 | 801 |
productFromFixedPoint:aFixedPoint |
802 |
"the receiver does not know how to multiply a fixed point number - |
|
803 |
retry the operation by coercing to higher generality" |
|
804 |
||
805 |
^ aFixedPoint retry:#* coercing:self |
|
806 |
||
807 |
"Created: 5.11.1996 / 14:56:28 / cg" |
|
808 |
! |
|
809 |
||
701 | 810 |
productFromFloat:aFloat |
811 |
"the receiver does not know how to multiply a float - |
|
812 |
retry the operation by coercing to higher generality" |
|
1 | 813 |
|
701 | 814 |
^ aFloat retry:#* coercing:self |
1 | 815 |
! |
816 |
||
701 | 817 |
productFromFraction:aFraction |
818 |
"the receiver does not know how to multiply a fraction - |
|
819 |
retry the operation by coercing to higher generality" |
|
1 | 820 |
|
701 | 821 |
^ aFraction retry:#* coercing:self |
1 | 822 |
! |
823 |
||
701 | 824 |
productFromInteger:anInteger |
825 |
"the receiver does not know how to multiply an integer - |
|
826 |
retry the operation by coercing to higher generality" |
|
1 | 827 |
|
701 | 828 |
^ anInteger retry:#* coercing:self |
1 | 829 |
! |
830 |
||
7440 | 831 |
productFromLargeFloat:aLargeFloat |
832 |
"the receiver does not know how to multiply a largeFloat - |
|
833 |
retry the operation by coercing to higher generality" |
|
834 |
||
835 |
^ aLargeFloat retry:#* coercing:self |
|
836 |
||
837 |
"Created: 17.4.1996 / 12:33:48 / cg" |
|
838 |
! |
|
839 |
||
7399 | 840 |
productFromLongFloat:aLongFloat |
841 |
"the receiver does not know how to multiply a longFloat - |
|
842 |
retry the operation by coercing to higher generality" |
|
843 |
||
844 |
^ aLongFloat retry:#* coercing:self |
|
845 |
||
846 |
"Created: 17.4.1996 / 12:33:48 / cg" |
|
847 |
! |
|
848 |
||
1200 | 849 |
productFromShortFloat:aShortFloat |
850 |
"the receiver does not know how to multiply a shortFloat - |
|
851 |
retry the operation by coercing to higher generality" |
|
852 |
||
853 |
^ aShortFloat retry:#* coercing:self |
|
854 |
||
855 |
"Created: 17.4.1996 / 12:33:48 / cg" |
|
856 |
! |
|
857 |
||
7355 | 858 |
quotientFromComplex:aComplex |
7440 | 859 |
"aComplex does not know how to divide by the receiver - |
7355 | 860 |
retry the operation by coercing to higher generality" |
861 |
||
862 |
^ aComplex retry:#/ coercing:self |
|
863 |
! |
|
864 |
||
1888 | 865 |
quotientFromFixedPoint:aFixedPoint |
7440 | 866 |
"aFixedPoint does not know how to divide by the receiver - |
1888 | 867 |
retry the operation by coercing to higher generality" |
868 |
||
869 |
^ aFixedPoint retry:#/ coercing:self |
|
870 |
||
871 |
"Created: 5.11.1996 / 14:56:41 / cg" |
|
872 |
! |
|
873 |
||
701 | 874 |
quotientFromFloat:aFloat |
7440 | 875 |
"aFloat does not know how to divide by the receiver - |
701 | 876 |
retry the operation by coercing to higher generality" |
1 | 877 |
|
701 | 878 |
^ aFloat retry:#/ coercing:self |
1 | 879 |
! |
880 |
||
701 | 881 |
quotientFromFraction:aFraction |
7440 | 882 |
"aFraction does not know how to divide by the receiver - |
701 | 883 |
retry the operation by coercing to higher generality" |
1 | 884 |
|
701 | 885 |
^ aFraction retry:#/ coercing:self |
1 | 886 |
! |
887 |
||
701 | 888 |
quotientFromInteger:anInteger |
7440 | 889 |
"anInteger does not know how to divide by the receiver - |
1 | 890 |
retry the operation by coercing to higher generality" |
891 |
||
701 | 892 |
^ anInteger retry:#/ coercing:self |
1 | 893 |
! |
894 |
||
7440 | 895 |
quotientFromLargeFloat:aLargeFloat |
896 |
"aLargeFloat does not know how to divide by the receiver - |
|
897 |
retry the operation by coercing to higher generality" |
|
898 |
||
899 |
^ aLargeFloat retry:#/ coercing:self |
|
900 |
||
901 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
902 |
! |
|
903 |
||
7399 | 904 |
quotientFromLongFloat:aLongFloat |
7440 | 905 |
"aLongFloat does not know how to divide by the receiver - |
7399 | 906 |
retry the operation by coercing to higher generality" |
907 |
||
908 |
^ aLongFloat retry:#/ coercing:self |
|
909 |
||
910 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
911 |
! |
|
912 |
||
1200 | 913 |
quotientFromShortFloat:aShortFloat |
7440 | 914 |
"aShortFloat does not know how to divide by the receiver - |
1200 | 915 |
retry the operation by coercing to higher generality" |
916 |
||
917 |
^ aShortFloat retry:#/ coercing:self |
|
918 |
||
919 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
920 |
! |
|
921 |
||
7355 | 922 |
sumFromComplex:aComplex |
923 |
"the receiver does not know how to add a complex - |
|
924 |
retry the operation by coercing to higher generality" |
|
925 |
||
926 |
^ aComplex retry:#+ coercing:self |
|
927 |
! |
|
928 |
||
1888 | 929 |
sumFromFixedPoint:aFixedPoint |
930 |
"the receiver does not know how to add a fixed point number - |
|
931 |
retry the operation by coercing to higher generality" |
|
932 |
||
933 |
^ aFixedPoint retry:#+ coercing:self |
|
934 |
||
935 |
"Created: 5.11.1996 / 14:56:56 / cg" |
|
936 |
! |
|
937 |
||
1 | 938 |
sumFromFloat:aFloat |
939 |
"the receiver does not know how to add a float - |
|
940 |
retry the operation by coercing to higher generality" |
|
941 |
||
942 |
^ aFloat retry:#+ coercing:self |
|
943 |
! |
|
944 |
||
945 |
sumFromFraction:aFraction |
|
946 |
"the receiver does not know how to add a fraction - |
|
947 |
retry the operation by coercing to higher generality" |
|
948 |
||
949 |
^ aFraction retry:#+ coercing:self |
|
950 |
! |
|
951 |
||
701 | 952 |
sumFromInteger:anInteger |
953 |
"the receiver does not know how to add an integer - |
|
1 | 954 |
retry the operation by coercing to higher generality" |
955 |
||
701 | 956 |
^ anInteger retry:#+ coercing:self |
1200 | 957 |
! |
958 |
||
7440 | 959 |
sumFromLargeFloat:aLargeFloat |
960 |
"the receiver does not know how to add a largeFloat - |
|
961 |
retry the operation by coercing to higher generality" |
|
962 |
||
963 |
^ aLargeFloat retry:#+ coercing:self |
|
964 |
||
965 |
"Created: 17.4.1996 / 12:34:10 / cg" |
|
966 |
! |
|
967 |
||
7399 | 968 |
sumFromLongFloat:aLongFloat |
969 |
"the receiver does not know how to add a longFloat - |
|
970 |
retry the operation by coercing to higher generality" |
|
971 |
||
972 |
^ aLongFloat retry:#+ coercing:self |
|
973 |
||
974 |
"Created: 17.4.1996 / 12:34:10 / cg" |
|
975 |
! |
|
976 |
||
1200 | 977 |
sumFromShortFloat:aShortFloat |
978 |
"the receiver does not know how to add a shortFloat - |
|
979 |
retry the operation by coercing to higher generality" |
|
980 |
||
981 |
^ aShortFloat retry:#+ coercing:self |
|
982 |
||
983 |
"Created: 17.4.1996 / 12:34:10 / cg" |
|
1 | 984 |
! ! |
985 |
||
7384 | 986 |
!ArithmeticValue methodsFor:'mathematical functions'! |
1 | 987 |
|
10326 | 988 |
** aNumber |
9443 | 989 |
"Answer the receiver raised to the power of the argument, aNumber." |
990 |
"same as Number>>raisedTo:" |
|
991 |
||
992 |
^self raisedTo: aNumber |
|
993 |
||
994 |
"Created: / 15-07-2006 / 15:11:05 / cg" |
|
995 |
"Modified: / 17-07-2006 / 12:51:33 / cg" |
|
996 |
! |
|
997 |
||
7635 | 998 |
raisedTo: aNumber |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
999 |
aNumber isInteger ifTrue:[ |
10326 | 1000 |
^ self raisedToInteger:aNumber |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1001 |
]. |
7635 | 1002 |
^ self subclassResponsibility |
6858 | 1003 |
! |
1004 |
||
10326 | 1005 |
raisedToInteger:exp |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1006 |
"return the receiver raised to exp" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1007 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1008 |
|result e t| |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1009 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1010 |
"use the addition chaining algorithm, |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1011 |
which is much faster for big exp-arguments" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1012 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1013 |
result := 1. |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1014 |
t := self. |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1015 |
exp < 0 ifTrue:[ |
10326 | 1016 |
e := exp negated. |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1017 |
] ifFalse:[ |
10326 | 1018 |
e := exp. |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1019 |
]. |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1020 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1021 |
[e ~~ 0] whileTrue:[ |
10326 | 1022 |
[(e bitAnd:1) == 0] whileTrue:[ |
1023 |
e := e bitShift:-1. |
|
1024 |
t := t * t. |
|
1025 |
]. |
|
1026 |
e := e - 1. |
|
1027 |
result := result * t. |
|
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1028 |
]. |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1029 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1030 |
(exp < 0) ifTrue:[ |
10326 | 1031 |
^ 1 / result |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1032 |
]. |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1033 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1034 |
^ result |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1035 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1036 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1037 |
" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1038 |
(2 raisedToInteger:216) |
10326 | 1039 |
(2 raisedTo:216) |
1040 |
-> 105312291668557186697918027683670432318895095400549111254310977536 |
|
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1041 |
|
10326 | 1042 |
(2 raisedToInteger:216) asFloat |
1043 |
(2 raisedTo:216) asFloat |
|
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1044 |
-> 1.05312E+65 |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1045 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1046 |
(2 raisedToInteger:500) |
10326 | 1047 |
(2 raisedTo:500) |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1048 |
-> 3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376 |
10326 | 1049 |
2 raisedToInteger:10 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1050 |
-> 1024 |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1051 |
-2 raisedToInteger:10 |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1052 |
-> 1024 |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1053 |
-2 raisedToInteger:9 |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1054 |
-> -512 |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1055 |
10 raisedToInteger:-10 |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1056 |
-> (1/10000000000) |
10326 | 1057 |
2 raisedToInteger:0 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1058 |
-> 1 |
10326 | 1059 |
2 raisedToInteger:-1 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1060 |
-> (1/2) |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1061 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1062 |
Time millisecondsToRun:[ |
10326 | 1063 |
10000 timesRepeat:[ |
1064 |
(2 raisedToInteger:500) |
|
1065 |
] |
|
1066 |
] |
|
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1067 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1068 |
Time millisecondsToRun:[ |
10326 | 1069 |
|bigNum| |
1070 |
bigNum := 2 raisedToInteger:500. |
|
1071 |
10 timesRepeat:[ |
|
1072 |
(bigNum raisedToInteger:500) |
|
1073 |
] |
|
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1074 |
] |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1075 |
" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1076 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1077 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1078 |
"Modified: / 27.4.1999 / 16:16:11 / stefan" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1079 |
! |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1080 |
|
701 | 1081 |
squared |
1082 |
"return receiver * receiver" |
|
1083 |
||
1084 |
^ self * self |
|
1085 |
! ! |
|
1086 |
||
1087 |
!ArithmeticValue methodsFor:'queries'! |
|
1088 |
||
1089 |
respondsToArithmetic |
|
1090 |
"return true, if the receiver responds to arithmetic messages" |
|
1091 |
||
1092 |
^ true |
|
1093 |
! ! |
|
1094 |
||
1095 |
!ArithmeticValue methodsFor:'testing'! |
|
1096 |
||
1097 |
denominator |
|
1098 |
"return the denominator of the receiver" |
|
1099 |
||
1100 |
^ 1 |
|
1101 |
! |
|
1102 |
||
1103 |
even |
|
1104 |
"return true if the receiver is divisible by 2" |
|
1105 |
||
1106 |
^ self truncated asInteger even |
|
1107 |
! |
|
1108 |
||
3635 | 1109 |
isComplex |
7355 | 1110 |
"Answer whether the receiver has an imaginary part |
1111 |
(i.e. if it is a complex number). Always false here." |
|
3635 | 1112 |
|
1113 |
^ false |
|
1114 |
||
1115 |
"Modified: / 9.7.1998 / 10:19:27 / cg" |
|
1116 |
! |
|
1117 |
||
8979
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1118 |
isFinite |
10326 | 1119 |
"return true, if the receiver is finite |
9400 | 1120 |
i.e. it can be represented as a rational number." |
1121 |
||
8979
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1122 |
^ true |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1123 |
! |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1124 |
|
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1125 |
isInfinite |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1126 |
^ false |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1127 |
! |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1128 |
|
7440 | 1129 |
isNegativeInfinity |
1130 |
^ self negative and:[self isInfinite] |
|
1131 |
! |
|
1132 |
||
1133 |
isNegativeZero |
|
1134 |
"return false - must be redefined by subclasses which can represent a negative zero |
|
1135 |
(i.e. limitedPrecisionReal classes)" |
|
1136 |
||
1137 |
^ false |
|
1138 |
! |
|
1139 |
||
1140 |
isPositiveInfinity |
|
1141 |
^ self positive and:[self isInfinite] |
|
1142 |
! |
|
1143 |
||
6156 | 1144 |
isReal |
1145 |
"return true, if the receiver is some kind of real number (as opposed to a complex); |
|
1146 |
false is returned here - the method is only redefined in Number (and Complex)." |
|
1147 |
||
1148 |
^ false |
|
1149 |
||
1150 |
"Modified: / 6.11.2001 / 13:20:20 / cg" |
|
1151 |
"Created: / 6.11.2001 / 13:26:29 / cg" |
|
1152 |
! |
|
1153 |
||
7440 | 1154 |
isZero |
1155 |
"return false - must be redefined by subclasses which can represent a negative zero |
|
1156 |
(i.e. limitedPrecisionReal classes)" |
|
701 | 1157 |
|
7440 | 1158 |
^ self = self class zero |
1159 |
! |
|
1160 |
||
1161 |
negative |
|
1162 |
"return true if the receiver is less than zero" |
|
1163 |
||
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
1164 |
^ self < self class zero |
701 | 1165 |
! |
1166 |
||
1167 |
numerator |
|
1168 |
"return the numerator of the receiver." |
|
1169 |
||
1170 |
^ self |
|
1171 |
! |
|
1172 |
||
1173 |
odd |
|
1174 |
"return true if the receiver is not divisible by 2" |
|
1175 |
||
1176 |
^ self even not |
|
1177 |
! |
|
1178 |
||
1179 |
positive |
|
1180 |
"return true, if the receiver is >= 0" |
|
1181 |
||
1182 |
^ self negative not |
|
1183 |
! |
|
1184 |
||
1185 |
sign |
|
7471 | 1186 |
"return the sign of the receiver (-1, 0 or 1)" |
701 | 1187 |
|
7440 | 1188 |
|zero| |
1189 |
||
1190 |
zero := self class zero. |
|
1191 |
(self < zero) ifTrue:[^ -1]. |
|
1192 |
(zero < self) ifTrue:[^ 1]. |
|
701 | 1193 |
^ 0 |
1194 |
! |
|
1195 |
||
1196 |
strictlyPositive |
|
1197 |
"return true, if the receiver is > 0" |
|
1198 |
||
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
1199 |
^ self class zero < self |
1 | 1200 |
! ! |
1201 |
||
701 | 1202 |
!ArithmeticValue methodsFor:'truncation & rounding'! |
1 | 1203 |
|
701 | 1204 |
ceiling |
1205 |
"return the integer nearest the receiver towards positive infinity." |
|
1 | 1206 |
|
701 | 1207 |
|anInteger| |
1 | 1208 |
|
701 | 1209 |
anInteger := self // 1. "truncates towards negative infinity" |
1210 |
anInteger = self ifTrue:[^ anInteger]. |
|
1211 |
^ anInteger + 1 |
|
1 | 1212 |
! |
1213 |
||
701 | 1214 |
floor |
1215 |
"return the receiver truncated towards negative infinity" |
|
1 | 1216 |
|
701 | 1217 |
^ self // 1 |
1 | 1218 |
! |
1219 |
||
701 | 1220 |
roundTo:aNumber |
1221 |
"return the receiver rounded to multiples of aNumber" |
|
1 | 1222 |
|
701 | 1223 |
^ (self / aNumber) rounded * aNumber |
8449 | 1224 |
|
1225 |
" |
|
10326 | 1226 |
0 roundTo:4 |
8449 | 1227 |
1 roundTo:4 |
1228 |
2 roundTo:4 |
|
1229 |
3 roundTo:4 |
|
1230 |
4 roundTo:4 |
|
1231 |
5 roundTo:4 |
|
1232 |
6 roundTo:4 |
|
1233 |
7 roundTo:4 |
|
1234 |
" |
|
1235 |
! |
|
1236 |
||
1237 |
roundUpTo:aNumber |
|
1238 |
"return the receiver rounded up to the next multiple of aNumber" |
|
1239 |
||
1240 |
^ (self / aNumber) ceiling * aNumber |
|
1241 |
||
1242 |
" |
|
10326 | 1243 |
0 roundUpTo:4 |
1244 |
1 roundUpTo:4 |
|
1245 |
2 roundUpTo:4 |
|
1246 |
3 roundUpTo:4 |
|
1247 |
4 roundUpTo:4 |
|
1248 |
5 roundUpTo:4 |
|
1249 |
6 roundUpTo:4 |
|
1250 |
7 roundUpTo:4 |
|
1251 |
8 roundUpTo:4 |
|
10911 | 1252 |
(3@4) roundUpTo:8 |
1253 |
(3@4) roundUpTo:(5 @ 4) |
|
1254 |
(3@3) roundUpTo:(5 @ 4) |
|
8449 | 1255 |
" |
701 | 1256 |
! |
1257 |
||
1258 |
rounded |
|
1259 |
"return the integer nearest the receiver" |
|
1260 |
||
1881 | 1261 |
self negative ifTrue:[ |
10326 | 1262 |
^ (self - 0.5) ceiling |
1881 | 1263 |
]. |
701 | 1264 |
^ (self + 0.5) floor |
1881 | 1265 |
|
1266 |
"Modified: 5.11.1996 / 11:31:59 / cg" |
|
1 | 1267 |
! |
1268 |
||
701 | 1269 |
truncateTo:aNumber |
1270 |
"return the receiver truncated to multiples of aNumber" |
|
1 | 1271 |
|
701 | 1272 |
^ ((self / aNumber) floor * aNumber) asInteger |
1 | 1273 |
! |
1274 |
||
701 | 1275 |
truncated |
1276 |
"return the receiver truncated towards zero" |
|
1 | 1277 |
|
701 | 1278 |
self negative ifTrue:[ |
1279 |
^ self ceiling |
|
1280 |
]. |
|
1281 |
^ self floor |
|
1282 |
! ! |
|
1 | 1283 |
|
701 | 1284 |
!ArithmeticValue class methodsFor:'documentation'! |
1285 |
||
1286 |
version |
|
12576 | 1287 |
^ '$Header: /cvs/stx/stx/libbasic/ArithmeticValue.st,v 1.83 2009-12-01 22:08:29 cg Exp $' |
1288 |
! |
|
1289 |
||
1290 |
version_CVS |
|
1291 |
^ '$Header: /cvs/stx/stx/libbasic/ArithmeticValue.st,v 1.83 2009-12-01 22:08:29 cg Exp $' |
|
1 | 1292 |
! ! |
6858 | 1293 |
|
701 | 1294 |
ArithmeticValue initialize! |