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