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