author | Jan Vrany <jan.vrany@labware.com> |
Sat, 29 Oct 2022 01:05:05 +0100 | |
branch | jv |
changeset 25446 | 98fb817db6de |
parent 21242 | 19fabe339f8b |
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 |
||
20094 | 143 |
"/ Notice that we now use class based exceptions; |
7440 | 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 |
|
20090 | 147 |
"/ messages (accessors) instead of referring to the class variables. |
7440 | 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. |
|
19454 | 249 |
Abstract subclasses must redefine this again." |
11227 | 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 |
20337 | 282 |
truncation towards negative infinity. |
20338 | 283 |
|
284 |
Please be aware of the effect of truncation on negative receivers, |
|
20337 | 285 |
and understand the difference between '//' vs. 'quo:' |
286 |
and the corresponding '\\' vs. 'rem:'" |
|
701 | 287 |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
288 |
"/ Note: '^ (self / something) floor' may lead to infinite recursion |
1172 | 289 |
|
290 |
^ self subclassResponsibility |
|
291 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
292 |
"Modified: / 5.11.1996 / 11:46:27 / cg" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
293 |
"Modified: / 12.2.1998 / 16:31:36 / stefan" |
701 | 294 |
! |
295 |
||
296 |
\\ something |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
297 |
"return the receiver modulo something. |
17038 | 298 |
The remainder has the same sign as the argument, something. |
6067 | 299 |
The following is always true: |
17038 | 300 |
(receiver // something) * something + (receiver \\ something) = receiver |
20338 | 301 |
|
302 |
Please be aware of the effect of truncation on negative receivers, |
|
303 |
and understand the difference between '//' vs. 'quo:' |
|
304 |
and the corresponding '\\' vs. 'rem:'." |
|
1 | 305 |
|
701 | 306 |
^ self - ((self // something) * something) |
3134 | 307 |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
308 |
" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
309 |
0.9 \\ 0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
310 |
0.9 \\ -0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
311 |
-0.9 \\ 0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
312 |
-0.9 \\ -0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
313 |
" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
314 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
315 |
"Modified: / 12.2.1998 / 19:14:37 / stefan" |
701 | 316 |
! |
317 |
||
318 |
abs |
|
319 |
"return the absolute value of the receiver" |
|
320 |
||
321 |
(self negative) ifTrue:[^ self negated]. |
|
322 |
^ self |
|
323 |
! |
|
324 |
||
10326 | 325 |
dist:arg |
20696 | 326 |
"return the distance between the arg and the receiver." |
8757 | 327 |
|
328 |
^ (arg - self) abs |
|
329 |
||
330 |
" |
|
10326 | 331 |
(1%1) dist:(0%0) |
332 |
(1@1) dist:(0@0) |
|
333 |
(1) dist:(0) |
|
8757 | 334 |
" |
335 |
! |
|
336 |
||
16121 | 337 |
modulusOf:aNumber |
20353 | 338 |
"return aNumber modulo the receiver. |
16121 | 339 |
The remainder has the same sign as something. |
340 |
Defined for protocol compatibility with ModuloNumber." |
|
341 |
||
342 |
^ aNumber \\ self |
|
343 |
! |
|
344 |
||
701 | 345 |
negated |
346 |
"return the receiver negated" |
|
347 |
||
348 |
^ self class zero - self |
|
1 | 349 |
! |
350 |
||
701 | 351 |
quo:something |
352 |
"Return the integer quotient of dividing the receiver by the argument |
|
6067 | 353 |
with truncation towards zero. |
20338 | 354 |
|
355 |
Please be aware of the effect of truncation on negative receivers, |
|
356 |
and understand the difference between '//' vs. 'quo:' |
|
357 |
and the corresponding '\\' vs. 'rem:'. |
|
358 |
||
6067 | 359 |
The following is always true: |
20337 | 360 |
(receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver |
361 |
For positive results, this is the same as #//, |
|
362 |
for negative results, the remainder is ignored. |
|
363 |
I.e.: '9 // 4 = 2' and '-9 // 4 = -3' |
|
364 |
in contrast: '9 quo: 4 = 2' and '-9 quo: 4 = -2'" |
|
701 | 365 |
|
366 |
^ (self / something) truncated |
|
367 |
! |
|
368 |
||
369 |
reciprocal |
|
18835 | 370 |
"return the receiver's reciprocal" |
701 | 371 |
|
372 |
^ self class unity / self |
|
373 |
! |
|
1 | 374 |
|
701 | 375 |
rem:something |
376 |
"Return the integer remainder of dividing the receiver by the argument |
|
377 |
with truncation towards zero. |
|
6067 | 378 |
The remainder has the same sign as the receiver. |
379 |
The following is always true: |
|
20338 | 380 |
(receiver quo: something) * something + (receiver rem: something) = receiver |
381 |
||
382 |
Please be aware of the effect of truncation on negative receivers, |
|
383 |
and understand the difference between '//' vs. 'quo:' |
|
384 |
and the corresponding '\\' vs. 'rem:'." |
|
701 | 385 |
|
386 |
^ self - ((self quo:something) * something) |
|
7440 | 387 |
! |
388 |
||
389 |
uncheckedDivide:aNumber |
|
390 |
"return the quotient of the receiver and the argument, aNumber. |
|
7471 | 391 |
Do not check for divide by zero (return NaN or Infinity). |
7440 | 392 |
This operation is provided for emulators of other languages/semantics, |
393 |
where no exception is raised for these results (i.e. Java). |
|
18835 | 394 |
It is only defined if the argument's type is the same as the receiver's." |
7440 | 395 |
|
396 |
aNumber isZero ifTrue:[ |
|
18835 | 397 |
self isZero ifTrue:[^ self class NaN]. |
398 |
self negative ifTrue:[^ self class negativeInfinity]. |
|
399 |
^ self class infinity. |
|
7440 | 400 |
]. |
401 |
^ self / aNumber |
|
701 | 402 |
! ! |
403 |
||
12578 | 404 |
!ArithmeticValue methodsFor:'arithmetic destructive'! |
405 |
||
406 |
*= aNumber |
|
407 |
"Return the product of self multiplied by aNumber. |
|
408 |
The receiver MAY, but NEED NOT be changed to contain the product. |
|
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 / 11:46:11 / stefan" |
|
415 |
"Modified: / 28.4.1999 / 11:53:28 / stefan" |
|
416 |
! |
|
417 |
||
418 |
+= aNumber |
|
419 |
"Return the sum of self and aNumber. |
|
420 |
The receiver MAY, but NEED NOT be changed to contain the sum. |
|
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 / 10:13:41 / stefan" |
|
427 |
"Modified: / 28.4.1999 / 11:54:11 / stefan" |
|
428 |
! |
|
429 |
||
430 |
-= aNumber |
|
431 |
"Return the difference of self and aNumber. |
|
432 |
The receiver MAY, but NEED NOT be changed to contain the difference. |
|
433 |
So this method must be used as: 'a := a -= 5'. |
|
434 |
This method can be redefined for constructed datatypes to do optimisations" |
|
435 |
||
436 |
^ self - aNumber |
|
437 |
||
438 |
"Created: / 28.4.1999 / 10:13:58 / stefan" |
|
439 |
"Modified: / 28.4.1999 / 11:54:37 / stefan" |
|
440 |
! |
|
441 |
||
442 |
/= aNumber |
|
443 |
"Return the quotient of self and aNumber. |
|
444 |
The receiver MAY, but NEED NOT be changed to contain the quotient. |
|
445 |
So this method must be used as: 'a := a /= 5'. |
|
446 |
This method can be redefined for constructed datatypes to do optimisations" |
|
447 |
||
448 |
^ self / aNumber |
|
449 |
||
450 |
"Created: / 28.4.1999 / 11:46:22 / stefan" |
|
451 |
"Modified: / 28.4.1999 / 11:55:06 / stefan" |
|
452 |
! |
|
453 |
||
454 |
div2 |
|
455 |
"Return the quotient of self divided by 2. |
|
456 |
The receiver MAY, but NEED NOT be changed to contain the result. |
|
457 |
So this method must be used as: 'a := a div2. |
|
458 |
This method can be redefined for constructed datatypes to do optimisations" |
|
459 |
||
460 |
^ self // 2 |
|
461 |
||
462 |
"Created: / 28.4.1999 / 10:12:44 / stefan" |
|
463 |
"Modified: / 28.4.1999 / 11:56:09 / stefan" |
|
464 |
! |
|
465 |
||
466 |
mul2 |
|
467 |
"Return the product of self multiplied by 2. |
|
468 |
The receiver MAY, but NEED NOT be changed to contain the result. |
|
15279 | 469 |
So this method must be used as: a := a mul2. |
12578 | 470 |
This method can be redefined for constructed datatypes to do optimisations" |
471 |
||
472 |
^ self * 2 |
|
473 |
||
474 |
"Created: / 28.4.1999 / 10:12:55 / stefan" |
|
475 |
"Modified: / 28.4.1999 / 11:56:38 / stefan" |
|
476 |
! ! |
|
477 |
||
5238 | 478 |
!ArithmeticValue methodsFor:'coercing & converting'! |
1888 | 479 |
|
480 |
coerce:aNumber |
|
18835 | 481 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
1888 | 482 |
|
483 |
^ self subclassResponsibility |
|
484 |
! |
|
485 |
||
486 |
generality |
|
18835 | 487 |
"return a number giving the receiver's generality. |
488 |
That number is used to convert one of the arguments in a mixed expression. |
|
1888 | 489 |
The generality has to be defined in subclasses, |
10326 | 490 |
such that gen(a) > gen(b) iff, conversion of b into a's class |
18835 | 491 |
does not cut precision. |
492 |
For example, Integer has 40, Float has 80, meaning that if we convert a Float to an Integer, |
|
493 |
some precision may be lost. |
|
494 |
The generality is used by ArithmeticValue>>retry:coercing:, |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
495 |
which converts the lower-precision number to the higher precision |
18835 | 496 |
number's class, when mixed-type arithmetic is performed." |
10326 | 497 |
|
1888 | 498 |
^ self subclassResponsibility |
499 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
500 |
"Modified: / 5.11.1996 / 15:05:30 / cg" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
501 |
"Modified: / 13.2.1998 / 15:36:01 / stefan" |
1888 | 502 |
! |
503 |
||
504 |
retry:aSymbol coercing:aNumber |
|
505 |
"arithmetic represented by the binary operator, aSymbol, |
|
10326 | 506 |
could not be performed with the receiver and the argument, aNumber, |
507 |
because of the differences in representation. |
|
508 |
Coerce either the receiver or the argument, depending on which has higher |
|
509 |
generality, and try again. |
|
1888 | 510 |
If the operation is compare for same value (=), return false if |
10326 | 511 |
the argument is not a Number. |
1888 | 512 |
If the generalities are the same, create an error message, since this |
513 |
means that a subclass has not been fully implemented." |
|
514 |
||
515 |
|hasGenerality myGenerality otherGenerality| |
|
516 |
||
517 |
hasGenerality := aNumber respondsTo:#generality. |
|
518 |
hasGenerality ifFalse:[ |
|
20060 | 519 |
(aSymbol == #=) ifTrue:[ |
520 |
^ false |
|
521 |
]. |
|
522 |
(aSymbol == #~=) ifTrue:[ |
|
523 |
^ true |
|
524 |
]. |
|
525 |
^ self error:'retry:coercing: argument is not a number'. |
|
1888 | 526 |
]. |
527 |
||
528 |
myGenerality := self generality. |
|
529 |
otherGenerality := aNumber generality. |
|
530 |
(myGenerality > otherGenerality) ifTrue:[ |
|
20060 | 531 |
^ self perform:aSymbol with:(self coerce:aNumber) |
1888 | 532 |
]. |
533 |
(myGenerality < otherGenerality) ifTrue:[ |
|
20060 | 534 |
^ (aNumber coerce:self) perform:aSymbol with:aNumber |
1888 | 535 |
]. |
20060 | 536 |
"/ self error:'retry:coercing: oops - same generality; retry should not happen' |
537 |
"same generality may happen with Lazy and LazyValue" |
|
538 |
^ self perform:aSymbol with:aNumber |
|
539 |
||
540 |
" |
|
541 |
([3] lazyValue) * ([2] lazyValue) |
|
542 |
" |
|
1888 | 543 |
|
544 |
"Modified: 5.11.1996 / 15:03:38 / cg" |
|
16657 | 545 |
! |
546 |
||
547 |
retry:aSymbol coercing:aNumber with:anArgument |
|
548 |
"arithmetic represented by the binary operator, aSymbol, |
|
549 |
could not be performed with the receiver and the argument, aNumber, |
|
550 |
because of the differences in representation. |
|
551 |
Coerce either the receiver or the argument, depending on which has higher |
|
552 |
generality, and try again. |
|
553 |
If the operation is compare for same value (=), return false if |
|
554 |
the argument is not a Number. |
|
555 |
If the generalities are the same, create an error message, since this |
|
556 |
means that a subclass has not been fully implemented." |
|
557 |
||
558 |
|hasGenerality myGenerality otherGenerality| |
|
559 |
||
560 |
hasGenerality := aNumber respondsTo:#generality. |
|
561 |
hasGenerality ifFalse:[ |
|
562 |
^ self error:'retry:coercing: argument is not a number'. |
|
563 |
]. |
|
564 |
||
565 |
myGenerality := self generality. |
|
566 |
otherGenerality := aNumber generality. |
|
567 |
(myGenerality > otherGenerality) ifTrue:[ |
|
568 |
^ self perform:aSymbol with:(self coerce:aNumber) with:anArgument |
|
569 |
]. |
|
570 |
(myGenerality < otherGenerality) ifTrue:[ |
|
571 |
^ (aNumber coerce:self) perform:aSymbol with:aNumber with:anArgument |
|
572 |
]. |
|
20060 | 573 |
"/ self error:'retry:coercing: oops - same generality; retry should not happen' |
574 |
"same generality may happen with Lazy and LazyValue" |
|
575 |
^ self perform:aSymbol with:aNumber with:anArgument |
|
576 |
||
16657 | 577 |
|
578 |
"Modified: 5.11.1996 / 15:03:38 / cg" |
|
1888 | 579 |
! ! |
580 |
||
701 | 581 |
!ArithmeticValue methodsFor:'converting'! |
582 |
||
17222 | 583 |
as32BitIEEEFloatBytesMSB:msb |
584 |
^ self asShortFloat digitBytesMSB:msb |
|
585 |
||
586 |
" |
|
587 |
2 as32BitIEEEFloatBytesMSB:true |
|
588 |
2.0 as32BitIEEEFloatBytesMSB:true |
|
589 |
" |
|
590 |
! |
|
591 |
||
592 |
as64BitIEEEFloatBytesMSB:msb |
|
593 |
^ self asFloat digitBytesMSB:msb |
|
594 |
||
595 |
" |
|
596 |
2 as64BitIEEEFloatBytesMSB:true |
|
597 |
2.0 as64BitIEEEFloatBytesMSB:true |
|
598 |
" |
|
599 |
! |
|
600 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
601 |
asDouble |
18835 | 602 |
"ST80 compatibility: return a double with receiver's value. |
603 |
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
|
604 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
605 |
^ self asFloat |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
606 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
607 |
"Created: / 13.2.1998 / 15:40:14 / stefan" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
608 |
! |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
609 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
610 |
asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
611 |
"return the receiver as fixedPoint number. |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
612 |
Q: what should the scale be here ?" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
613 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
614 |
^ self asFraction asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
615 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
616 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
617 |
0.3 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
618 |
0.5 asFixedPoint |
10326 | 619 |
(1/5) asFloat asFixedPoint |
620 |
(1/3) asFloat asFixedPoint |
|
621 |
(2/3) asFloat asFixedPoint |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
622 |
(1/8) asFloat asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
623 |
3.14159 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
624 |
0.0000001 asFraction |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
625 |
0.0000001 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
626 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
627 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
628 |
"Modified: / 25.10.1997 / 15:36:54 / cg" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
629 |
! |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
630 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
631 |
asFixedPoint:scale |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
632 |
"return the receiver as fixedPoint number with the given |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
633 |
number of post-decimal-digits." |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
634 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
635 |
^ self asFraction asFixedPoint:scale |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
636 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
637 |
" |
10326 | 638 |
0.3 asFixedPoint:4 |
639 |
0.3 asFixedPoint:3 |
|
640 |
0.3 asFixedPoint:2 |
|
641 |
0.3 asFixedPoint:1 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
642 |
0.3 asFixedPoint:0 |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
643 |
|
10326 | 644 |
0.5 asFixedPoint:3 |
645 |
(1/5) asFloat asFixedPoint:1 |
|
646 |
(1/8) asFloat asFixedPoint:1 |
|
647 |
1.0 asFixedPoint:2 |
|
648 |
3.14159 asFixedPoint:2 |
|
649 |
3.14159 asFixedPoint:3 |
|
650 |
(3.14159 asFixedPoint:2) asFixedPoint:5 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
651 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
652 |
! |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
653 |
|
12980 | 654 |
asFixedPointRoundedToScale |
655 |
"return the receiver as fixedPoint number, rounded to its scale." |
|
656 |
||
657 |
^ self asFixedPoint roundedToScale |
|
658 |
||
659 |
" |
|
660 |
0.3 asFixedPoint |
|
661 |
0.5 asFixedPoint |
|
662 |
(2/3) asFloat asFixedPoint |
|
663 |
(1/8) asFloat asFixedPoint |
|
664 |
3.14159 asFixedPoint |
|
665 |
||
666 |
0.3 asFixedPointRoundedToScale |
|
667 |
0.5 asFixedPointRoundedToScale |
|
668 |
(2/3) asFloat asFixedPointRoundedToScale |
|
669 |
(1/8) asFloat asFixedPointRoundedToScale |
|
670 |
3.14159 asFixedPointRoundedToScale |
|
671 |
" |
|
672 |
||
673 |
"Created: / 02-08-2010 / 13:32:16 / cg" |
|
674 |
! |
|
675 |
||
676 |
asFixedPointRoundedToScale:scale |
|
677 |
"return the receiver as fixedPoint number with the given |
|
678 |
number of post-decimal-digits, rounded to its scale" |
|
679 |
||
680 |
^ (self asFixedPoint:scale) roundedToScale |
|
681 |
||
17336 | 682 |
" |
683 |
3.14159 asFixedPointRoundedToScale:1 |
|
684 |
3.14159 asFixedPointRoundedToScale:2 |
|
685 |
3.14159 asFixedPointRoundedToScale:3 |
|
686 |
3.14159 asFixedPointRoundedToScale:4 |
|
687 |
" |
|
688 |
||
12980 | 689 |
"Created: / 02-08-2010 / 13:33:11 / cg" |
690 |
! |
|
691 |
||
1 | 692 |
asFloat |
693 |
"return a float with same value" |
|
694 |
||
695 |
^ self subclassResponsibility |
|
696 |
! |
|
697 |
||
5986 | 698 |
asFloatD |
699 |
"return a double precision float with same value. |
|
700 |
Added for ANSI compatibility" |
|
701 |
||
702 |
^ self asFloat |
|
703 |
||
704 |
"Created: / 7.9.2001 / 13:36:48 / cg" |
|
705 |
"Modified: / 7.9.2001 / 13:38:10 / cg" |
|
706 |
! |
|
707 |
||
708 |
asFloatE |
|
709 |
"return a single precision float with same value. |
|
710 |
Added for ANSI compatibility" |
|
711 |
||
712 |
^ self asShortFloat |
|
713 |
||
714 |
"Created: / 7.9.2001 / 13:37:06 / cg" |
|
715 |
"Modified: / 7.9.2001 / 13:38:15 / cg" |
|
716 |
! |
|
717 |
||
718 |
asFloatQ |
|
719 |
"return a quad precision float with same value. |
|
720 |
Added for ANSI compatibility" |
|
721 |
||
722 |
^ self asLongFloat |
|
723 |
||
724 |
"Created: / 7.9.2001 / 13:38:44 / cg" |
|
725 |
! |
|
726 |
||
1 | 727 |
asFraction |
728 |
"return a fraction with same value" |
|
729 |
||
730 |
^ self subclassResponsibility |
|
731 |
! |
|
732 |
||
701 | 733 |
asInteger |
734 |
"return an integer with same value - might truncate" |
|
735 |
||
736 |
^ self truncated |
|
737 |
! |
|
738 |
||
7378 | 739 |
asLimitedPrecisionReal |
740 |
"return a float of any precision with same value" |
|
741 |
||
742 |
^ self asFloat |
|
743 |
! |
|
744 |
||
5986 | 745 |
asLongFloat |
746 |
"return a longFloat with same value" |
|
747 |
||
10315 | 748 |
"WARNING: could loose precision here, if not redefined in concrete classes which |
749 |
have more than float precision (i.e. LargeIntegers and Fractions)" |
|
7355 | 750 |
|
10315 | 751 |
^ self asFloat asLongFloat "/ subclassResponsibility |
5986 | 752 |
|
753 |
"Modified: / 17.4.1996 / 12:21:35 / cg" |
|
754 |
"Created: / 7.9.2001 / 13:39:31 / cg" |
|
755 |
! |
|
756 |
||
5987 | 757 |
asScaledDecimal:scale |
18835 | 758 |
"return a fixedPoint approximating the receiver's value" |
5987 | 759 |
|
760 |
^ self asFixedPoint:scale |
|
761 |
||
762 |
" |
|
763 |
1.234 asScaledDecimal:2 |
|
764 |
" |
|
765 |
||
766 |
"Created: / 7.9.2001 / 13:46:26 / cg" |
|
767 |
"Modified: / 7.9.2001 / 13:46:44 / cg" |
|
768 |
! |
|
769 |
||
1200 | 770 |
asShortFloat |
771 |
"return a shortFloat with same value" |
|
772 |
||
773 |
^ self asFloat asShortFloat |
|
774 |
||
775 |
"Modified: 17.4.1996 / 12:21:35 / cg" |
|
776 |
! |
|
777 |
||
701 | 778 |
degreesToRadians |
21106 | 779 |
"interpreting the receiver as degrees, return the radians" |
701 | 780 |
|
781 |
^ self asFloat degreesToRadians |
|
782 |
! |
|
783 |
||
784 |
radiansToDegrees |
|
21106 | 785 |
"interpreting the receiver as radians, return the degrees" |
701 | 786 |
|
787 |
^ self asFloat radiansToDegrees |
|
1 | 788 |
! ! |
789 |
||
701 | 790 |
!ArithmeticValue methodsFor:'double dispatching'! |
1 | 791 |
|
7355 | 792 |
differenceFromComplex:aComplex |
793 |
"the receiver does not know how to subtract from a complex - |
|
794 |
retry the operation by coercing to higher generality" |
|
795 |
||
796 |
^ aComplex retry:#- coercing:self |
|
797 |
! |
|
798 |
||
1888 | 799 |
differenceFromFixedPoint:aFixedPoint |
800 |
"the receiver does not know how to subtract from a fixedPoint number - |
|
801 |
retry the operation by coercing to higher generality" |
|
802 |
||
803 |
^ aFixedPoint retry:#- coercing:self |
|
804 |
||
805 |
"Created: 5.11.1996 / 14:55:51 / cg" |
|
806 |
! |
|
807 |
||
701 | 808 |
differenceFromFloat:aFloat |
809 |
"the receiver does not know how to subtract from a float - |
|
810 |
retry the operation by coercing to higher generality" |
|
1 | 811 |
|
701 | 812 |
^ aFloat retry:#- coercing:self |
1 | 813 |
! |
814 |
||
701 | 815 |
differenceFromFraction:aFraction |
816 |
"the receiver does not know how to subtract from a fraction - |
|
817 |
retry the operation by coercing to higher generality" |
|
1 | 818 |
|
701 | 819 |
^ aFraction retry:#- coercing:self |
1 | 820 |
! |
821 |
||
701 | 822 |
differenceFromInteger:anInteger |
823 |
"the receiver does not know how to subtract from an integer - |
|
824 |
retry the operation by coercing to higher generality" |
|
1 | 825 |
|
701 | 826 |
^ anInteger retry:#- coercing:self |
1 | 827 |
! |
828 |
||
7440 | 829 |
differenceFromLargeFloat:aLargeFloat |
830 |
"the receiver does not know how to subtract from a largeFloat - |
|
831 |
retry the operation by coercing to higher generality" |
|
832 |
||
833 |
^ aLargeFloat retry:#- coercing:self |
|
834 |
! |
|
835 |
||
7399 | 836 |
differenceFromLongFloat:aLongFloat |
837 |
"the receiver does not know how to subtract from a longFloat - |
|
838 |
retry the operation by coercing to higher generality" |
|
839 |
||
840 |
^ aLongFloat retry:#- coercing:self |
|
841 |
||
842 |
"Created: 17.4.1996 / 12:33:16 / cg" |
|
843 |
! |
|
844 |
||
1200 | 845 |
differenceFromShortFloat:aShortFloat |
846 |
"the receiver does not know how to subtract from a shortFloat - |
|
847 |
retry the operation by coercing to higher generality" |
|
848 |
||
849 |
^ aShortFloat retry:#- coercing:self |
|
850 |
||
851 |
"Created: 17.4.1996 / 12:33:16 / cg" |
|
852 |
! |
|
853 |
||
7355 | 854 |
equalFromComplex:aComplex |
855 |
"the receiver does not know how to compare to a complex number - |
|
856 |
retry the operation by coercing to higher generality" |
|
857 |
||
858 |
^ aComplex retry:#= coercing:self |
|
859 |
! |
|
860 |
||
861 |
equalFromFixedPoint:aFixedPoint |
|
862 |
"the receiver does not know how to compare to a fixed point - |
|
863 |
retry the operation by coercing to higher generality" |
|
864 |
||
865 |
^ aFixedPoint retry:#= coercing:self |
|
866 |
! |
|
867 |
||
868 |
equalFromFloat:aFloat |
|
869 |
"the receiver does not know how to compare to a float - |
|
870 |
retry the operation by coercing to higher generality" |
|
871 |
||
872 |
^ aFloat retry:#= coercing:self |
|
873 |
! |
|
874 |
||
875 |
equalFromFraction:aFraction |
|
876 |
"the receiver does not know how to compare to a fraction - |
|
877 |
retry the operation by coercing to higher generality" |
|
878 |
||
879 |
^ aFraction retry:#= coercing:self |
|
880 |
! |
|
881 |
||
882 |
equalFromInteger:anInteger |
|
883 |
"the receiver does not know how to compare to an integer - |
|
884 |
retry the operation by coercing to higher generality" |
|
885 |
||
886 |
^ anInteger retry:#= coercing:self |
|
887 |
! |
|
888 |
||
7440 | 889 |
equalFromLargeFloat:aLargeFloat |
890 |
"the receiver does not know how to compare to a large float - |
|
891 |
retry the operation by coercing to higher generality" |
|
892 |
||
893 |
^ aLargeFloat retry:#= coercing:self |
|
894 |
! |
|
895 |
||
7355 | 896 |
equalFromLongFloat:aLongFloat |
897 |
"the receiver does not know how to compare to a long float - |
|
898 |
retry the operation by coercing to higher generality" |
|
899 |
||
900 |
^ aLongFloat retry:#= coercing:self |
|
901 |
! |
|
902 |
||
903 |
equalFromShortFloat:aShortFloat |
|
904 |
"the receiver does not know how to compare to a short float - |
|
905 |
retry the operation by coercing to higher generality" |
|
906 |
||
907 |
^ aShortFloat retry:#= coercing:self |
|
908 |
! |
|
909 |
||
13007 | 910 |
integerQuotientFromInteger:anInteger |
911 |
"anInteger does not know how to divide by the receiver - |
|
912 |
retry the operation by coercing to higher generality" |
|
913 |
||
914 |
^ anInteger retry:#// coercing:self |
|
915 |
||
916 |
"Created: / 09-08-2010 / 19:49:26 / cg" |
|
917 |
! |
|
918 |
||
16657 | 919 |
isAlmostEqualToFromFloat:aFloat nEpsilon:nE |
920 |
"the receiver does not know how to compare to a short float - |
|
921 |
retry the operation by coercing to higher generality" |
|
922 |
||
923 |
^ aFloat retry:#isAlmostEqualTo:nEpsilon: coercing:self with:nE |
|
924 |
! |
|
925 |
||
926 |
isAlmostEqualToFromShortFloat:aShortFloat nEpsilon:nE |
|
927 |
"the receiver does not know how to compare to a short float - |
|
928 |
retry the operation by coercing to higher generality" |
|
929 |
||
930 |
^ aShortFloat retry:#isAlmostEqualTo:nEpsilon: coercing:self with:nE |
|
931 |
! |
|
932 |
||
1888 | 933 |
lessFromFixedPoint:aFixedPoint |
934 |
"the receiver does not know how to compare to a fixedPoint number - |
|
18860 | 935 |
Return true if aFixedPoint < self. |
1888 | 936 |
retry the operation by coercing to higher generality" |
937 |
||
938 |
^ aFixedPoint retry:#< coercing:self |
|
939 |
||
940 |
"Created: 5.11.1996 / 14:56:12 / cg" |
|
941 |
! |
|
942 |
||
701 | 943 |
lessFromFloat:aFloat |
944 |
"the receiver does not know how to compare to a float - |
|
18860 | 945 |
Return true if aFloat < self. |
701 | 946 |
retry the operation by coercing to higher generality" |
1 | 947 |
|
701 | 948 |
^ aFloat retry:#< coercing:self |
1 | 949 |
! |
950 |
||
701 | 951 |
lessFromFraction:aFraction |
952 |
"the receiver does not know how to compare to a fraction - |
|
18860 | 953 |
Return true if aFraction < self. |
701 | 954 |
retry the operation by coercing to higher generality" |
1 | 955 |
|
701 | 956 |
^ aFraction retry:#< coercing:self |
1 | 957 |
! |
958 |
||
701 | 959 |
lessFromInteger:anInteger |
960 |
"the receiver does not know how to compare to an integer - |
|
18860 | 961 |
Return true if anInteger < self. |
701 | 962 |
retry the operation by coercing to higher generality" |
1 | 963 |
|
701 | 964 |
^ anInteger retry:#< coercing:self |
1 | 965 |
! |
966 |
||
7440 | 967 |
lessFromLargeFloat:aLargeFloat |
968 |
"the receiver does not know how to compare to a largeFloat - |
|
18860 | 969 |
Return true if aLargeFloat < self. |
7440 | 970 |
retry the operation by coercing to higher generality" |
971 |
||
972 |
^ aLargeFloat retry:#< coercing:self |
|
973 |
! |
|
974 |
||
7399 | 975 |
lessFromLongFloat:aLongFloat |
976 |
"the receiver does not know how to compare to a longFloat - |
|
18860 | 977 |
Return true if aLongFloat < self. |
7399 | 978 |
retry the operation by coercing to higher generality" |
979 |
||
980 |
^ aLongFloat retry:#< coercing:self |
|
981 |
||
982 |
"Modified: 17.4.1996 / 12:33:33 / cg" |
|
983 |
! |
|
984 |
||
1200 | 985 |
lessFromShortFloat:aShortFloat |
986 |
"the receiver does not know how to compare to a shortFloat - |
|
18860 | 987 |
Return true if aShortFloat < self. |
1200 | 988 |
retry the operation by coercing to higher generality" |
989 |
||
990 |
^ aShortFloat retry:#< coercing:self |
|
991 |
||
992 |
"Modified: 17.4.1996 / 12:33:33 / cg" |
|
993 |
! |
|
994 |
||
14002 | 995 |
moduloFromInteger:anInteger |
996 |
"anInteger does not know how to compute the modulo from the receiver - |
|
997 |
retry the operation by coercing to higher generality" |
|
998 |
||
999 |
^ anInteger retry:#\\ coercing:self |
|
1000 |
||
1001 |
"Created: / 12-02-2012 / 20:38:13 / cg" |
|
1002 |
! |
|
1003 |
||
7355 | 1004 |
productFromComplex:aComplex |
1005 |
"the receiver does not know how to multiply a complex - |
|
1006 |
retry the operation by coercing to higher generality" |
|
1007 |
||
1008 |
^ aComplex retry:#* coercing:self |
|
1009 |
! |
|
1010 |
||
1888 | 1011 |
productFromFixedPoint:aFixedPoint |
1012 |
"the receiver does not know how to multiply a fixed point number - |
|
1013 |
retry the operation by coercing to higher generality" |
|
1014 |
||
1015 |
^ aFixedPoint retry:#* coercing:self |
|
1016 |
||
1017 |
"Created: 5.11.1996 / 14:56:28 / cg" |
|
1018 |
! |
|
1019 |
||
701 | 1020 |
productFromFloat:aFloat |
1021 |
"the receiver does not know how to multiply a float - |
|
1022 |
retry the operation by coercing to higher generality" |
|
1 | 1023 |
|
701 | 1024 |
^ aFloat retry:#* coercing:self |
1 | 1025 |
! |
1026 |
||
701 | 1027 |
productFromFraction:aFraction |
1028 |
"the receiver does not know how to multiply a fraction - |
|
1029 |
retry the operation by coercing to higher generality" |
|
1 | 1030 |
|
701 | 1031 |
^ aFraction retry:#* coercing:self |
1 | 1032 |
! |
1033 |
||
701 | 1034 |
productFromInteger:anInteger |
1035 |
"the receiver does not know how to multiply an integer - |
|
1036 |
retry the operation by coercing to higher generality" |
|
1 | 1037 |
|
701 | 1038 |
^ anInteger retry:#* coercing:self |
1 | 1039 |
! |
1040 |
||
7440 | 1041 |
productFromLargeFloat:aLargeFloat |
1042 |
"the receiver does not know how to multiply a largeFloat - |
|
1043 |
retry the operation by coercing to higher generality" |
|
1044 |
||
1045 |
^ aLargeFloat retry:#* coercing:self |
|
1046 |
||
1047 |
"Created: 17.4.1996 / 12:33:48 / cg" |
|
1048 |
! |
|
1049 |
||
7399 | 1050 |
productFromLongFloat:aLongFloat |
1051 |
"the receiver does not know how to multiply a longFloat - |
|
1052 |
retry the operation by coercing to higher generality" |
|
1053 |
||
1054 |
^ aLongFloat retry:#* coercing:self |
|
1055 |
||
1056 |
"Created: 17.4.1996 / 12:33:48 / cg" |
|
1057 |
! |
|
1058 |
||
1200 | 1059 |
productFromShortFloat:aShortFloat |
1060 |
"the receiver does not know how to multiply a shortFloat - |
|
1061 |
retry the operation by coercing to higher generality" |
|
1062 |
||
1063 |
^ aShortFloat retry:#* coercing:self |
|
1064 |
||
1065 |
"Created: 17.4.1996 / 12:33:48 / cg" |
|
1066 |
! |
|
1067 |
||
7355 | 1068 |
quotientFromComplex:aComplex |
7440 | 1069 |
"aComplex does not know how to divide by the receiver - |
7355 | 1070 |
retry the operation by coercing to higher generality" |
1071 |
||
1072 |
^ aComplex retry:#/ coercing:self |
|
1073 |
! |
|
1074 |
||
1888 | 1075 |
quotientFromFixedPoint:aFixedPoint |
7440 | 1076 |
"aFixedPoint does not know how to divide by the receiver - |
1888 | 1077 |
retry the operation by coercing to higher generality" |
1078 |
||
1079 |
^ aFixedPoint retry:#/ coercing:self |
|
1080 |
||
1081 |
"Created: 5.11.1996 / 14:56:41 / cg" |
|
1082 |
! |
|
1083 |
||
701 | 1084 |
quotientFromFloat:aFloat |
7440 | 1085 |
"aFloat does not know how to divide by the receiver - |
701 | 1086 |
retry the operation by coercing to higher generality" |
1 | 1087 |
|
701 | 1088 |
^ aFloat retry:#/ coercing:self |
1 | 1089 |
! |
1090 |
||
701 | 1091 |
quotientFromFraction:aFraction |
7440 | 1092 |
"aFraction does not know how to divide by the receiver - |
701 | 1093 |
retry the operation by coercing to higher generality" |
1 | 1094 |
|
701 | 1095 |
^ aFraction retry:#/ coercing:self |
1 | 1096 |
! |
1097 |
||
701 | 1098 |
quotientFromInteger:anInteger |
7440 | 1099 |
"anInteger does not know how to divide by the receiver - |
1 | 1100 |
retry the operation by coercing to higher generality" |
1101 |
||
701 | 1102 |
^ anInteger retry:#/ coercing:self |
1 | 1103 |
! |
1104 |
||
7440 | 1105 |
quotientFromLargeFloat:aLargeFloat |
1106 |
"aLargeFloat does not know how to divide by the receiver - |
|
1107 |
retry the operation by coercing to higher generality" |
|
1108 |
||
1109 |
^ aLargeFloat retry:#/ coercing:self |
|
1110 |
||
1111 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
1112 |
! |
|
1113 |
||
7399 | 1114 |
quotientFromLongFloat:aLongFloat |
7440 | 1115 |
"aLongFloat does not know how to divide by the receiver - |
7399 | 1116 |
retry the operation by coercing to higher generality" |
1117 |
||
1118 |
^ aLongFloat retry:#/ coercing:self |
|
1119 |
||
1120 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
1121 |
! |
|
1122 |
||
1200 | 1123 |
quotientFromShortFloat:aShortFloat |
7440 | 1124 |
"aShortFloat does not know how to divide by the receiver - |
1200 | 1125 |
retry the operation by coercing to higher generality" |
1126 |
||
1127 |
^ aShortFloat retry:#/ coercing:self |
|
1128 |
||
1129 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
1130 |
! |
|
1131 |
||
14747 | 1132 |
remainderFromFloat:aFloat |
1133 |
"aFloat does not know how to compute the remainder with the receiver - |
|
1134 |
retry the operation by coercing to higher generality" |
|
1135 |
||
1136 |
^ aFloat retry:#rem: coercing:self |
|
1137 |
! |
|
1138 |
||
1139 |
remainderFromShortFloat:aShortFloat |
|
1140 |
"aShortFloat does not know how to compute the remainder with the receiver - |
|
1141 |
retry the operation by coercing to higher generality" |
|
1142 |
||
1143 |
^ aShortFloat retry:#rem: coercing:self |
|
1144 |
! |
|
1145 |
||
7355 | 1146 |
sumFromComplex:aComplex |
1147 |
"the receiver does not know how to add a complex - |
|
1148 |
retry the operation by coercing to higher generality" |
|
1149 |
||
1150 |
^ aComplex retry:#+ coercing:self |
|
1151 |
! |
|
1152 |
||
1888 | 1153 |
sumFromFixedPoint:aFixedPoint |
1154 |
"the receiver does not know how to add a fixed point number - |
|
1155 |
retry the operation by coercing to higher generality" |
|
1156 |
||
1157 |
^ aFixedPoint retry:#+ coercing:self |
|
1158 |
||
1159 |
"Created: 5.11.1996 / 14:56:56 / cg" |
|
1160 |
! |
|
1161 |
||
1 | 1162 |
sumFromFloat:aFloat |
1163 |
"the receiver does not know how to add a float - |
|
1164 |
retry the operation by coercing to higher generality" |
|
1165 |
||
1166 |
^ aFloat retry:#+ coercing:self |
|
1167 |
! |
|
1168 |
||
1169 |
sumFromFraction:aFraction |
|
1170 |
"the receiver does not know how to add a fraction - |
|
1171 |
retry the operation by coercing to higher generality" |
|
1172 |
||
1173 |
^ aFraction retry:#+ coercing:self |
|
1174 |
! |
|
1175 |
||
701 | 1176 |
sumFromInteger:anInteger |
1177 |
"the receiver does not know how to add an integer - |
|
1 | 1178 |
retry the operation by coercing to higher generality" |
1179 |
||
701 | 1180 |
^ anInteger retry:#+ coercing:self |
1200 | 1181 |
! |
1182 |
||
7440 | 1183 |
sumFromLargeFloat:aLargeFloat |
1184 |
"the receiver does not know how to add a largeFloat - |
|
1185 |
retry the operation by coercing to higher generality" |
|
1186 |
||
1187 |
^ aLargeFloat retry:#+ coercing:self |
|
1188 |
||
1189 |
"Created: 17.4.1996 / 12:34:10 / cg" |
|
1190 |
! |
|
1191 |
||
7399 | 1192 |
sumFromLongFloat:aLongFloat |
1193 |
"the receiver does not know how to add a longFloat - |
|
1194 |
retry the operation by coercing to higher generality" |
|
1195 |
||
1196 |
^ aLongFloat retry:#+ coercing:self |
|
1197 |
||
1198 |
"Created: 17.4.1996 / 12:34:10 / cg" |
|
1199 |
! |
|
1200 |
||
1200 | 1201 |
sumFromShortFloat:aShortFloat |
1202 |
"the receiver does not know how to add a shortFloat - |
|
1203 |
retry the operation by coercing to higher generality" |
|
1204 |
||
1205 |
^ aShortFloat retry:#+ coercing:self |
|
1206 |
||
1207 |
"Created: 17.4.1996 / 12:34:10 / cg" |
|
1 | 1208 |
! ! |
1209 |
||
7384 | 1210 |
!ArithmeticValue methodsFor:'mathematical functions'! |
1 | 1211 |
|
10326 | 1212 |
** aNumber |
9443 | 1213 |
"Answer the receiver raised to the power of the argument, aNumber." |
1214 |
"same as Number>>raisedTo:" |
|
1215 |
||
1216 |
^self raisedTo: aNumber |
|
1217 |
||
1218 |
"Created: / 15-07-2006 / 15:11:05 / cg" |
|
1219 |
"Modified: / 17-07-2006 / 12:51:33 / cg" |
|
1220 |
! |
|
1221 |
||
18860 | 1222 |
basicRaisedToInteger:exp |
1223 |
"return the receiver raised to exp" |
|
1224 |
||
1225 |
|result e t| |
|
1226 |
||
1227 |
"use the addition chaining algorithm, |
|
1228 |
which is much faster for big exponents" |
|
6858 | 1229 |
|
18860 | 1230 |
result := 1. |
1231 |
t := self. |
|
1232 |
exp < 0 ifTrue:[ |
|
1233 |
e := exp negated. |
|
1234 |
] ifFalse:[ |
|
1235 |
e := exp. |
|
1236 |
]. |
|
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1237 |
|
18860 | 1238 |
[e ~~ 0] whileTrue:[ |
1239 |
[(e bitAnd:1) == 0] whileTrue:[ |
|
1240 |
e := e bitShift:-1. |
|
1241 |
t := t * t. |
|
1242 |
]. |
|
1243 |
e := e - 1. |
|
1244 |
result := result * t. |
|
1245 |
]. |
|
1246 |
||
1247 |
(exp < 0) ifTrue:[ |
|
1248 |
^ 1 / result |
|
1249 |
]. |
|
1250 |
||
1251 |
^ result |
|
18835 | 1252 |
|
1253 |
" |
|
1254 |
(2.0 raisedToInteger:216) |
|
1255 |
(2 raisedToInteger:216) |
|
1256 |
(2 raisedTo:216) |
|
1257 |
-> 105312291668557186697918027683670432318895095400549111254310977536 |
|
1258 |
||
1259 |
(2 raisedToInteger:216) asFloat |
|
1260 |
(2 raisedTo:216) asFloat |
|
1261 |
-> 1.05312E+65 |
|
1262 |
||
1263 |
(2 raisedToInteger:500) |
|
1264 |
(2 raisedTo:500) |
|
1265 |
-> 3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376 |
|
18860 | 1266 |
(2 raisedTo:-500) |
1267 |
-> (1/3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376) |
|
18835 | 1268 |
2 raisedToInteger:10 |
1269 |
-> 1024 |
|
1270 |
-2 raisedToInteger:10 |
|
1271 |
-> 1024 |
|
1272 |
-2 raisedToInteger:9 |
|
1273 |
-> -512 |
|
1274 |
10 raisedToInteger:-10 |
|
1275 |
-> (1/10000000000) |
|
1276 |
2 raisedToInteger:0 |
|
1277 |
-> 1 |
|
1278 |
2 raisedToInteger:-1 |
|
1279 |
-> (1/2) |
|
1280 |
||
1281 |
Time millisecondsToRun:[ |
|
1282 |
10000 timesRepeat:[ |
|
1283 |
(2 raisedToInteger:500) |
|
1284 |
] |
|
1285 |
] |
|
1286 |
||
1287 |
Time millisecondsToRun:[ |
|
1288 |
|bigNum| |
|
1289 |
bigNum := 2 raisedToInteger:500. |
|
1290 |
10 timesRepeat:[ |
|
1291 |
(bigNum raisedToInteger:500) |
|
1292 |
] |
|
1293 |
] |
|
1294 |
" |
|
1295 |
||
1296 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
|
1297 |
"Modified: / 27.4.1999 / 16:16:11 / stefan" |
|
1298 |
! |
|
1299 |
||
18860 | 1300 |
raisedTo: aNumber |
1301 |
aNumber isInteger ifTrue:[ |
|
1302 |
^ self raisedToInteger:aNumber |
|
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1303 |
]. |
18860 | 1304 |
^ self subclassResponsibility |
1305 |
! |
|
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1306 |
|
18860 | 1307 |
raisedToInteger:exp |
1308 |
"return the receiver raised to exp. |
|
1309 |
Warning: if the receiver is a float/double, currently INF |
|
1310 |
may be returned. This may be changed silently to raise an error |
|
1311 |
in future versions." |
|
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1312 |
|
18860 | 1313 |
^ self basicRaisedToInteger:exp |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1314 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1315 |
" |
18860 | 1316 |
(2.0 raisedToInteger:10000) |
1317 |
(2 raisedToInteger:10000) |
|
1318 |
(2 raisedToInteger:-10000) |
|
1319 |
||
17662 | 1320 |
(2.0 raisedToInteger:216) |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1321 |
(2 raisedToInteger:216) |
10326 | 1322 |
(2 raisedTo:216) |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1323 |
-> 105312291668557186697918027683670432318895095400549111254310977536 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1324 |
|
10326 | 1325 |
(2 raisedToInteger:216) asFloat |
1326 |
(2 raisedTo:216) asFloat |
|
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1327 |
-> 1.05312E+65 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1328 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1329 |
(2 raisedToInteger:500) |
10326 | 1330 |
(2 raisedTo:500) |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1331 |
-> 3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376 |
10326 | 1332 |
2 raisedToInteger:10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1333 |
-> 1024 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1334 |
-2 raisedToInteger:10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1335 |
-> 1024 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1336 |
-2 raisedToInteger:9 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1337 |
-> -512 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1338 |
10 raisedToInteger:-10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1339 |
-> (1/10000000000) |
10326 | 1340 |
2 raisedToInteger:0 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1341 |
-> 1 |
10326 | 1342 |
2 raisedToInteger:-1 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1343 |
-> (1/2) |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1344 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1345 |
Time millisecondsToRun:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1346 |
10000 timesRepeat:[ |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1347 |
(2 raisedToInteger:500) |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1348 |
] |
10326 | 1349 |
] |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1350 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1351 |
Time millisecondsToRun:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1352 |
|bigNum| |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1353 |
bigNum := 2 raisedToInteger:500. |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1354 |
10 timesRepeat:[ |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1355 |
(bigNum raisedToInteger:500) |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1356 |
] |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1357 |
] |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1358 |
" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1359 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1360 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1361 |
"Modified: / 27.4.1999 / 16:16:11 / stefan" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1362 |
! |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1363 |
|
701 | 1364 |
squared |
1365 |
"return receiver * receiver" |
|
1366 |
||
1367 |
^ self * self |
|
1368 |
! ! |
|
1369 |
||
1370 |
!ArithmeticValue methodsFor:'queries'! |
|
1371 |
||
1372 |
respondsToArithmetic |
|
1373 |
"return true, if the receiver responds to arithmetic messages" |
|
1374 |
||
1375 |
^ true |
|
1376 |
! ! |
|
1377 |
||
1378 |
!ArithmeticValue methodsFor:'testing'! |
|
1379 |
||
1380 |
denominator |
|
1381 |
"return the denominator of the receiver" |
|
1382 |
||
1383 |
^ 1 |
|
1384 |
! |
|
1385 |
||
1386 |
even |
|
17223 | 1387 |
"return true if the receiver is divisible by 2. |
1388 |
Only defined for whole-numbers." |
|
701 | 1389 |
|
17223 | 1390 |
^ self class |
1391 |
raise:DomainError |
|
1392 |
receiver:self |
|
1393 |
selector:#even |
|
1394 |
arguments:#() |
|
1395 |
errorString:'Receiver must be a whole-number' |
|
1396 |
||
1397 |
"/ No, this is bad: ^ self truncated asInteger even |
|
1398 |
||
1399 |
" |
|
1400 |
2.4 even |
|
1401 |
2.0 even |
|
1402 |
" |
|
701 | 1403 |
! |
1404 |
||
3635 | 1405 |
isComplex |
7355 | 1406 |
"Answer whether the receiver has an imaginary part |
1407 |
(i.e. if it is a complex number). Always false here." |
|
3635 | 1408 |
|
1409 |
^ false |
|
1410 |
||
1411 |
"Modified: / 9.7.1998 / 10:19:27 / cg" |
|
1412 |
! |
|
1413 |
||
8979
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1414 |
isFinite |
10326 | 1415 |
"return true, if the receiver is finite |
9400 | 1416 |
i.e. it can be represented as a rational number." |
1417 |
||
8979
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1418 |
^ true |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1419 |
! |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1420 |
|
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1421 |
isInfinite |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1422 |
^ false |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1423 |
! |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1424 |
|
7440 | 1425 |
isNegativeInfinity |
1426 |
^ self negative and:[self isInfinite] |
|
1427 |
! |
|
1428 |
||
1429 |
isNegativeZero |
|
1430 |
"return false - must be redefined by subclasses which can represent a negative zero |
|
1431 |
(i.e. limitedPrecisionReal classes)" |
|
1432 |
||
1433 |
^ false |
|
1434 |
! |
|
1435 |
||
1436 |
isPositiveInfinity |
|
1437 |
^ self positive and:[self isInfinite] |
|
1438 |
! |
|
1439 |
||
6156 | 1440 |
isReal |
1441 |
"return true, if the receiver is some kind of real number (as opposed to a complex); |
|
1442 |
false is returned here - the method is only redefined in Number (and Complex)." |
|
1443 |
||
1444 |
^ false |
|
1445 |
||
1446 |
"Modified: / 6.11.2001 / 13:20:20 / cg" |
|
1447 |
"Created: / 6.11.2001 / 13:26:29 / cg" |
|
1448 |
! |
|
1449 |
||
7440 | 1450 |
isZero |
1451 |
"return false - must be redefined by subclasses which can represent a negative zero |
|
1452 |
(i.e. limitedPrecisionReal classes)" |
|
701 | 1453 |
|
7440 | 1454 |
^ self = self class zero |
1455 |
! |
|
1456 |
||
1457 |
negative |
|
18860 | 1458 |
"return true if the receiver is less than zero." |
7440 | 1459 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
1460 |
^ self < self class zero |
701 | 1461 |
! |
1462 |
||
1463 |
numerator |
|
1464 |
"return the numerator of the receiver." |
|
1465 |
||
1466 |
^ self |
|
1467 |
! |
|
1468 |
||
1469 |
odd |
|
1470 |
"return true if the receiver is not divisible by 2" |
|
1471 |
||
1472 |
^ self even not |
|
1473 |
! |
|
1474 |
||
1475 |
positive |
|
18860 | 1476 |
"return true, if the receiver is greater or equal to zero (not negative)" |
701 | 1477 |
|
1478 |
^ self negative not |
|
1479 |
! |
|
1480 |
||
1481 |
sign |
|
7471 | 1482 |
"return the sign of the receiver (-1, 0 or 1)" |
701 | 1483 |
|
7440 | 1484 |
|zero| |
1485 |
||
1486 |
zero := self class zero. |
|
1487 |
(self < zero) ifTrue:[^ -1]. |
|
1488 |
(zero < self) ifTrue:[^ 1]. |
|
701 | 1489 |
^ 0 |
1490 |
! |
|
1491 |
||
1492 |
strictlyPositive |
|
18860 | 1493 |
"return true, if the receiver is greater than zero" |
701 | 1494 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
1495 |
^ self class zero < self |
1 | 1496 |
! ! |
1497 |
||
701 | 1498 |
!ArithmeticValue methodsFor:'truncation & rounding'! |
1 | 1499 |
|
701 | 1500 |
ceiling |
1501 |
"return the integer nearest the receiver towards positive infinity." |
|
1 | 1502 |
|
701 | 1503 |
|anInteger| |
1 | 1504 |
|
701 | 1505 |
anInteger := self // 1. "truncates towards negative infinity" |
1506 |
anInteger = self ifTrue:[^ anInteger]. |
|
1507 |
^ anInteger + 1 |
|
1 | 1508 |
! |
1509 |
||
701 | 1510 |
floor |
1511 |
"return the receiver truncated towards negative infinity" |
|
1 | 1512 |
|
701 | 1513 |
^ self // 1 |
1 | 1514 |
! |
1515 |
||
701 | 1516 |
roundTo:aNumber |
1517 |
"return the receiver rounded to multiples of aNumber" |
|
1 | 1518 |
|
701 | 1519 |
^ (self / aNumber) rounded * aNumber |
8449 | 1520 |
|
1521 |
" |
|
10326 | 1522 |
0 roundTo:4 |
8449 | 1523 |
1 roundTo:4 |
1524 |
2 roundTo:4 |
|
1525 |
3 roundTo:4 |
|
1526 |
4 roundTo:4 |
|
1527 |
5 roundTo:4 |
|
1528 |
6 roundTo:4 |
|
1529 |
7 roundTo:4 |
|
14497 | 1530 |
|
1531 |
7.123 roundTo:0.1 |
|
1532 |
7.523 roundTo:0.1 |
|
1533 |
7.583 roundTo:0.1 |
|
1534 |
7.623 roundTo:0.1 |
|
1535 |
7.623 roundTo:0.01 |
|
1536 |
7.628 roundTo:0.01 |
|
8449 | 1537 |
" |
1538 |
! |
|
1539 |
||
1540 |
roundUpTo:aNumber |
|
1541 |
"return the receiver rounded up to the next multiple of aNumber" |
|
1542 |
||
1543 |
^ (self / aNumber) ceiling * aNumber |
|
1544 |
||
1545 |
" |
|
10326 | 1546 |
0 roundUpTo:4 |
1547 |
1 roundUpTo:4 |
|
1548 |
2 roundUpTo:4 |
|
1549 |
3 roundUpTo:4 |
|
1550 |
4 roundUpTo:4 |
|
1551 |
5 roundUpTo:4 |
|
1552 |
6 roundUpTo:4 |
|
1553 |
7 roundUpTo:4 |
|
1554 |
8 roundUpTo:4 |
|
10911 | 1555 |
(3@4) roundUpTo:8 |
1556 |
(3@4) roundUpTo:(5 @ 4) |
|
1557 |
(3@3) roundUpTo:(5 @ 4) |
|
8449 | 1558 |
" |
701 | 1559 |
! |
1560 |
||
1561 |
rounded |
|
1562 |
"return the integer nearest the receiver" |
|
1563 |
||
1881 | 1564 |
self negative ifTrue:[ |
10326 | 1565 |
^ (self - 0.5) ceiling |
1881 | 1566 |
]. |
701 | 1567 |
^ (self + 0.5) floor |
1881 | 1568 |
|
1569 |
"Modified: 5.11.1996 / 11:31:59 / cg" |
|
1 | 1570 |
! |
1571 |
||
701 | 1572 |
truncateTo:aNumber |
1573 |
"return the receiver truncated to multiples of aNumber" |
|
1 | 1574 |
|
19056 | 1575 |
|t| |
1576 |
||
1577 |
aNumber <= 0 ifTrue:[self error:'argument must be positive']. |
|
1578 |
||
1579 |
t := (self / aNumber) floor * aNumber. |
|
1580 |
(aNumber isInteger) ifTrue:[ |
|
1581 |
^ t asInteger |
|
1582 |
]. |
|
1583 |
^ t |
|
1584 |
||
1585 |
" |
|
1586 |
truncate to multiples of 4 |
|
1587 |
123.456 truncateTo:4 |
|
1588 |
124.456 truncateTo:4 |
|
1589 |
truncate to multiples of 2 |
|
1590 |
122.456 truncateTo:2 |
|
1591 |
123.456 truncateTo:2 |
|
1592 |
124.456 truncateTo:2 |
|
1593 |
normal truncate |
|
1594 |
123.456 truncateTo:1 |
|
1595 |
124.456 truncateTo:1 |
|
1596 |
truncate to decimal digits |
|
1597 |
123.456 truncateTo:0.1 |
|
1598 |
123.987 truncateTo:0.1 |
|
1599 |
123.456 truncateTo:0.01 |
|
1600 |
" |
|
1 | 1601 |
! |
1602 |
||
701 | 1603 |
truncated |
1604 |
"return the receiver truncated towards zero" |
|
1 | 1605 |
|
701 | 1606 |
self negative ifTrue:[ |
1607 |
^ self ceiling |
|
1608 |
]. |
|
1609 |
^ self floor |
|
1610 |
! ! |
|
1 | 1611 |
|
701 | 1612 |
!ArithmeticValue class methodsFor:'documentation'! |
1613 |
||
1614 |
version |
|
18835 | 1615 |
^ '$Header$' |
12576 | 1616 |
! |
1617 |
||
1618 |
version_CVS |
|
18835 | 1619 |
^ '$Header$' |
1 | 1620 |
! ! |
6858 | 1621 |
|
14747 | 1622 |
|
701 | 1623 |
ArithmeticValue initialize! |