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