author | Claus Gittinger <cg@exept.de> |
Mon, 25 Nov 2019 15:24:07 +0100 | |
changeset 24980 | 2f028fe7bd3d |
parent 24963 | 3f40782dfac4 |
child 24993 | f64786c96b57 |
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:] |
21967 | 50 |
ArithmeticSignal <Error> parent of all arithmetic signals |
51 |
(never raised itself) |
|
52 |
New: now a reference to ArithmeticError |
|
82 | 53 |
|
21967 | 54 |
DomainErrorSignal <Error> raised upon float errors |
55 |
(for example range in trigonometric) |
|
56 |
New: now a reference to DomainError; no longer a classVar |
|
1283 | 57 |
|
21967 | 58 |
DivisionByZeroSignal <Error> raised when division by 0 is attempted |
59 |
New: now a reference to ZeroDivide |
|
155 | 60 |
|
21967 | 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:] |
21967 | 69 |
Claus Gittinger |
1295 | 70 |
|
1283 | 71 |
[See also:] |
21967 | 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 |
||
23645 | 84 |
coercionErrorSignal |
85 |
^ ArithmeticError |
|
86 |
||
87 |
"Created: / 26-01-2019 / 10:53:12 / Claus Gittinger" |
|
88 |
! |
|
89 |
||
701 | 90 |
divisionByZeroSignal |
21865 | 91 |
"return the signal which is raised on division by zero. |
92 |
No longer used - we now have the class based ZeroDivide exception. |
|
93 |
This method is kept for backward compatibility." |
|
701 | 94 |
|
7403 | 95 |
^ ZeroDivide |
21865 | 96 |
|
97 |
"Modified (comment): / 20-06-2017 / 17:53:33 / cg" |
|
701 | 98 |
! |
1 | 99 |
|
100 |
domainErrorSignal |
|
22289 | 101 |
"return the signal which is raised on some math errors, |
102 |
when the argument is outside the legal domain. |
|
103 |
(such as arcSin of 2 etc.)" |
|
1 | 104 |
|
7403 | 105 |
^ DomainError |
22289 | 106 |
|
107 |
" |
|
108 |
2 arcSin |
|
109 |
0 log - raises infiniteResult, which is a child of DomainError |
|
110 |
" |
|
111 |
||
112 |
"Modified (comment): / 22-09-2017 / 09:50:41 / cg" |
|
7403 | 113 |
! |
114 |
||
115 |
imaginaryResultSignal |
|
116 |
"return the signal which is raised when an imaginary result would be created |
|
22289 | 117 |
(such as when taking the sqrt of a negative number). |
118 |
This error can be handled by wrapping the computation inside a trapImaginary |
|
119 |
block; then, a complex result is generated." |
|
7403 | 120 |
|
121 |
^ ImaginaryResultError |
|
22289 | 122 |
|
123 |
" |
|
124 |
-2 sqrt |
|
125 |
||
126 |
Complex trapImaginary:[ -2 sqrt ] |
|
127 |
" |
|
128 |
||
129 |
"Modified (comment): / 22-09-2017 / 09:53:58 / cg" |
|
1 | 130 |
! |
131 |
||
21967 | 132 |
infiniteResultSignal |
22289 | 133 |
"return the signal which is raised when an infinite result would be created. |
134 |
This is a subclass of DomainError. |
|
21967 | 135 |
(such as when taking the logarithm of zero)" |
136 |
||
137 |
^ InfiniteResultError |
|
138 |
||
139 |
"Created: / 03-07-2017 / 15:46:02 / cg" |
|
22289 | 140 |
"Modified (comment): / 22-09-2017 / 09:49:15 / cg" |
21967 | 141 |
! |
142 |
||
7440 | 143 |
operationNotPossibleSignal |
23645 | 144 |
^ ArithmeticError |
145 |
||
146 |
"Modified: / 26-01-2019 / 10:54:35 / Claus Gittinger" |
|
7440 | 147 |
! |
148 |
||
1 | 149 |
overflowSignal |
22289 | 150 |
"return the signal which is raised on overflow conditions (in floats). |
151 |
Attention: currently not raised on all architectures; some return NaN" |
|
1 | 152 |
|
7403 | 153 |
^ OverflowError |
22289 | 154 |
|
155 |
" |
|
156 |
10e300 * 10e30 |
|
157 |
||
158 |
10q300 * 10e30 |
|
159 |
" |
|
160 |
||
161 |
"Modified (comment): / 22-09-2017 / 09:58:49 / cg" |
|
1 | 162 |
! |
163 |
||
5955 | 164 |
rangeErrorSignal |
165 |
"return the parent of the overflow/underflow signals" |
|
166 |
||
167 |
^ RangeError |
|
168 |
! |
|
169 |
||
7440 | 170 |
undefinedResultSignal |
23645 | 171 |
^ ArithmeticError |
172 |
||
173 |
"Modified: / 26-01-2019 / 10:54:28 / Claus Gittinger" |
|
7440 | 174 |
! |
175 |
||
1 | 176 |
underflowSignal |
22289 | 177 |
"return the signal which is raised on underflow conditions (in floats) |
178 |
Attention: currently not raised on all architectures; some return zero" |
|
1 | 179 |
|
7403 | 180 |
^ UnderflowError |
22289 | 181 |
|
182 |
" |
|
183 |
10e-300 / 10e30 |
|
184 |
" |
|
185 |
||
186 |
"Modified (comment): / 22-09-2017 / 09:58:57 / cg" |
|
1 | 187 |
! |
188 |
||
345 | 189 |
unorderedSignal |
10326 | 190 |
"return the signal which is raised when numbers are compared, |
345 | 191 |
for which no ordering is defined (for example: complex numbers)" |
192 |
||
7403 | 193 |
^ UnorderedNumbersError |
22289 | 194 |
|
195 |
" |
|
196 |
(0 + 3i) > (0 + 4i) |
|
197 |
3i > 4i |
|
198 |
" |
|
199 |
||
200 |
"Modified (comment): / 22-09-2017 / 09:57:28 / cg" |
|
1 | 201 |
! ! |
202 |
||
7440 | 203 |
!ArithmeticValue class methodsFor:'class initialization'! |
204 |
||
205 |
initialize |
|
206 |
"setup the signals" |
|
207 |
||
20094 | 208 |
"/ Notice that we now use class based exceptions; |
7440 | 209 |
"/ however, for backward compatibility, the class variables |
210 |
"/ referring to those are kept for a while, for subclass compatibility. |
|
211 |
"/ Please convert your code to access those via the Signal-constants class |
|
20090 | 212 |
"/ messages (accessors) instead of referring to the class variables. |
7440 | 213 |
|
214 |
ArithmeticSignal := ArithmeticError. |
|
21967 | 215 |
"/ DomainErrorSignal := DomainError. |
7440 | 216 |
DivisionByZeroSignal := ZeroDivide. |
217 |
UnorderedSignal := UnorderedNumbersError. |
|
218 |
ConversionErrorSignal := ConversionError. |
|
219 |
OverflowSignal := OverflowError. |
|
220 |
UnderflowSignal := UnderflowError. |
|
21967 | 221 |
|
222 |
"Modified: / 03-07-2017 / 14:05:10 / cg" |
|
7440 | 223 |
! ! |
224 |
||
18835 | 225 |
!ArithmeticValue class methodsFor:'coercing & converting'! |
226 |
||
227 |
coerce:aNumber |
|
228 |
"convert the argument aNumber into an instance of the receiver (class) and return it." |
|
229 |
||
230 |
^ self subclassResponsibility |
|
231 |
! ! |
|
232 |
||
7440 | 233 |
!ArithmeticValue class methodsFor:'constants'! |
234 |
||
235 |
NaN |
|
8850 | 236 |
"return the constant NaN (not a Number)." |
237 |
||
7440 | 238 |
^ NotANumber new |
239 |
! |
|
240 |
||
241 |
infinity |
|
24301 | 242 |
"return something which represents positive infinity. |
243 |
Warning: do not compare equal against infinities; |
|
244 |
instead, check using isFinite or isInfinite" |
|
8850 | 245 |
|
7440 | 246 |
^ Infinity positive |
21862 | 247 |
|
248 |
"Modified (comment): / 20-06-2017 / 13:45:23 / cg" |
|
24301 | 249 |
"Modified (comment): / 09-06-2019 / 12:56:30 / Claus Gittinger" |
7440 | 250 |
! |
251 |
||
7538
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
252 |
nan |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
253 |
"VW compatibility" |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
254 |
|
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
255 |
^ self NaN |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
256 |
! |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
257 |
|
7440 | 258 |
negativeInfinity |
24301 | 259 |
"return something which represents negative infinity. |
260 |
Warning: do not compare equal against infinities; |
|
261 |
instead, check using isFinite or isInfinite" |
|
8850 | 262 |
|
7440 | 263 |
^ Infinity negative |
24301 | 264 |
|
265 |
"Modified (comment): / 09-06-2019 / 12:56:36 / Claus Gittinger" |
|
266 |
! |
|
267 |
||
268 |
positiveInfinity |
|
269 |
"return something which represents positive infinity. |
|
270 |
Warning: do not compare equal against infinities; |
|
271 |
instead, check using isFinite or isInfinite" |
|
272 |
||
273 |
^ self infinity |
|
274 |
||
275 |
"Created: / 09-06-2019 / 12:49:31 / Claus Gittinger" |
|
7440 | 276 |
! |
277 |
||
7726 | 278 |
unity |
11467 | 279 |
"return something which represents the unity element (for my instances). |
280 |
That is the neutral element for multiplication." |
|
8850 | 281 |
|
21827 | 282 |
^ self subclassResponsibility |
283 |
||
284 |
"Modified: / 16-06-2017 / 11:04:36 / cg" |
|
7726 | 285 |
! |
286 |
||
7440 | 287 |
zero |
11467 | 288 |
"return something which represents the zero element (for my instances). |
289 |
That is the neutral element for addition." |
|
8850 | 290 |
|
21827 | 291 |
^ self subclassResponsibility |
292 |
||
293 |
"Modified: / 16-06-2017 / 11:04:33 / cg" |
|
7440 | 294 |
! ! |
295 |
||
17223 | 296 |
!ArithmeticValue class methodsFor:'error reporting'! |
297 |
||
298 |
raise:aSignalSymbolOrErrorClass receiver:someNumber selector:sel arguments:argArray errorString:text |
|
299 |
"ST-80 compatible signal raising. Provided for PD numeric classes. |
|
300 |
aSignalSymbolOrErrorClass is either an Error-subclass, or |
|
301 |
the selector which is sent to myself, to retrieve the Exception class / Signal." |
|
302 |
||
303 |
<context: #return> |
|
304 |
||
305 |
|msg signalOrException| |
|
306 |
||
307 |
msg := MessageSend |
|
308 |
receiver:someNumber |
|
309 |
selector:sel |
|
310 |
arguments:argArray. |
|
311 |
||
312 |
aSignalSymbolOrErrorClass isSymbol ifTrue:[ |
|
313 |
signalOrException := self perform:aSignalSymbolOrErrorClass. |
|
314 |
] ifFalse:[ |
|
315 |
signalOrException := aSignalSymbolOrErrorClass. "/ assume its an Error-Subclass |
|
316 |
]. |
|
317 |
||
318 |
^ signalOrException |
|
319 |
raiseRequestWith:msg |
|
320 |
errorString:text |
|
321 |
in:thisContext sender |
|
322 |
||
323 |
" |
|
324 |
Number |
|
325 |
raise:#domainErrorSignal |
|
326 |
receiver:1.0 |
|
327 |
selector:#foo |
|
328 |
errorString:'foo bar test' |
|
329 |
" |
|
330 |
||
331 |
"Modified: / 16.11.2001 / 14:12:09 / cg" |
|
332 |
! ! |
|
333 |
||
21967 | 334 |
!ArithmeticValue class methodsFor:'exception handling'! |
335 |
||
336 |
trapImaginary:aBlock |
|
337 |
"evaluate aBlock; |
|
22289 | 338 |
if any ImaginaryResult occurs inside, which would return an imaginary result, |
21967 | 339 |
(eg. square root of negative number), |
340 |
convert the result to a complex number and proceed. |
|
341 |
||
342 |
This allows for regular (failing) code to transparently convert to complex." |
|
343 |
||
344 |
^ ImaginaryResultError |
|
345 |
handle: [:ex | |
|
346 |
|msgSend selector rcvr| |
|
347 |
||
348 |
msgSend := ex parameter. |
|
349 |
selector := msgSend selector. |
|
350 |
rcvr := msgSend receiver. |
|
22289 | 351 |
|
21967 | 352 |
(selector == #sqrt or: [selector == #sqrtTruncated]) ifTrue: [ |
353 |
(rcvr isNumber and:[rcvr isReal]) ifTrue:[ |
|
354 |
ex proceedWith:(rcvr abs perform:selector) i |
|
355 |
] ifFalse:[ |
|
356 |
msgSend receiver: rcvr asComplex. |
|
357 |
ex proceedWith: msgSend value |
|
358 |
]. |
|
359 |
] ifFalse: [ |
|
360 |
(selector == #integerSqrt) ifTrue: [ |
|
361 |
(rcvr isInteger) ifTrue:[ |
|
362 |
ex proceedWith:(rcvr abs integerSqrt) i |
|
363 |
] ifFalse:[ |
|
364 |
ex proceedWith:(rcvr abs asComplex sqrt floor) i |
|
365 |
]. |
|
22289 | 366 |
] ifFalse: [ |
367 |
(selector == #nthRoot:) ifTrue: [ |
|
368 |
ex proceedWith:(rcvr abs nthRoot:msgSend argument) i |
|
369 |
] |
|
21967 | 370 |
] |
371 |
]. |
|
22289 | 372 |
|
21967 | 373 |
ex reject |
374 |
] do: |
|
375 |
aBlock |
|
376 |
||
377 |
" |
|
378 |
this raises an error: |
|
379 |
-2 sqrt |
|
380 |
||
381 |
this returns an imaginary result: |
|
382 |
Complex trapImaginary: [-2 sqrt] |
|
383 |
||
384 |
of course, this one as well: |
|
385 |
-2 asComplex sqrt |
|
386 |
" |
|
387 |
||
388 |
"failing code: |
|
389 |
|a| |
|
390 |
||
391 |
a := -2. |
|
392 |
(a sqrt + 5) * 17. |
|
393 |
" |
|
394 |
"complex code: |
|
395 |
|a| |
|
396 |
||
397 |
Complex trapImaginary:[ |
|
398 |
a := -2. |
|
399 |
(a sqrt + 5) * 2. |
|
400 |
] |
|
401 |
" |
|
402 |
||
22289 | 403 |
"Modified: / 22-09-2017 / 10:18:14 / cg" |
21967 | 404 |
! |
405 |
||
406 |
trapInfinity:aBlock |
|
407 |
"evaluate aBlock; |
|
408 |
if any DomainError occurs inside, which would return an infinite result, |
|
409 |
(eg. ln of zero), |
|
410 |
convert the result to infinity and proceed. |
|
411 |
||
412 |
This allows for regular (failing) code to transparently convert to infinity and behave |
|
413 |
similar to other systems which do that." |
|
414 |
||
415 |
^ InfiniteResultError |
|
416 |
handle: [:ex | |
|
417 |
|msgSend selector rcvr| |
|
418 |
||
419 |
msgSend := ex parameter. |
|
420 |
selector := msgSend selector. |
|
421 |
rcvr := msgSend receiver. |
|
422 |
((selector == #ln) |
|
423 |
or:[selector == #log10 |
|
424 |
or:[selector == #integerLog10]]) ifTrue: [ |
|
425 |
(rcvr isNumber) ifTrue:[ |
|
426 |
ex proceedWith:(rcvr class negativeInfinity ) |
|
427 |
]. |
|
428 |
]. |
|
429 |
ex reject |
|
430 |
] do: |
|
431 |
aBlock |
|
432 |
||
433 |
" |
|
434 |
this raises an error: |
|
435 |
0 ln |
|
436 |
||
437 |
this returns an imaginary result: |
|
438 |
Number trapInfinity: [0 ln] |
|
439 |
" |
|
440 |
||
441 |
"failing code: |
|
442 |
|a| |
|
443 |
||
444 |
a := 0. |
|
445 |
a ln isFinite. |
|
446 |
" |
|
447 |
"fixed code: |
|
448 |
|a| |
|
449 |
||
450 |
Number trapInfinity:[ |
|
451 |
a := 0. |
|
452 |
a ln isFinite. |
|
453 |
] |
|
454 |
" |
|
455 |
||
456 |
"Created: / 03-07-2017 / 14:25:57 / cg" |
|
457 |
"Modified: / 03-07-2017 / 15:53:40 / cg" |
|
21991 | 458 |
! |
459 |
||
460 |
trapOverflow:aBlock |
|
461 |
"evaluate aBlock; |
|
462 |
if an Overflow occurs inside, return INF and proceed." |
|
463 |
||
464 |
^ OverflowError |
|
465 |
handle: [:ex | |
|
466 |
|msgSend rcvr| |
|
467 |
||
468 |
msgSend := ex parameter. |
|
469 |
rcvr := msgSend receiver. |
|
24301 | 470 |
ex proceedWith:(rcvr class positiveInfinity) |
21991 | 471 |
] do: |
472 |
aBlock |
|
473 |
||
474 |
" |
|
475 |
this raises an error: |
|
476 |
1000 factorial asShortFloat |
|
477 |
||
478 |
this returns an imaginary result: |
|
479 |
Number trapOverflow: [ 1000 factorial asShortFloat] |
|
480 |
" |
|
481 |
||
482 |
"Created: / 07-07-2017 / 13:08:06 / cg" |
|
24301 | 483 |
"Modified: / 09-06-2019 / 12:53:55 / Claus Gittinger" |
21991 | 484 |
! |
485 |
||
486 |
trapUnderflow:aBlock |
|
487 |
"evaluate aBlock; |
|
488 |
if an Underflow occurs inside, return zero and proceed." |
|
489 |
||
490 |
^ UnderflowError |
|
491 |
handle: [:ex | |
|
492 |
|msgSend rcvr| |
|
493 |
||
494 |
msgSend := ex parameter. |
|
495 |
rcvr := msgSend receiver. |
|
496 |
ex proceedWith:(rcvr class zero) |
|
497 |
] do: |
|
498 |
aBlock |
|
499 |
||
500 |
" |
|
501 |
this raises an error: |
|
502 |
1.0 / (1000 factorial) |
|
503 |
||
504 |
this returns an imaginary result: |
|
505 |
Number trapUnderflow: 1.0 / (1000 factorial)] |
|
506 |
" |
|
507 |
||
508 |
"Created: / 05-07-2017 / 13:16:43 / cg" |
|
21967 | 509 |
! ! |
510 |
||
8892 | 511 |
!ArithmeticValue class methodsFor:'queries'! |
512 |
||
513 |
isAbstract |
|
11227 | 514 |
"Return if this class is an abstract class. |
515 |
True is returned for ArithmeticValue here; false for subclasses. |
|
19454 | 516 |
Abstract subclasses must redefine this again." |
11227 | 517 |
|
8892 | 518 |
^ self == ArithmeticValue |
519 |
! ! |
|
520 |
||
24980 | 521 |
|
701 | 522 |
!ArithmeticValue methodsFor:'arithmetic'! |
523 |
||
524 |
* something |
|
11735 | 525 |
"return the product of the receiver and the argument." |
701 | 526 |
|
527 |
^ self subclassResponsibility |
|
528 |
! |
|
529 |
||
530 |
+ something |
|
531 |
"return the sum of the receiver and the argument" |
|
532 |
||
533 |
^ self subclassResponsibility |
|
534 |
! |
|
535 |
||
536 |
- something |
|
537 |
"return the difference of the receiver and the argument" |
|
538 |
||
539 |
^ self subclassResponsibility |
|
540 |
! |
|
541 |
||
542 |
/ something |
|
543 |
"return the quotient of the receiver and the argument" |
|
1 | 544 |
|
701 | 545 |
^ self subclassResponsibility |
546 |
! |
|
547 |
||
22358 | 548 |
// aNumber |
1881 | 549 |
"return the integer quotient of dividing the receiver by aNumber with |
20337 | 550 |
truncation towards negative infinity. |
22358 | 551 |
|
20338 | 552 |
Please be aware of the effect of truncation on negative receivers, |
20337 | 553 |
and understand the difference between '//' vs. 'quo:' |
554 |
and the corresponding '\\' vs. 'rem:'" |
|
701 | 555 |
|
22358 | 556 |
"/ Note: '^ (self / aNumber) floor' may lead to infinite recursion |
1172 | 557 |
|
558 |
^ self subclassResponsibility |
|
559 |
||
22358 | 560 |
"Modified: / 05-11-1996 / 11:46:27 / cg" |
561 |
"Modified: / 12-02-1998 / 16:31:36 / stefan" |
|
562 |
"Modified (comment): / 16-11-2017 / 11:26:13 / mawalch" |
|
701 | 563 |
! |
564 |
||
565 |
\\ something |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
566 |
"return the receiver modulo something. |
17038 | 567 |
The remainder has the same sign as the argument, something. |
6067 | 568 |
The following is always true: |
17038 | 569 |
(receiver // something) * something + (receiver \\ something) = receiver |
20338 | 570 |
|
571 |
Please be aware of the effect of truncation on negative receivers, |
|
572 |
and understand the difference between '//' vs. 'quo:' |
|
573 |
and the corresponding '\\' vs. 'rem:'." |
|
1 | 574 |
|
701 | 575 |
^ self - ((self // something) * something) |
3134 | 576 |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
577 |
" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
578 |
0.9 \\ 0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
579 |
0.9 \\ -0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
580 |
-0.9 \\ 0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
581 |
-0.9 \\ -0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
582 |
" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
583 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
584 |
"Modified: / 12.2.1998 / 19:14:37 / stefan" |
701 | 585 |
! |
586 |
||
587 |
abs |
|
588 |
"return the absolute value of the receiver" |
|
589 |
||
590 |
(self negative) ifTrue:[^ self negated]. |
|
591 |
^ self |
|
592 |
! |
|
593 |
||
22723 | 594 |
copySignTo:aNumber |
595 |
"Return a number with same magnitude as aNumber and same sign as self" |
|
596 |
||
597 |
(self negative == aNumber negative) ifTrue:[^ aNumber]. |
|
598 |
^ aNumber negated |
|
599 |
||
600 |
" |
|
601 |
-15 copySignTo:1234 -> -1234 |
|
602 |
-15 copySignTo:-1234 -> -1234 |
|
603 |
15 copySignTo:1234 -> 1234 |
|
604 |
15 copySignTo:-1234 -> 1234 |
|
605 |
1.0 copySignTo:0.0 -> 0.0 |
|
606 |
-1.0 copySignTo:0.0 -> -0.0 |
|
607 |
1 copySignTo:(1/3) -> (1/3) |
|
608 |
-1 copySignTo:(1/3) -> (-1/3) |
|
609 |
" |
|
610 |
! |
|
611 |
||
10326 | 612 |
dist:arg |
20696 | 613 |
"return the distance between the arg and the receiver." |
8757 | 614 |
|
615 |
^ (arg - self) abs |
|
616 |
||
617 |
" |
|
10326 | 618 |
(1%1) dist:(0%0) |
619 |
(1@1) dist:(0@0) |
|
620 |
(1) dist:(0) |
|
8757 | 621 |
" |
622 |
! |
|
623 |
||
16121 | 624 |
modulusOf:aNumber |
20353 | 625 |
"return aNumber modulo the receiver. |
16121 | 626 |
The remainder has the same sign as something. |
627 |
Defined for protocol compatibility with ModuloNumber." |
|
628 |
||
629 |
^ aNumber \\ self |
|
630 |
! |
|
631 |
||
701 | 632 |
negated |
633 |
"return the receiver negated" |
|
634 |
||
635 |
^ self class zero - self |
|
1 | 636 |
! |
637 |
||
701 | 638 |
quo:something |
639 |
"Return the integer quotient of dividing the receiver by the argument |
|
6067 | 640 |
with truncation towards zero. |
20338 | 641 |
|
642 |
Please be aware of the effect of truncation on negative receivers, |
|
643 |
and understand the difference between '//' vs. 'quo:' |
|
644 |
and the corresponding '\\' vs. 'rem:'. |
|
645 |
||
6067 | 646 |
The following is always true: |
20337 | 647 |
(receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver |
648 |
For positive results, this is the same as #//, |
|
649 |
for negative results, the remainder is ignored. |
|
650 |
I.e.: '9 // 4 = 2' and '-9 // 4 = -3' |
|
651 |
in contrast: '9 quo: 4 = 2' and '-9 quo: 4 = -2'" |
|
701 | 652 |
|
653 |
^ (self / something) truncated |
|
654 |
! |
|
655 |
||
656 |
reciprocal |
|
18835 | 657 |
"return the receiver's reciprocal" |
701 | 658 |
|
659 |
^ self class unity / self |
|
22731 | 660 |
|
661 |
" |
|
662 |
(10 + 4i) class unity -> (1+0i) |
|
663 |
(10 + 4i) reciprocal -> ((5/58)-(1/29)i) |
|
664 |
(4/3) reciprocal -> (3/4) |
|
665 |
3 reciprocal -> (1/3) |
|
666 |
3.0 reciprocal -> 0.333333333333333 |
|
667 |
3.0 asLongFloat reciprocal -> 0.3333333333333333333 |
|
668 |
3.0 asShortFloat reciprocal -> 0.3333333 |
|
669 |
" |
|
701 | 670 |
! |
1 | 671 |
|
701 | 672 |
rem:something |
673 |
"Return the integer remainder of dividing the receiver by the argument |
|
674 |
with truncation towards zero. |
|
6067 | 675 |
The remainder has the same sign as the receiver. |
676 |
The following is always true: |
|
20338 | 677 |
(receiver quo: something) * something + (receiver rem: something) = receiver |
678 |
||
679 |
Please be aware of the effect of truncation on negative receivers, |
|
680 |
and understand the difference between '//' vs. 'quo:' |
|
681 |
and the corresponding '\\' vs. 'rem:'." |
|
701 | 682 |
|
683 |
^ self - ((self quo:something) * something) |
|
7440 | 684 |
! |
685 |
||
686 |
uncheckedDivide:aNumber |
|
687 |
"return the quotient of the receiver and the argument, aNumber. |
|
7471 | 688 |
Do not check for divide by zero (return NaN or Infinity). |
7440 | 689 |
This operation is provided for emulators of other languages/semantics, |
690 |
where no exception is raised for these results (i.e. Java). |
|
18835 | 691 |
It is only defined if the argument's type is the same as the receiver's." |
7440 | 692 |
|
693 |
aNumber isZero ifTrue:[ |
|
21831 | 694 |
"/ protect against NaN and negativeInfinity trying to use me... |
23158 | 695 |
thisContext isRecursive ifTrue:[ |
696 |
self error:'recursive #uncheckedDivide:'. |
|
697 |
]. |
|
21831 | 698 |
|
18835 | 699 |
self isZero ifTrue:[^ self class NaN]. |
700 |
self negative ifTrue:[^ self class negativeInfinity]. |
|
24301 | 701 |
^ self class positiveInfinity. |
7440 | 702 |
]. |
703 |
^ self / aNumber |
|
21831 | 704 |
|
705 |
"Modified (format): / 18-06-2017 / 23:43:13 / cg" |
|
24301 | 706 |
"Modified: / 09-06-2019 / 12:53:47 / Claus Gittinger" |
701 | 707 |
! ! |
708 |
||
12578 | 709 |
!ArithmeticValue methodsFor:'arithmetic destructive'! |
710 |
||
711 |
*= aNumber |
|
712 |
"Return the product of self multiplied by aNumber. |
|
713 |
The receiver MAY, but NEED NOT be changed to contain the product. |
|
714 |
So this method must be used as: 'a := a *= 5'. |
|
715 |
This method can be redefined for constructed datatypes to do optimisations" |
|
716 |
||
717 |
^ self * aNumber |
|
718 |
||
719 |
"Created: / 28.4.1999 / 11:46:11 / stefan" |
|
720 |
"Modified: / 28.4.1999 / 11:53:28 / stefan" |
|
721 |
! |
|
722 |
||
723 |
+= aNumber |
|
724 |
"Return the sum of self and aNumber. |
|
725 |
The receiver MAY, but NEED NOT be changed to contain the sum. |
|
726 |
So this method must be used as: 'a := a += 5'. |
|
727 |
This method can be redefined for constructed datatypes to do optimisations" |
|
728 |
||
729 |
^ self + aNumber |
|
730 |
||
731 |
"Created: / 28.4.1999 / 10:13:41 / stefan" |
|
732 |
"Modified: / 28.4.1999 / 11:54:11 / stefan" |
|
733 |
! |
|
734 |
||
735 |
-= aNumber |
|
736 |
"Return the difference of self and aNumber. |
|
737 |
The receiver MAY, but NEED NOT be changed to contain the difference. |
|
738 |
So this method must be used as: 'a := a -= 5'. |
|
739 |
This method can be redefined for constructed datatypes to do optimisations" |
|
740 |
||
741 |
^ self - aNumber |
|
742 |
||
743 |
"Created: / 28.4.1999 / 10:13:58 / stefan" |
|
744 |
"Modified: / 28.4.1999 / 11:54:37 / stefan" |
|
745 |
! |
|
746 |
||
747 |
/= aNumber |
|
748 |
"Return the quotient of self and aNumber. |
|
749 |
The receiver MAY, but NEED NOT be changed to contain the quotient. |
|
750 |
So this method must be used as: 'a := a /= 5'. |
|
751 |
This method can be redefined for constructed datatypes to do optimisations" |
|
752 |
||
753 |
^ self / aNumber |
|
754 |
||
755 |
"Created: / 28.4.1999 / 11:46:22 / stefan" |
|
756 |
"Modified: / 28.4.1999 / 11:55:06 / stefan" |
|
757 |
! |
|
758 |
||
759 |
div2 |
|
760 |
"Return the quotient of self divided by 2. |
|
761 |
The receiver MAY, but NEED NOT be changed to contain the result. |
|
762 |
So this method must be used as: 'a := a div2. |
|
763 |
This method can be redefined for constructed datatypes to do optimisations" |
|
764 |
||
765 |
^ self // 2 |
|
766 |
||
767 |
"Created: / 28.4.1999 / 10:12:44 / stefan" |
|
768 |
"Modified: / 28.4.1999 / 11:56:09 / stefan" |
|
769 |
! |
|
770 |
||
771 |
mul2 |
|
772 |
"Return the product of self multiplied by 2. |
|
773 |
The receiver MAY, but NEED NOT be changed to contain the result. |
|
15279 | 774 |
So this method must be used as: a := a mul2. |
12578 | 775 |
This method can be redefined for constructed datatypes to do optimisations" |
776 |
||
777 |
^ self * 2 |
|
778 |
||
779 |
"Created: / 28.4.1999 / 10:12:55 / stefan" |
|
780 |
"Modified: / 28.4.1999 / 11:56:38 / stefan" |
|
781 |
! ! |
|
782 |
||
5238 | 783 |
!ArithmeticValue methodsFor:'coercing & converting'! |
1888 | 784 |
|
785 |
coerce:aNumber |
|
18835 | 786 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
1888 | 787 |
|
21816 | 788 |
^ self class coerce:aNumber |
789 |
||
790 |
"Modified: / 15-06-2017 / 10:27:03 / cg" |
|
1888 | 791 |
! |
792 |
||
793 |
generality |
|
18835 | 794 |
"return a number giving the receiver's generality. |
795 |
That number is used to convert one of the arguments in a mixed expression. |
|
1888 | 796 |
The generality has to be defined in subclasses, |
10326 | 797 |
such that gen(a) > gen(b) iff, conversion of b into a's class |
18835 | 798 |
does not cut precision. |
799 |
For example, Integer has 40, Float has 80, meaning that if we convert a Float to an Integer, |
|
800 |
some precision may be lost. |
|
801 |
The generality is used by ArithmeticValue>>retry:coercing:, |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
802 |
which converts the lower-precision number to the higher precision |
18835 | 803 |
number's class, when mixed-type arithmetic is performed." |
10326 | 804 |
|
1888 | 805 |
^ self subclassResponsibility |
806 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
807 |
"Modified: / 5.11.1996 / 15:05:30 / cg" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
808 |
"Modified: / 13.2.1998 / 15:36:01 / stefan" |
1888 | 809 |
! |
810 |
||
811 |
retry:aSymbol coercing:aNumber |
|
812 |
"arithmetic represented by the binary operator, aSymbol, |
|
10326 | 813 |
could not be performed with the receiver and the argument, aNumber, |
814 |
because of the differences in representation. |
|
815 |
Coerce either the receiver or the argument, depending on which has higher |
|
816 |
generality, and try again. |
|
1888 | 817 |
If the operation is compare for same value (=), return false if |
10326 | 818 |
the argument is not a Number. |
1888 | 819 |
If the generalities are the same, create an error message, since this |
820 |
means that a subclass has not been fully implemented." |
|
821 |
||
822 |
|hasGenerality myGenerality otherGenerality| |
|
823 |
||
824 |
hasGenerality := aNumber respondsTo:#generality. |
|
825 |
hasGenerality ifFalse:[ |
|
20060 | 826 |
(aSymbol == #=) ifTrue:[ |
827 |
^ false |
|
828 |
]. |
|
829 |
(aSymbol == #~=) ifTrue:[ |
|
830 |
^ true |
|
831 |
]. |
|
24274 | 832 |
^ ArgumentError raiseErrorString:'retry:coercing: argument is not a number'. |
1888 | 833 |
]. |
834 |
||
835 |
myGenerality := self generality. |
|
836 |
otherGenerality := aNumber generality. |
|
837 |
(myGenerality > otherGenerality) ifTrue:[ |
|
20060 | 838 |
^ self perform:aSymbol with:(self coerce:aNumber) |
1888 | 839 |
]. |
840 |
(myGenerality < otherGenerality) ifTrue:[ |
|
20060 | 841 |
^ (aNumber coerce:self) perform:aSymbol with:aNumber |
1888 | 842 |
]. |
20060 | 843 |
"/ self error:'retry:coercing: oops - same generality; retry should not happen' |
844 |
"same generality may happen with Lazy and LazyValue" |
|
845 |
^ self perform:aSymbol with:aNumber |
|
846 |
||
847 |
" |
|
848 |
([3] lazyValue) * ([2] lazyValue) |
|
849 |
" |
|
1888 | 850 |
|
24274 | 851 |
"Modified: / 05-11-1996 / 15:03:38 / cg" |
852 |
"Modified: / 06-06-2019 / 23:27:43 / Claus Gittinger" |
|
16657 | 853 |
! |
854 |
||
855 |
retry:aSymbol coercing:aNumber with:anArgument |
|
856 |
"arithmetic represented by the binary operator, aSymbol, |
|
857 |
could not be performed with the receiver and the argument, aNumber, |
|
858 |
because of the differences in representation. |
|
859 |
Coerce either the receiver or the argument, depending on which has higher |
|
860 |
generality, and try again. |
|
861 |
If the operation is compare for same value (=), return false if |
|
862 |
the argument is not a Number. |
|
863 |
If the generalities are the same, create an error message, since this |
|
864 |
means that a subclass has not been fully implemented." |
|
865 |
||
866 |
|hasGenerality myGenerality otherGenerality| |
|
867 |
||
868 |
hasGenerality := aNumber respondsTo:#generality. |
|
869 |
hasGenerality ifFalse:[ |
|
24274 | 870 |
^ ArgumentError raiseErrorString:'retry:coercing: argument is not a number'. |
16657 | 871 |
]. |
872 |
||
873 |
myGenerality := self generality. |
|
874 |
otherGenerality := aNumber generality. |
|
875 |
(myGenerality > otherGenerality) ifTrue:[ |
|
876 |
^ self perform:aSymbol with:(self coerce:aNumber) with:anArgument |
|
877 |
]. |
|
878 |
(myGenerality < otherGenerality) ifTrue:[ |
|
879 |
^ (aNumber coerce:self) perform:aSymbol with:aNumber with:anArgument |
|
880 |
]. |
|
20060 | 881 |
"/ self error:'retry:coercing: oops - same generality; retry should not happen' |
882 |
"same generality may happen with Lazy and LazyValue" |
|
883 |
^ self perform:aSymbol with:aNumber with:anArgument |
|
884 |
||
24274 | 885 |
"Modified: / 05-11-1996 / 15:03:38 / cg" |
886 |
"Modified: / 06-06-2019 / 23:27:50 / Claus Gittinger" |
|
1888 | 887 |
! ! |
888 |
||
701 | 889 |
!ArithmeticValue methodsFor:'converting'! |
890 |
||
17222 | 891 |
as32BitIEEEFloatBytesMSB:msb |
892 |
^ self asShortFloat digitBytesMSB:msb |
|
893 |
||
894 |
" |
|
895 |
2 as32BitIEEEFloatBytesMSB:true |
|
896 |
2.0 as32BitIEEEFloatBytesMSB:true |
|
897 |
" |
|
898 |
! |
|
899 |
||
900 |
as64BitIEEEFloatBytesMSB:msb |
|
901 |
^ self asFloat digitBytesMSB:msb |
|
902 |
||
903 |
" |
|
904 |
2 as64BitIEEEFloatBytesMSB:true |
|
905 |
2.0 as64BitIEEEFloatBytesMSB:true |
|
906 |
" |
|
907 |
! |
|
908 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
909 |
asDouble |
18835 | 910 |
"ST80 compatibility: return a double with receiver's value. |
911 |
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
|
912 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
913 |
^ self asFloat |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
914 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
915 |
"Created: / 13.2.1998 / 15:40:14 / stefan" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
916 |
! |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
917 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
918 |
asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
919 |
"return the receiver as fixedPoint number. |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
920 |
Q: what should the scale be here ?" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
921 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
922 |
^ self asFraction asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
923 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
924 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
925 |
0.3 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
926 |
0.5 asFixedPoint |
10326 | 927 |
(1/5) asFloat asFixedPoint |
928 |
(1/3) asFloat asFixedPoint |
|
929 |
(2/3) asFloat asFixedPoint |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
930 |
(1/8) asFloat asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
931 |
3.14159 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
932 |
0.0000001 asFraction |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
933 |
0.0000001 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
934 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
935 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
936 |
"Modified: / 25.10.1997 / 15:36:54 / cg" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
937 |
! |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
938 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
939 |
asFixedPoint:scale |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
940 |
"return the receiver as fixedPoint number with the given |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
941 |
number of post-decimal-digits." |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
942 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
943 |
^ self asFraction asFixedPoint:scale |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
944 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
945 |
" |
10326 | 946 |
0.3 asFixedPoint:4 |
947 |
0.3 asFixedPoint:3 |
|
948 |
0.3 asFixedPoint:2 |
|
949 |
0.3 asFixedPoint:1 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
950 |
0.3 asFixedPoint:0 |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
951 |
|
10326 | 952 |
0.5 asFixedPoint:3 |
953 |
(1/5) asFloat asFixedPoint:1 |
|
954 |
(1/8) asFloat asFixedPoint:1 |
|
955 |
1.0 asFixedPoint:2 |
|
956 |
3.14159 asFixedPoint:2 |
|
957 |
3.14159 asFixedPoint:3 |
|
958 |
(3.14159 asFixedPoint:2) asFixedPoint:5 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
959 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
960 |
! |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
961 |
|
12980 | 962 |
asFixedPointRoundedToScale |
963 |
"return the receiver as fixedPoint number, rounded to its scale." |
|
964 |
||
965 |
^ self asFixedPoint roundedToScale |
|
966 |
||
967 |
" |
|
968 |
0.3 asFixedPoint |
|
969 |
0.5 asFixedPoint |
|
970 |
(2/3) asFloat asFixedPoint |
|
971 |
(1/8) asFloat asFixedPoint |
|
972 |
3.14159 asFixedPoint |
|
973 |
||
974 |
0.3 asFixedPointRoundedToScale |
|
975 |
0.5 asFixedPointRoundedToScale |
|
976 |
(2/3) asFloat asFixedPointRoundedToScale |
|
977 |
(1/8) asFloat asFixedPointRoundedToScale |
|
978 |
3.14159 asFixedPointRoundedToScale |
|
979 |
" |
|
980 |
||
981 |
"Created: / 02-08-2010 / 13:32:16 / cg" |
|
982 |
! |
|
983 |
||
984 |
asFixedPointRoundedToScale:scale |
|
985 |
"return the receiver as fixedPoint number with the given |
|
986 |
number of post-decimal-digits, rounded to its scale" |
|
987 |
||
988 |
^ (self asFixedPoint:scale) roundedToScale |
|
989 |
||
17336 | 990 |
" |
991 |
3.14159 asFixedPointRoundedToScale:1 |
|
992 |
3.14159 asFixedPointRoundedToScale:2 |
|
993 |
3.14159 asFixedPointRoundedToScale:3 |
|
994 |
3.14159 asFixedPointRoundedToScale:4 |
|
995 |
" |
|
996 |
||
12980 | 997 |
"Created: / 02-08-2010 / 13:33:11 / cg" |
998 |
! |
|
999 |
||
1 | 1000 |
asFloat |
1001 |
"return a float with same value" |
|
1002 |
||
1003 |
^ self subclassResponsibility |
|
1004 |
! |
|
1005 |
||
5986 | 1006 |
asFloatD |
1007 |
"return a double precision float with same value. |
|
1008 |
Added for ANSI compatibility" |
|
1009 |
||
1010 |
^ self asFloat |
|
1011 |
||
1012 |
"Created: / 7.9.2001 / 13:36:48 / cg" |
|
1013 |
"Modified: / 7.9.2001 / 13:38:10 / cg" |
|
1014 |
! |
|
1015 |
||
1016 |
asFloatE |
|
1017 |
"return a single precision float with same value. |
|
1018 |
Added for ANSI compatibility" |
|
1019 |
||
1020 |
^ self asShortFloat |
|
1021 |
||
1022 |
"Created: / 7.9.2001 / 13:37:06 / cg" |
|
1023 |
"Modified: / 7.9.2001 / 13:38:15 / cg" |
|
1024 |
! |
|
1025 |
||
1026 |
asFloatQ |
|
1027 |
"return a quad precision float with same value. |
|
21879 | 1028 |
Notice that longFloats as returned here may or may not provide more |
21940 | 1029 |
precision than a double - depending on the machine's CPU |
1030 |
(and usually do not provide quad the number of bits of a float) |
|
5986 | 1031 |
Added for ANSI compatibility" |
1032 |
||
1033 |
^ self asLongFloat |
|
1034 |
||
21879 | 1035 |
"Created: / 07-09-2001 / 13:38:44 / cg" |
21940 | 1036 |
"Modified (comment): / 03-07-2017 / 10:19:13 / cg" |
1037 |
! |
|
1038 |
||
1039 |
asFloatQD |
|
1040 |
"return a quad double precision float with same value." |
|
1041 |
||
1042 |
^ self asQDouble |
|
1043 |
||
1044 |
"Created: / 03-07-2017 / 10:19:41 / cg" |
|
5986 | 1045 |
! |
1046 |
||
1 | 1047 |
asFraction |
1048 |
"return a fraction with same value" |
|
1049 |
||
1050 |
^ self subclassResponsibility |
|
1051 |
! |
|
1052 |
||
701 | 1053 |
asInteger |
1054 |
"return an integer with same value - might truncate" |
|
1055 |
||
1056 |
^ self truncated |
|
1057 |
! |
|
1058 |
||
24279 | 1059 |
asLargeFloat |
1060 |
"return a largeFloat with same value" |
|
1061 |
||
1062 |
"WARNING: could loose precision here, if not redefined in concrete classes which |
|
1063 |
have more than float precision (i.e. LargeIntegers and Fractions)" |
|
1064 |
||
1065 |
^ self asFloat asLargeFloat "/ subclassResponsibility |
|
1066 |
||
1067 |
"Created: / 07-06-2019 / 02:32:57 / Claus Gittinger" |
|
1068 |
! |
|
1069 |
||
7378 | 1070 |
asLimitedPrecisionReal |
1071 |
"return a float of any precision with same value" |
|
1072 |
||
1073 |
^ self asFloat |
|
1074 |
! |
|
1075 |
||
5986 | 1076 |
asLongFloat |
1077 |
"return a longFloat with same value" |
|
1078 |
||
10315 | 1079 |
"WARNING: could loose precision here, if not redefined in concrete classes which |
1080 |
have more than float precision (i.e. LargeIntegers and Fractions)" |
|
7355 | 1081 |
|
10315 | 1082 |
^ self asFloat asLongFloat "/ subclassResponsibility |
5986 | 1083 |
|
1084 |
"Modified: / 17.4.1996 / 12:21:35 / cg" |
|
1085 |
"Created: / 7.9.2001 / 13:39:31 / cg" |
|
1086 |
! |
|
1087 |
||
5987 | 1088 |
asScaledDecimal:scale |
18835 | 1089 |
"return a fixedPoint approximating the receiver's value" |
5987 | 1090 |
|
1091 |
^ self asFixedPoint:scale |
|
1092 |
||
1093 |
" |
|
1094 |
1.234 asScaledDecimal:2 |
|
1095 |
" |
|
1096 |
||
1097 |
"Created: / 7.9.2001 / 13:46:26 / cg" |
|
1098 |
"Modified: / 7.9.2001 / 13:46:44 / cg" |
|
1099 |
! |
|
1100 |
||
1200 | 1101 |
asShortFloat |
1102 |
"return a shortFloat with same value" |
|
1103 |
||
1104 |
^ self asFloat asShortFloat |
|
1105 |
||
1106 |
"Modified: 17.4.1996 / 12:21:35 / cg" |
|
1107 |
! |
|
1108 |
||
701 | 1109 |
degreesToRadians |
21106 | 1110 |
"interpreting the receiver as degrees, return the radians" |
701 | 1111 |
|
1112 |
^ self asFloat degreesToRadians |
|
1113 |
! |
|
1114 |
||
1115 |
radiansToDegrees |
|
21106 | 1116 |
"interpreting the receiver as radians, return the degrees" |
701 | 1117 |
|
1118 |
^ self asFloat radiansToDegrees |
|
1 | 1119 |
! ! |
1120 |
||
701 | 1121 |
!ArithmeticValue methodsFor:'double dispatching'! |
1 | 1122 |
|
23499 | 1123 |
bitAndFromInteger:anInteger |
1124 |
"anInteger does not know how to do bitAnd: with the receiver - |
|
1125 |
retry the operation by coercing to Integer" |
|
1126 |
||
1127 |
|selfAsInteger| |
|
1128 |
||
1129 |
selfAsInteger := self asInteger. |
|
1130 |
self ~= selfAsInteger ifTrue:[ |
|
1131 |
DomainError raiseRequestErrorString:'non-integer mask in bit-operation'. |
|
1132 |
]. |
|
1133 |
||
1134 |
^ anInteger bitAnd:selfAsInteger |
|
1135 |
||
1136 |
"Created: / 01-11-2018 / 11:49:27 / Stefan Vogel" |
|
1137 |
! |
|
1138 |
||
1139 |
bitOrFromInteger:anInteger |
|
1140 |
"anInteger does not know how to do bitOr: with the receiver - |
|
1141 |
retry the operation by coercing to Integer" |
|
1142 |
||
1143 |
|selfAsInteger| |
|
1144 |
||
1145 |
selfAsInteger := self asInteger. |
|
1146 |
self ~= selfAsInteger ifTrue:[ |
|
1147 |
DomainError raiseRequestErrorString:'non-integer mask in bit-operation'. |
|
1148 |
]. |
|
1149 |
||
1150 |
^ anInteger bitOr:selfAsInteger |
|
1151 |
||
1152 |
"Created: / 01-11-2018 / 11:49:09 / Stefan Vogel" |
|
1153 |
! |
|
1154 |
||
1155 |
bitXorFromInteger:anInteger |
|
1156 |
"anInteger does not know how to do bitXor: with the receiver - |
|
1157 |
retry the operation by coercing to Integer" |
|
1158 |
||
1159 |
|selfAsInteger| |
|
1160 |
||
1161 |
selfAsInteger := self asInteger. |
|
1162 |
self ~= selfAsInteger ifTrue:[ |
|
1163 |
DomainError raiseRequestErrorString:'non-integer mask in bit-operation'. |
|
1164 |
]. |
|
1165 |
||
1166 |
^ anInteger bitXor:selfAsInteger |
|
1167 |
||
1168 |
"Created: / 01-11-2018 / 11:58:53 / Stefan Vogel" |
|
1169 |
! |
|
1170 |
||
7355 | 1171 |
differenceFromComplex:aComplex |
21797 | 1172 |
"aComplex does not know how to subtract the receiver - |
7355 | 1173 |
retry the operation by coercing to higher generality" |
1174 |
||
1175 |
^ aComplex retry:#- coercing:self |
|
21797 | 1176 |
|
1177 |
"Modified (comment): / 12-06-2017 / 20:34:41 / cg" |
|
7355 | 1178 |
! |
1179 |
||
1888 | 1180 |
differenceFromFixedPoint:aFixedPoint |
21797 | 1181 |
"aFixedPoint does not know how to subtract the receiver - |
1888 | 1182 |
retry the operation by coercing to higher generality" |
1183 |
||
1184 |
^ aFixedPoint retry:#- coercing:self |
|
1185 |
||
21797 | 1186 |
"Created: / 05-11-1996 / 14:55:51 / cg" |
1187 |
"Modified (comment): / 12-06-2017 / 20:34:33 / cg" |
|
1888 | 1188 |
! |
1189 |
||
701 | 1190 |
differenceFromFloat:aFloat |
21797 | 1191 |
"aFloat does not know how to subtract the receiver - |
701 | 1192 |
retry the operation by coercing to higher generality" |
1 | 1193 |
|
701 | 1194 |
^ aFloat retry:#- coercing:self |
21797 | 1195 |
|
1196 |
"Modified (comment): / 12-06-2017 / 20:34:23 / cg" |
|
1 | 1197 |
! |
1198 |
||
701 | 1199 |
differenceFromFraction:aFraction |
21797 | 1200 |
"aFraction does not know how to subtract the receiver - |
701 | 1201 |
retry the operation by coercing to higher generality" |
1 | 1202 |
|
701 | 1203 |
^ aFraction retry:#- coercing:self |
21797 | 1204 |
|
1205 |
"Modified (comment): / 12-06-2017 / 20:34:16 / cg" |
|
1 | 1206 |
! |
1207 |
||
701 | 1208 |
differenceFromInteger:anInteger |
21797 | 1209 |
"anInteger does not know how to subtract the receiver - |
701 | 1210 |
retry the operation by coercing to higher generality" |
1 | 1211 |
|
701 | 1212 |
^ anInteger retry:#- coercing:self |
21797 | 1213 |
|
1214 |
"Modified (comment): / 12-06-2017 / 20:34:07 / cg" |
|
1 | 1215 |
! |
1216 |
||
7440 | 1217 |
differenceFromLargeFloat:aLargeFloat |
21797 | 1218 |
"aLargeFloat does not know how to subtract the receiver - |
7440 | 1219 |
retry the operation by coercing to higher generality" |
1220 |
||
1221 |
^ aLargeFloat retry:#- coercing:self |
|
21797 | 1222 |
|
1223 |
"Modified (comment): / 12-06-2017 / 20:34:00 / cg" |
|
7440 | 1224 |
! |
1225 |
||
7399 | 1226 |
differenceFromLongFloat:aLongFloat |
21797 | 1227 |
"aLongFloat does not know how to subtract the receiver - |
7399 | 1228 |
retry the operation by coercing to higher generality" |
1229 |
||
1230 |
^ aLongFloat retry:#- coercing:self |
|
1231 |
||
21797 | 1232 |
"Created: / 17-04-1996 / 12:33:16 / cg" |
1233 |
"Modified (comment): / 12-06-2017 / 20:33:50 / cg" |
|
7399 | 1234 |
! |
1235 |
||
24278 | 1236 |
differenceFromQuadFloat:aQuadFloat |
1237 |
"aQuadFloat does not know how to subtract the receiver - |
|
1238 |
retry the operation by coercing to higher generality" |
|
1239 |
||
1240 |
^ aQuadFloat retry:#- coercing:self |
|
1241 |
||
1242 |
"Created: / 07-06-2019 / 02:20:52 / Claus Gittinger" |
|
1243 |
! |
|
1244 |
||
1200 | 1245 |
differenceFromShortFloat:aShortFloat |
21797 | 1246 |
"aShortFloat does not know how to subtract the receiver - |
1200 | 1247 |
retry the operation by coercing to higher generality" |
1248 |
||
1249 |
^ aShortFloat retry:#- coercing:self |
|
1250 |
||
21797 | 1251 |
"Created: / 17-04-1996 / 12:33:16 / cg" |
1252 |
"Modified (comment): / 12-06-2017 / 20:33:43 / cg" |
|
1200 | 1253 |
! |
1254 |
||
7355 | 1255 |
equalFromComplex:aComplex |
21797 | 1256 |
"aComplex does not know how to compare to the receiver - |
7355 | 1257 |
retry the operation by coercing to higher generality" |
1258 |
||
1259 |
^ aComplex retry:#= coercing:self |
|
21797 | 1260 |
|
1261 |
"Modified (comment): / 12-06-2017 / 20:33:26 / cg" |
|
7355 | 1262 |
! |
1263 |
||
1264 |
equalFromFixedPoint:aFixedPoint |
|
21797 | 1265 |
"aFixedPoint does not know how to compare to the receiver - |
7355 | 1266 |
retry the operation by coercing to higher generality" |
1267 |
||
1268 |
^ aFixedPoint retry:#= coercing:self |
|
21797 | 1269 |
|
1270 |
"Modified (comment): / 12-06-2017 / 20:33:17 / cg" |
|
7355 | 1271 |
! |
1272 |
||
1273 |
equalFromFloat:aFloat |
|
21797 | 1274 |
"aFloat does not know how to compare to the receiver - |
7355 | 1275 |
retry the operation by coercing to higher generality" |
1276 |
||
1277 |
^ aFloat retry:#= coercing:self |
|
21797 | 1278 |
|
1279 |
"Modified (comment): / 12-06-2017 / 20:33:09 / cg" |
|
7355 | 1280 |
! |
1281 |
||
1282 |
equalFromFraction:aFraction |
|
21797 | 1283 |
"aFraction does not know how to compare to the receiver - |
7355 | 1284 |
retry the operation by coercing to higher generality" |
1285 |
||
1286 |
^ aFraction retry:#= coercing:self |
|
21797 | 1287 |
|
1288 |
"Modified (comment): / 12-06-2017 / 20:33:01 / cg" |
|
7355 | 1289 |
! |
1290 |
||
1291 |
equalFromInteger:anInteger |
|
21797 | 1292 |
"anInteger does not know how to compare to the receiver - |
7355 | 1293 |
retry the operation by coercing to higher generality" |
1294 |
||
1295 |
^ anInteger retry:#= coercing:self |
|
21797 | 1296 |
|
1297 |
"Modified (comment): / 12-06-2017 / 20:32:53 / cg" |
|
7355 | 1298 |
! |
1299 |
||
7440 | 1300 |
equalFromLargeFloat:aLargeFloat |
21797 | 1301 |
"aLargeFloat does not know how to compare to the receiver - |
7440 | 1302 |
retry the operation by coercing to higher generality" |
1303 |
||
1304 |
^ aLargeFloat retry:#= coercing:self |
|
21797 | 1305 |
|
1306 |
"Modified (comment): / 12-06-2017 / 20:32:45 / cg" |
|
7440 | 1307 |
! |
1308 |
||
7355 | 1309 |
equalFromLongFloat:aLongFloat |
21797 | 1310 |
"aLongFloat does not know how to compare to the receiver - |
7355 | 1311 |
retry the operation by coercing to higher generality" |
1312 |
||
1313 |
^ aLongFloat retry:#= coercing:self |
|
21797 | 1314 |
|
1315 |
"Modified (comment): / 12-06-2017 / 20:32:36 / cg" |
|
7355 | 1316 |
! |
1317 |
||
24278 | 1318 |
equalFromQuadFloat:aQuadFloat |
1319 |
"aQuadFloat does not know how to compare to the receiver - |
|
1320 |
retry the operation by coercing to higher generality" |
|
1321 |
||
1322 |
^ aQuadFloat retry:#= coercing:self |
|
1323 |
||
1324 |
"Created: / 07-06-2019 / 02:21:11 / Claus Gittinger" |
|
1325 |
! |
|
1326 |
||
7355 | 1327 |
equalFromShortFloat:aShortFloat |
21797 | 1328 |
"aShortFloat does not know how to compare to the receiver - |
7355 | 1329 |
retry the operation by coercing to higher generality" |
1330 |
||
1331 |
^ aShortFloat retry:#= coercing:self |
|
21797 | 1332 |
|
1333 |
"Modified (comment): / 12-06-2017 / 20:32:28 / cg" |
|
7355 | 1334 |
! |
1335 |
||
13007 | 1336 |
integerQuotientFromInteger:anInteger |
1337 |
"anInteger does not know how to divide by the receiver - |
|
1338 |
retry the operation by coercing to higher generality" |
|
1339 |
||
1340 |
^ anInteger retry:#// coercing:self |
|
1341 |
||
1342 |
"Created: / 09-08-2010 / 19:49:26 / cg" |
|
1343 |
! |
|
1344 |
||
16657 | 1345 |
isAlmostEqualToFromFloat:aFloat nEpsilon:nE |
24278 | 1346 |
"aFloat does not know how to compare to the receiver - |
16657 | 1347 |
retry the operation by coercing to higher generality" |
1348 |
||
1349 |
^ aFloat retry:#isAlmostEqualTo:nEpsilon: coercing:self with:nE |
|
24278 | 1350 |
|
1351 |
"Modified (comment): / 07-06-2019 / 02:23:00 / Claus Gittinger" |
|
1352 |
! |
|
1353 |
||
1354 |
isAlmostEqualToFromLargeFloat:aLargeFloat nEpsilon:nE |
|
1355 |
"aLargeFloat does not know how to compare to the receiver - |
|
1356 |
retry the operation by coercing to higher generality" |
|
1357 |
||
1358 |
^ aLargeFloat retry:#isAlmostEqualTo:nEpsilon: coercing:self with:nE |
|
1359 |
||
1360 |
"Created: / 07-06-2019 / 02:24:57 / Claus Gittinger" |
|
1361 |
! |
|
1362 |
||
1363 |
isAlmostEqualToFromLongFloat:aLongFloat nEpsilon:nE |
|
1364 |
"aLongFloat does not know how to compare to the receiver - |
|
1365 |
retry the operation by coercing to higher generality" |
|
1366 |
||
1367 |
^ aLongFloat retry:#isAlmostEqualTo:nEpsilon: coercing:self with:nE |
|
1368 |
||
1369 |
"Created: / 07-06-2019 / 02:22:01 / Claus Gittinger" |
|
1370 |
! |
|
1371 |
||
1372 |
isAlmostEqualToFromQuadFloat:aQuadFloat nEpsilon:nE |
|
1373 |
"aQuadFloat does not know how to compare to the receiver - |
|
1374 |
retry the operation by coercing to higher generality" |
|
1375 |
||
1376 |
^ aQuadFloat retry:#isAlmostEqualTo:nEpsilon: coercing:self with:nE |
|
1377 |
||
1378 |
"Created: / 07-06-2019 / 02:22:15 / Claus Gittinger" |
|
16657 | 1379 |
! |
1380 |
||
1381 |
isAlmostEqualToFromShortFloat:aShortFloat nEpsilon:nE |
|
24278 | 1382 |
"aShortFloat does not know how to compare to the receiver - |
16657 | 1383 |
retry the operation by coercing to higher generality" |
1384 |
||
1385 |
^ aShortFloat retry:#isAlmostEqualTo:nEpsilon: coercing:self with:nE |
|
24278 | 1386 |
|
1387 |
"Modified (comment): / 07-06-2019 / 02:23:25 / Claus Gittinger" |
|
16657 | 1388 |
! |
1389 |
||
1888 | 1390 |
lessFromFixedPoint:aFixedPoint |
21797 | 1391 |
"aFixedPoint does not know how to compare to the receiver - |
18860 | 1392 |
Return true if aFixedPoint < self. |
1888 | 1393 |
retry the operation by coercing to higher generality" |
1394 |
||
1395 |
^ aFixedPoint retry:#< coercing:self |
|
1396 |
||
21797 | 1397 |
"Created: / 05-11-1996 / 14:56:12 / cg" |
1398 |
"Modified (comment): / 12-06-2017 / 20:32:12 / cg" |
|
1888 | 1399 |
! |
1400 |
||
701 | 1401 |
lessFromFloat:aFloat |
21797 | 1402 |
"aFloat does not know how to compare to the receiver - |
18860 | 1403 |
Return true if aFloat < self. |
701 | 1404 |
retry the operation by coercing to higher generality" |
1 | 1405 |
|
701 | 1406 |
^ aFloat retry:#< coercing:self |
21797 | 1407 |
|
1408 |
"Modified (comment): / 12-06-2017 / 20:32:02 / cg" |
|
1 | 1409 |
! |
1410 |
||
701 | 1411 |
lessFromFraction:aFraction |
21797 | 1412 |
"aFraction does not know how to compare to the receiver - |
18860 | 1413 |
Return true if aFraction < self. |
701 | 1414 |
retry the operation by coercing to higher generality" |
1 | 1415 |
|
701 | 1416 |
^ aFraction retry:#< coercing:self |
21797 | 1417 |
|
1418 |
"Modified (comment): / 12-06-2017 / 20:31:53 / cg" |
|
1 | 1419 |
! |
1420 |
||
701 | 1421 |
lessFromInteger:anInteger |
21797 | 1422 |
"anInteger does not know how to compare to the receiver - |
18860 | 1423 |
Return true if anInteger < self. |
701 | 1424 |
retry the operation by coercing to higher generality" |
1 | 1425 |
|
701 | 1426 |
^ anInteger retry:#< coercing:self |
21797 | 1427 |
|
1428 |
"Modified (comment): / 12-06-2017 / 20:31:44 / cg" |
|
1 | 1429 |
! |
1430 |
||
7440 | 1431 |
lessFromLargeFloat:aLargeFloat |
21797 | 1432 |
"aLargeFloat does not know how to compare to the receiver - |
18860 | 1433 |
Return true if aLargeFloat < self. |
7440 | 1434 |
retry the operation by coercing to higher generality" |
1435 |
||
1436 |
^ aLargeFloat retry:#< coercing:self |
|
21797 | 1437 |
|
1438 |
"Modified (comment): / 12-06-2017 / 20:31:36 / cg" |
|
7440 | 1439 |
! |
1440 |
||
7399 | 1441 |
lessFromLongFloat:aLongFloat |
21797 | 1442 |
"aLongFloat does not know how to compare to the receiver - |
18860 | 1443 |
Return true if aLongFloat < self. |
7399 | 1444 |
retry the operation by coercing to higher generality" |
1445 |
||
1446 |
^ aLongFloat retry:#< coercing:self |
|
1447 |
||
21797 | 1448 |
"Modified: / 17-04-1996 / 12:33:33 / cg" |
1449 |
"Modified (comment): / 12-06-2017 / 20:31:28 / cg" |
|
7399 | 1450 |
! |
1451 |
||
24278 | 1452 |
lessFromQuadFloat:aQuadFloat |
1453 |
"aQuadFloat does not know how to compare to the receiver - |
|
1454 |
Return true if aQuadFloat < self. |
|
1455 |
retry the operation by coercing to higher generality" |
|
1456 |
||
1457 |
^ aQuadFloat retry:#< coercing:self |
|
1458 |
||
1459 |
"Created: / 07-06-2019 / 02:23:41 / Claus Gittinger" |
|
1460 |
! |
|
1461 |
||
1200 | 1462 |
lessFromShortFloat:aShortFloat |
21797 | 1463 |
"aShortFloat does not know how to compare to the receiver - |
18860 | 1464 |
Return true if aShortFloat < self. |
1200 | 1465 |
retry the operation by coercing to higher generality" |
1466 |
||
1467 |
^ aShortFloat retry:#< coercing:self |
|
1468 |
||
21797 | 1469 |
"Modified: / 17-04-1996 / 12:33:33 / cg" |
1470 |
"Modified (comment): / 12-06-2017 / 20:31:18 / cg" |
|
1200 | 1471 |
! |
1472 |
||
14002 | 1473 |
moduloFromInteger:anInteger |
1474 |
"anInteger does not know how to compute the modulo from the receiver - |
|
1475 |
retry the operation by coercing to higher generality" |
|
1476 |
||
1477 |
^ anInteger retry:#\\ coercing:self |
|
1478 |
||
1479 |
"Created: / 12-02-2012 / 20:38:13 / cg" |
|
1480 |
! |
|
1481 |
||
7355 | 1482 |
productFromComplex:aComplex |
21797 | 1483 |
"aComplex does not know how to multiply the receiver - |
7355 | 1484 |
retry the operation by coercing to higher generality" |
1485 |
||
1486 |
^ aComplex retry:#* coercing:self |
|
21797 | 1487 |
|
1488 |
"Modified (comment): / 12-06-2017 / 20:31:06 / cg" |
|
7355 | 1489 |
! |
1490 |
||
1888 | 1491 |
productFromFixedPoint:aFixedPoint |
21797 | 1492 |
"aFixedPoint does not know how to multiply the receiver - |
1888 | 1493 |
retry the operation by coercing to higher generality" |
1494 |
||
1495 |
^ aFixedPoint retry:#* coercing:self |
|
1496 |
||
21797 | 1497 |
"Created: / 05-11-1996 / 14:56:28 / cg" |
1498 |
"Modified (comment): / 12-06-2017 / 20:30:56 / cg" |
|
1888 | 1499 |
! |
1500 |
||
701 | 1501 |
productFromFloat:aFloat |
21797 | 1502 |
"aFloat does not know how to multiply the receiver - |
701 | 1503 |
retry the operation by coercing to higher generality" |
1 | 1504 |
|
701 | 1505 |
^ aFloat retry:#* coercing:self |
21797 | 1506 |
|
1507 |
"Modified (comment): / 12-06-2017 / 20:30:46 / cg" |
|
1 | 1508 |
! |
1509 |
||
701 | 1510 |
productFromFraction:aFraction |
21797 | 1511 |
"aFraction does not know how to multiply the receiver - |
701 | 1512 |
retry the operation by coercing to higher generality" |
1 | 1513 |
|
701 | 1514 |
^ aFraction retry:#* coercing:self |
21797 | 1515 |
|
1516 |
"Modified (comment): / 12-06-2017 / 20:30:38 / cg" |
|
1 | 1517 |
! |
1518 |
||
701 | 1519 |
productFromInteger:anInteger |
21797 | 1520 |
"anInteger does not know how to multiply the receiver - |
701 | 1521 |
retry the operation by coercing to higher generality" |
1 | 1522 |
|
701 | 1523 |
^ anInteger retry:#* coercing:self |
21797 | 1524 |
|
1525 |
"Modified (comment): / 12-06-2017 / 20:29:21 / cg" |
|
1 | 1526 |
! |
1527 |
||
7440 | 1528 |
productFromLargeFloat:aLargeFloat |
21797 | 1529 |
"aLargeFloat does not know how to multiply the receiver - |
7440 | 1530 |
retry the operation by coercing to higher generality" |
1531 |
||
1532 |
^ aLargeFloat retry:#* coercing:self |
|
1533 |
||
21797 | 1534 |
"Created: / 17-04-1996 / 12:33:48 / cg" |
1535 |
"Modified (comment): / 12-06-2017 / 20:29:13 / cg" |
|
7440 | 1536 |
! |
1537 |
||
7399 | 1538 |
productFromLongFloat:aLongFloat |
21797 | 1539 |
"aLongFloat does not know how to multiply the receiver - |
7399 | 1540 |
retry the operation by coercing to higher generality" |
1541 |
||
1542 |
^ aLongFloat retry:#* coercing:self |
|
1543 |
||
21797 | 1544 |
"Created: / 17-04-1996 / 12:33:48 / cg" |
1545 |
"Modified (comment): / 12-06-2017 / 20:29:05 / cg" |
|
7399 | 1546 |
! |
1547 |
||
24278 | 1548 |
productFromQuadFloat:aQuadFloat |
1549 |
"aQuadFloat does not know how to multiply the receiver - |
|
1550 |
retry the operation by coercing to higher generality" |
|
1551 |
||
1552 |
^ aQuadFloat retry:#* coercing:self |
|
1553 |
||
1554 |
"Created: / 07-06-2019 / 02:24:05 / Claus Gittinger" |
|
1555 |
! |
|
1556 |
||
1200 | 1557 |
productFromShortFloat:aShortFloat |
21797 | 1558 |
"aShortFloat does not know how to multiply the receiver - |
1200 | 1559 |
retry the operation by coercing to higher generality" |
1560 |
||
1561 |
^ aShortFloat retry:#* coercing:self |
|
1562 |
||
21797 | 1563 |
"Created: / 17-04-1996 / 12:33:48 / cg" |
1564 |
"Modified (comment): / 12-06-2017 / 20:28:56 / cg" |
|
1200 | 1565 |
! |
1566 |
||
7355 | 1567 |
quotientFromComplex:aComplex |
7440 | 1568 |
"aComplex does not know how to divide by the receiver - |
7355 | 1569 |
retry the operation by coercing to higher generality" |
1570 |
||
1571 |
^ aComplex retry:#/ coercing:self |
|
1572 |
! |
|
1573 |
||
1888 | 1574 |
quotientFromFixedPoint:aFixedPoint |
7440 | 1575 |
"aFixedPoint does not know how to divide by the receiver - |
1888 | 1576 |
retry the operation by coercing to higher generality" |
1577 |
||
1578 |
^ aFixedPoint retry:#/ coercing:self |
|
1579 |
||
1580 |
"Created: 5.11.1996 / 14:56:41 / cg" |
|
1581 |
! |
|
1582 |
||
701 | 1583 |
quotientFromFloat:aFloat |
7440 | 1584 |
"aFloat does not know how to divide by the receiver - |
701 | 1585 |
retry the operation by coercing to higher generality" |
1 | 1586 |
|
701 | 1587 |
^ aFloat retry:#/ coercing:self |
1 | 1588 |
! |
1589 |
||
701 | 1590 |
quotientFromFraction:aFraction |
7440 | 1591 |
"aFraction does not know how to divide by the receiver - |
701 | 1592 |
retry the operation by coercing to higher generality" |
1 | 1593 |
|
701 | 1594 |
^ aFraction retry:#/ coercing:self |
1 | 1595 |
! |
1596 |
||
21797 | 1597 |
quotientFromInteger:aQDouble |
1598 |
"aQDouble does not know how to divide by the receiver - |
|
1 | 1599 |
retry the operation by coercing to higher generality" |
1600 |
||
21797 | 1601 |
^ aQDouble retry:#/ coercing:self |
1602 |
||
1603 |
"Modified (comment): / 12-06-2017 / 20:35:45 / cg" |
|
1 | 1604 |
! |
1605 |
||
7440 | 1606 |
quotientFromLargeFloat:aLargeFloat |
1607 |
"aLargeFloat does not know how to divide by the receiver - |
|
1608 |
retry the operation by coercing to higher generality" |
|
1609 |
||
1610 |
^ aLargeFloat retry:#/ coercing:self |
|
1611 |
||
1612 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
1613 |
! |
|
1614 |
||
7399 | 1615 |
quotientFromLongFloat:aLongFloat |
7440 | 1616 |
"aLongFloat does not know how to divide by the receiver - |
7399 | 1617 |
retry the operation by coercing to higher generality" |
1618 |
||
1619 |
^ aLongFloat retry:#/ coercing:self |
|
1620 |
||
1621 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
1622 |
! |
|
1623 |
||
24278 | 1624 |
quotientFromQuadFloat:aQuadFloat |
1625 |
"aQuadFloat does not know how to divide by the receiver - |
|
1626 |
retry the operation by coercing to higher generality" |
|
1627 |
||
1628 |
^ aQuadFloat retry:#/ coercing:self |
|
1629 |
||
1630 |
"Created: / 07-06-2019 / 02:25:19 / Claus Gittinger" |
|
1631 |
! |
|
1632 |
||
1200 | 1633 |
quotientFromShortFloat:aShortFloat |
7440 | 1634 |
"aShortFloat does not know how to divide by the receiver - |
1200 | 1635 |
retry the operation by coercing to higher generality" |
1636 |
||
1637 |
^ aShortFloat retry:#/ coercing:self |
|
1638 |
||
1639 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
1640 |
! |
|
1641 |
||
21935 | 1642 |
raisedFromFloat:aFloat |
1643 |
"aFloat does not know how to be raised to the receiver" |
|
1644 |
||
1645 |
self isNumber ifTrue:[ |
|
1646 |
self isFloat ifFalse:[ |
|
1647 |
^ self asFloat raisedFromFloat:aFloat |
|
1648 |
]. |
|
1649 |
]. |
|
1650 |
^ self raisedFromNumber:aFloat |
|
1651 |
||
1652 |
"Created: / 01-07-2017 / 21:18:54 / cg" |
|
1653 |
"Modified: / 02-07-2017 / 00:44:47 / cg" |
|
1654 |
! |
|
1655 |
||
1656 |
raisedFromNumber:aNumber |
|
21967 | 1657 |
"aNumber does not know how to be raised to the receiver |
1658 |
(i.e. how to compute aNumber^self)" |
|
21935 | 1659 |
|
1660 |
^ self class |
|
1661 |
raise:#domainErrorSignal |
|
1662 |
receiver:aNumber |
|
1663 |
selector:#raisedTo: |
|
1664 |
arguments:(Array with:self) |
|
1665 |
errorString:'bad receiver/arg in raisedTo:' |
|
1666 |
||
1667 |
"Created: / 01-07-2017 / 21:17:06 / cg" |
|
21967 | 1668 |
"Modified (comment): / 03-07-2017 / 14:06:53 / cg" |
21935 | 1669 |
! |
1670 |
||
14747 | 1671 |
remainderFromFloat:aFloat |
1672 |
"aFloat does not know how to compute the remainder with the receiver - |
|
1673 |
retry the operation by coercing to higher generality" |
|
1674 |
||
1675 |
^ aFloat retry:#rem: coercing:self |
|
1676 |
! |
|
1677 |
||
24278 | 1678 |
remainderFromLargeFloat:aLargeFloat |
1679 |
"aLargeFloat does not know how to compute the remainder with the receiver - |
|
1680 |
retry the operation by coercing to higher generality" |
|
1681 |
||
1682 |
^ aLargeFloat retry:#rem: coercing:self |
|
1683 |
||
1684 |
"Created: / 07-06-2019 / 02:25:57 / Claus Gittinger" |
|
1685 |
! |
|
1686 |
||
1687 |
remainderFromLongFloat:aLongFloat |
|
1688 |
"aLongFloat does not know how to compute the remainder with the receiver - |
|
1689 |
retry the operation by coercing to higher generality" |
|
1690 |
||
1691 |
^ aLongFloat retry:#rem: coercing:self |
|
1692 |
||
1693 |
"Created: / 07-06-2019 / 02:25:43 / Claus Gittinger" |
|
1694 |
! |
|
1695 |
||
1696 |
remainderFromQuadFloat:aQuadFloat |
|
1697 |
"aQuadFloat does not know how to compute the remainder with the receiver - |
|
1698 |
retry the operation by coercing to higher generality" |
|
1699 |
||
1700 |
^ aQuadFloat retry:#rem: coercing:self |
|
1701 |
||
1702 |
"Created: / 07-06-2019 / 02:26:08 / Claus Gittinger" |
|
1703 |
! |
|
1704 |
||
14747 | 1705 |
remainderFromShortFloat:aShortFloat |
1706 |
"aShortFloat does not know how to compute the remainder with the receiver - |
|
1707 |
retry the operation by coercing to higher generality" |
|
1708 |
||
1709 |
^ aShortFloat retry:#rem: coercing:self |
|
1710 |
! |
|
1711 |
||
7355 | 1712 |
sumFromComplex:aComplex |
21797 | 1713 |
"aComplex does not know how to add the receiver - |
7355 | 1714 |
retry the operation by coercing to higher generality" |
1715 |
||
1716 |
^ aComplex retry:#+ coercing:self |
|
21797 | 1717 |
|
1718 |
"Modified (comment): / 12-06-2017 / 20:27:25 / cg" |
|
7355 | 1719 |
! |
1720 |
||
1888 | 1721 |
sumFromFixedPoint:aFixedPoint |
21797 | 1722 |
"aFixedPoint does not know how to add the receiver - |
1888 | 1723 |
retry the operation by coercing to higher generality" |
1724 |
||
1725 |
^ aFixedPoint retry:#+ coercing:self |
|
1726 |
||
21797 | 1727 |
"Created: / 05-11-1996 / 14:56:56 / cg" |
1728 |
"Modified (comment): / 12-06-2017 / 20:27:38 / cg" |
|
1888 | 1729 |
! |
1730 |
||
1 | 1731 |
sumFromFloat:aFloat |
21797 | 1732 |
"aFloat does not know how to add the receiver - |
1 | 1733 |
retry the operation by coercing to higher generality" |
1734 |
||
1735 |
^ aFloat retry:#+ coercing:self |
|
21797 | 1736 |
|
1737 |
"Modified (comment): / 12-06-2017 / 20:27:47 / cg" |
|
1 | 1738 |
! |
1739 |
||
1740 |
sumFromFraction:aFraction |
|
21797 | 1741 |
"aFraction does not know how to add the receiver - |
1 | 1742 |
retry the operation by coercing to higher generality" |
1743 |
||
1744 |
^ aFraction retry:#+ coercing:self |
|
21797 | 1745 |
|
1746 |
"Modified (comment): / 12-06-2017 / 20:27:55 / cg" |
|
1 | 1747 |
! |
1748 |
||
701 | 1749 |
sumFromInteger:anInteger |
21797 | 1750 |
"anInteger does not know how to add the receiver - |
1 | 1751 |
retry the operation by coercing to higher generality" |
1752 |
||
701 | 1753 |
^ anInteger retry:#+ coercing:self |
21797 | 1754 |
|
1755 |
"Modified (comment): / 12-06-2017 / 20:28:03 / cg" |
|
1200 | 1756 |
! |
1757 |
||
7440 | 1758 |
sumFromLargeFloat:aLargeFloat |
21797 | 1759 |
"aLargeFloat does not know how to add the receiver - |
7440 | 1760 |
retry the operation by coercing to higher generality" |
1761 |
||
1762 |
^ aLargeFloat retry:#+ coercing:self |
|
1763 |
||
21797 | 1764 |
"Created: / 17-04-1996 / 12:34:10 / cg" |
1765 |
"Modified (comment): / 12-06-2017 / 20:28:11 / cg" |
|
7440 | 1766 |
! |
1767 |
||
7399 | 1768 |
sumFromLongFloat:aLongFloat |
21797 | 1769 |
"aLongFloat does not know how to add the receiver - |
7399 | 1770 |
retry the operation by coercing to higher generality" |
1771 |
||
1772 |
^ aLongFloat retry:#+ coercing:self |
|
1773 |
||
21797 | 1774 |
"Created: / 17-04-1996 / 12:34:10 / cg" |
1775 |
"Modified (comment): / 12-06-2017 / 20:28:19 / cg" |
|
7399 | 1776 |
! |
1777 |
||
24278 | 1778 |
sumFromQuadFloat:aQuadFloat |
1779 |
"aQuadFloat does not know how to add the receiver - |
|
1780 |
retry the operation by coercing to higher generality" |
|
1781 |
||
1782 |
^ aQuadFloat retry:#+ coercing:self |
|
1783 |
||
1784 |
"Created: / 07-06-2019 / 02:26:36 / Claus Gittinger" |
|
1785 |
! |
|
1786 |
||
1200 | 1787 |
sumFromShortFloat:aShortFloat |
21797 | 1788 |
"aShortFloat does not know how to add the receiver - |
1200 | 1789 |
retry the operation by coercing to higher generality" |
1790 |
||
1791 |
^ aShortFloat retry:#+ coercing:self |
|
1792 |
||
21797 | 1793 |
"Created: / 17-04-1996 / 12:34:10 / cg" |
1794 |
"Modified (comment): / 12-06-2017 / 20:28:39 / cg" |
|
1 | 1795 |
! ! |
1796 |
||
7384 | 1797 |
!ArithmeticValue methodsFor:'mathematical functions'! |
1 | 1798 |
|
10326 | 1799 |
** aNumber |
9443 | 1800 |
"Answer the receiver raised to the power of the argument, aNumber." |
1801 |
"same as Number>>raisedTo:" |
|
1802 |
||
1803 |
^self raisedTo: aNumber |
|
1804 |
||
1805 |
"Created: / 15-07-2006 / 15:11:05 / cg" |
|
1806 |
"Modified: / 17-07-2006 / 12:51:33 / cg" |
|
1807 |
! |
|
1808 |
||
18860 | 1809 |
basicRaisedToInteger:exp |
21804 | 1810 |
"return the receiver raised to exp. |
1811 |
Warning: if the receiver is a float/double, |
|
1812 |
currently INF may be returned on overflow. |
|
1813 |
This may be changed silently to raise an error in future versions." |
|
18860 | 1814 |
|
1815 |
|result e t| |
|
1816 |
||
1817 |
"use the addition chaining algorithm, |
|
1818 |
which is much faster for big exponents" |
|
6858 | 1819 |
|
18860 | 1820 |
result := 1. |
1821 |
t := self. |
|
24956 | 1822 |
(e := exp) < 0 ifTrue:[ |
18860 | 1823 |
e := exp negated. |
1824 |
]. |
|
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1825 |
|
18860 | 1826 |
[e ~~ 0] whileTrue:[ |
1827 |
[(e bitAnd:1) == 0] whileTrue:[ |
|
1828 |
e := e bitShift:-1. |
|
1829 |
t := t * t. |
|
1830 |
]. |
|
1831 |
e := e - 1. |
|
1832 |
result := result * t. |
|
1833 |
]. |
|
1834 |
||
1835 |
(exp < 0) ifTrue:[ |
|
1836 |
^ 1 / result |
|
1837 |
]. |
|
1838 |
||
1839 |
^ result |
|
18835 | 1840 |
|
1841 |
" |
|
24963 | 1842 |
(2 raisedToInteger:216) -> 105312291668557186697918027683670432318895095400549111254310977536 |
1843 |
(2.0 raisedToInteger:216) -> 1.05312291668557E+65 |
|
1844 |
(2.0 asLongFloat) raisedToInteger:216 -> 1.053122916685571867E+65 |
|
1845 |
(2.0 asShortFloat) raisedToInteger:216 -> inf |
|
24956 | 1846 |
(2.0 asQDouble) raisedToInteger:216 |
1847 |
||
18835 | 1848 |
(2 raisedTo:216) |
1849 |
-> 105312291668557186697918027683670432318895095400549111254310977536 |
|
24956 | 1850 |
(2.0 raisedToInteger:216) asInteger - (2 raisedToInteger:216) |
1851 |
(2.0 raisedToInteger:400) asInteger - (2 raisedToInteger:400) |
|
1852 |
(2.0 raisedToInteger:500) asInteger - (2 raisedToInteger:500) |
|
1853 |
(2.0 raisedToInteger:1000) asInteger - (2 raisedToInteger:1000) |
|
18835 | 1854 |
|
1855 |
(2 raisedToInteger:216) asFloat |
|
1856 |
(2 raisedTo:216) asFloat |
|
1857 |
-> 1.05312E+65 |
|
1858 |
||
1859 |
(2 raisedToInteger:500) |
|
1860 |
(2 raisedTo:500) |
|
1861 |
-> 3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376 |
|
18860 | 1862 |
(2 raisedTo:-500) |
1863 |
-> (1/3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376) |
|
18835 | 1864 |
2 raisedToInteger:10 |
1865 |
-> 1024 |
|
1866 |
-2 raisedToInteger:10 |
|
1867 |
-> 1024 |
|
1868 |
-2 raisedToInteger:9 |
|
1869 |
-> -512 |
|
1870 |
10 raisedToInteger:-10 |
|
1871 |
-> (1/10000000000) |
|
1872 |
2 raisedToInteger:0 |
|
1873 |
-> 1 |
|
1874 |
2 raisedToInteger:-1 |
|
1875 |
-> (1/2) |
|
1876 |
||
1877 |
Time millisecondsToRun:[ |
|
1878 |
10000 timesRepeat:[ |
|
1879 |
(2 raisedToInteger:500) |
|
1880 |
] |
|
1881 |
] |
|
1882 |
||
1883 |
Time millisecondsToRun:[ |
|
1884 |
|bigNum| |
|
1885 |
bigNum := 2 raisedToInteger:500. |
|
1886 |
10 timesRepeat:[ |
|
1887 |
(bigNum raisedToInteger:500) |
|
1888 |
] |
|
1889 |
] |
|
1890 |
" |
|
1891 |
||
21804 | 1892 |
"Created: / 27-04-1999 / 15:19:22 / stefan" |
1893 |
"Modified (comment): / 13-06-2017 / 17:35:41 / cg" |
|
18835 | 1894 |
! |
1895 |
||
18860 | 1896 |
raisedTo: aNumber |
21935 | 1897 |
"return the receiver raised to aNumber (i.e. self ^ aNumber)" |
1898 |
||
18860 | 1899 |
aNumber isInteger ifTrue:[ |
21935 | 1900 |
^ self raisedToInteger:aNumber |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1901 |
]. |
23158 | 1902 |
self raisedFromNumber:aNumber. "/ this raises an error. |
21935 | 1903 |
|
1904 |
" |
|
1905 |
2 raisedTo:16 |
|
1906 |
2.0 raisedTo:16 |
|
1907 |
||
1908 |
3 raisedTo:4 |
|
23158 | 1909 |
10@10 raisedTo:4 |
1910 |
10@10 raisedTo:4.0 |
|
21935 | 1911 |
" |
1912 |
||
1913 |
"Modified (format): / 01-07-2017 / 20:59:35 / cg" |
|
18860 | 1914 |
! |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1915 |
|
18860 | 1916 |
raisedToInteger:exp |
1917 |
"return the receiver raised to exp. |
|
21804 | 1918 |
Warning: if the receiver is a float/double, |
1919 |
currently INF may be returned on overflow. |
|
1920 |
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
|
1921 |
|
18860 | 1922 |
^ self basicRaisedToInteger:exp |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1923 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1924 |
" |
18860 | 1925 |
(2.0 raisedToInteger:10000) |
1926 |
(2 raisedToInteger:10000) |
|
1927 |
(2 raisedToInteger:-10000) |
|
1928 |
||
17662 | 1929 |
(2.0 raisedToInteger:216) |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1930 |
(2 raisedToInteger:216) |
10326 | 1931 |
(2 raisedTo:216) |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1932 |
-> 105312291668557186697918027683670432318895095400549111254310977536 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1933 |
|
10326 | 1934 |
(2 raisedToInteger:216) asFloat |
1935 |
(2 raisedTo:216) asFloat |
|
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1936 |
-> 1.05312E+65 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1937 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1938 |
(2 raisedToInteger:500) |
10326 | 1939 |
(2 raisedTo:500) |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1940 |
-> 3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376 |
10326 | 1941 |
2 raisedToInteger:10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1942 |
-> 1024 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1943 |
-2 raisedToInteger:10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1944 |
-> 1024 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1945 |
-2 raisedToInteger:9 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1946 |
-> -512 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1947 |
10 raisedToInteger:-10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1948 |
-> (1/10000000000) |
10326 | 1949 |
2 raisedToInteger:0 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1950 |
-> 1 |
10326 | 1951 |
2 raisedToInteger:-1 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1952 |
-> (1/2) |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1953 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1954 |
Time millisecondsToRun:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1955 |
10000 timesRepeat:[ |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1956 |
(2 raisedToInteger:500) |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1957 |
] |
10326 | 1958 |
] |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1959 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1960 |
Time millisecondsToRun:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1961 |
|bigNum| |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1962 |
bigNum := 2 raisedToInteger:500. |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1963 |
10 timesRepeat:[ |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1964 |
(bigNum raisedToInteger:500) |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1965 |
] |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1966 |
] |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1967 |
" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1968 |
|
21804 | 1969 |
"Created: / 27-04-1999 / 15:19:22 / stefan" |
1970 |
"Modified (comment): / 13-06-2017 / 17:35:20 / cg" |
|
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1971 |
! |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1972 |
|
701 | 1973 |
squared |
1974 |
"return receiver * receiver" |
|
1975 |
||
1976 |
^ self * self |
|
1977 |
! ! |
|
1978 |
||
1979 |
!ArithmeticValue methodsFor:'queries'! |
|
1980 |
||
1981 |
respondsToArithmetic |
|
1982 |
"return true, if the receiver responds to arithmetic messages" |
|
1983 |
||
1984 |
^ true |
|
1985 |
! ! |
|
1986 |
||
1987 |
!ArithmeticValue methodsFor:'testing'! |
|
1988 |
||
1989 |
denominator |
|
1990 |
"return the denominator of the receiver" |
|
1991 |
||
1992 |
^ 1 |
|
1993 |
! |
|
1994 |
||
1995 |
even |
|
17223 | 1996 |
"return true if the receiver is divisible by 2. |
21967 | 1997 |
This is only defined for whole-numbers (integers)." |
701 | 1998 |
|
17223 | 1999 |
^ self class |
21967 | 2000 |
raise:#domainErrorSignal |
17223 | 2001 |
receiver:self |
2002 |
selector:#even |
|
2003 |
arguments:#() |
|
2004 |
errorString:'Receiver must be a whole-number' |
|
2005 |
||
21967 | 2006 |
"/ No no no, the following is bad: |
2007 |
"/ ^ self truncated asInteger even |
|
17223 | 2008 |
|
2009 |
" |
|
21967 | 2010 |
2.4 even |
2011 |
2.0 even |
|
17223 | 2012 |
" |
21967 | 2013 |
|
2014 |
"Modified (comment): / 03-07-2017 / 14:10:01 / cg" |
|
701 | 2015 |
! |
2016 |
||
3635 | 2017 |
isComplex |
7355 | 2018 |
"Answer whether the receiver has an imaginary part |
2019 |
(i.e. if it is a complex number). Always false here." |
|
3635 | 2020 |
|
2021 |
^ false |
|
2022 |
||
2023 |
"Modified: / 9.7.1998 / 10:19:27 / cg" |
|
2024 |
! |
|
2025 |
||
8979
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
2026 |
isFinite |
24711 | 2027 |
"return true, if the receiver is finite (not NaN and not +/-INF) |
9400 | 2028 |
i.e. it can be represented as a rational number." |
2029 |
||
8979
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
2030 |
^ true |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
2031 |
! |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
2032 |
|
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
2033 |
isInfinite |
24711 | 2034 |
"return true, if the receiver is an infinite number (+Inf or -Inf)" |
2035 |
||
8979
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
2036 |
^ false |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
2037 |
! |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
2038 |
|
7440 | 2039 |
isNegativeInfinity |
2040 |
^ self negative and:[self isInfinite] |
|
2041 |
! |
|
2042 |
||
2043 |
isNegativeZero |
|
2044 |
"return false - must be redefined by subclasses which can represent a negative zero |
|
2045 |
(i.e. limitedPrecisionReal classes)" |
|
2046 |
||
2047 |
^ false |
|
2048 |
! |
|
2049 |
||
2050 |
isPositiveInfinity |
|
2051 |
^ self positive and:[self isInfinite] |
|
2052 |
! |
|
2053 |
||
6156 | 2054 |
isReal |
2055 |
"return true, if the receiver is some kind of real number (as opposed to a complex); |
|
2056 |
false is returned here - the method is only redefined in Number (and Complex)." |
|
2057 |
||
2058 |
^ false |
|
2059 |
||
2060 |
"Modified: / 6.11.2001 / 13:20:20 / cg" |
|
2061 |
"Created: / 6.11.2001 / 13:26:29 / cg" |
|
2062 |
! |
|
2063 |
||
7440 | 2064 |
isZero |
2065 |
"return false - must be redefined by subclasses which can represent a negative zero |
|
2066 |
(i.e. limitedPrecisionReal classes)" |
|
701 | 2067 |
|
7440 | 2068 |
^ self = self class zero |
2069 |
! |
|
2070 |
||
2071 |
negative |
|
18860 | 2072 |
"return true if the receiver is less than zero." |
7440 | 2073 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
2074 |
^ self < self class zero |
701 | 2075 |
! |
2076 |
||
2077 |
numerator |
|
2078 |
"return the numerator of the receiver." |
|
2079 |
||
2080 |
^ self |
|
2081 |
! |
|
2082 |
||
2083 |
odd |
|
2084 |
"return true if the receiver is not divisible by 2" |
|
2085 |
||
2086 |
^ self even not |
|
2087 |
! |
|
2088 |
||
2089 |
positive |
|
18860 | 2090 |
"return true, if the receiver is greater or equal to zero (not negative)" |
701 | 2091 |
|
2092 |
^ self negative not |
|
2093 |
! |
|
2094 |
||
2095 |
sign |
|
7471 | 2096 |
"return the sign of the receiver (-1, 0 or 1)" |
701 | 2097 |
|
7440 | 2098 |
|zero| |
2099 |
||
2100 |
zero := self class zero. |
|
2101 |
(self < zero) ifTrue:[^ -1]. |
|
2102 |
(zero < self) ifTrue:[^ 1]. |
|
701 | 2103 |
^ 0 |
2104 |
! |
|
2105 |
||
2106 |
strictlyPositive |
|
18860 | 2107 |
"return true, if the receiver is greater than zero" |
701 | 2108 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
2109 |
^ self class zero < self |
1 | 2110 |
! ! |
2111 |
||
701 | 2112 |
!ArithmeticValue methodsFor:'truncation & rounding'! |
1 | 2113 |
|
701 | 2114 |
ceiling |
2115 |
"return the integer nearest the receiver towards positive infinity." |
|
1 | 2116 |
|
701 | 2117 |
|anInteger| |
1 | 2118 |
|
701 | 2119 |
anInteger := self // 1. "truncates towards negative infinity" |
2120 |
anInteger = self ifTrue:[^ anInteger]. |
|
2121 |
^ anInteger + 1 |
|
1 | 2122 |
! |
2123 |
||
701 | 2124 |
floor |
2125 |
"return the receiver truncated towards negative infinity" |
|
1 | 2126 |
|
701 | 2127 |
^ self // 1 |
1 | 2128 |
! |
2129 |
||
701 | 2130 |
roundTo:aNumber |
2131 |
"return the receiver rounded to multiples of aNumber" |
|
1 | 2132 |
|
701 | 2133 |
^ (self / aNumber) rounded * aNumber |
8449 | 2134 |
|
2135 |
" |
|
10326 | 2136 |
0 roundTo:4 |
8449 | 2137 |
1 roundTo:4 |
2138 |
2 roundTo:4 |
|
2139 |
3 roundTo:4 |
|
2140 |
4 roundTo:4 |
|
2141 |
5 roundTo:4 |
|
2142 |
6 roundTo:4 |
|
2143 |
7 roundTo:4 |
|
14497 | 2144 |
|
24861 | 2145 |
1.15 roundTo:0.1 |
2146 |
1.14 roundTo:0.1 |
|
2147 |
||
2148 |
7.15 roundTo:0.1 |
|
2149 |
7.149 roundTo:0.1 |
|
2150 |
||
14497 | 2151 |
7.123 roundTo:0.1 |
2152 |
7.523 roundTo:0.1 |
|
2153 |
7.583 roundTo:0.1 |
|
2154 |
7.623 roundTo:0.1 |
|
2155 |
7.623 roundTo:0.01 |
|
2156 |
7.628 roundTo:0.01 |
|
8449 | 2157 |
" |
2158 |
! |
|
2159 |
||
2160 |
roundUpTo:aNumber |
|
2161 |
"return the receiver rounded up to the next multiple of aNumber" |
|
2162 |
||
2163 |
^ (self / aNumber) ceiling * aNumber |
|
2164 |
||
2165 |
" |
|
10326 | 2166 |
0 roundUpTo:4 |
2167 |
1 roundUpTo:4 |
|
2168 |
2 roundUpTo:4 |
|
2169 |
3 roundUpTo:4 |
|
2170 |
4 roundUpTo:4 |
|
2171 |
5 roundUpTo:4 |
|
2172 |
6 roundUpTo:4 |
|
2173 |
7 roundUpTo:4 |
|
2174 |
8 roundUpTo:4 |
|
10911 | 2175 |
(3@4) roundUpTo:8 |
2176 |
(3@4) roundUpTo:(5 @ 4) |
|
2177 |
(3@3) roundUpTo:(5 @ 4) |
|
8449 | 2178 |
" |
701 | 2179 |
! |
2180 |
||
2181 |
rounded |
|
2182 |
"return the integer nearest the receiver" |
|
2183 |
||
1881 | 2184 |
self negative ifTrue:[ |
10326 | 2185 |
^ (self - 0.5) ceiling |
1881 | 2186 |
]. |
701 | 2187 |
^ (self + 0.5) floor |
1881 | 2188 |
|
2189 |
"Modified: 5.11.1996 / 11:31:59 / cg" |
|
1 | 2190 |
! |
2191 |
||
701 | 2192 |
truncateTo:aNumber |
2193 |
"return the receiver truncated to multiples of aNumber" |
|
1 | 2194 |
|
19056 | 2195 |
|t| |
2196 |
||
24274 | 2197 |
aNumber <= 0 ifTrue:[ArgumentError raiseErrorString:'argument must be positive']. |
19056 | 2198 |
|
2199 |
t := (self / aNumber) floor * aNumber. |
|
2200 |
(aNumber isInteger) ifTrue:[ |
|
2201 |
^ t asInteger |
|
2202 |
]. |
|
2203 |
^ t |
|
2204 |
||
2205 |
" |
|
2206 |
truncate to multiples of 4 |
|
2207 |
123.456 truncateTo:4 |
|
2208 |
124.456 truncateTo:4 |
|
2209 |
truncate to multiples of 2 |
|
2210 |
122.456 truncateTo:2 |
|
2211 |
123.456 truncateTo:2 |
|
2212 |
124.456 truncateTo:2 |
|
2213 |
normal truncate |
|
2214 |
123.456 truncateTo:1 |
|
2215 |
124.456 truncateTo:1 |
|
2216 |
truncate to decimal digits |
|
2217 |
123.456 truncateTo:0.1 |
|
2218 |
123.987 truncateTo:0.1 |
|
2219 |
123.456 truncateTo:0.01 |
|
2220 |
" |
|
24274 | 2221 |
|
2222 |
"Modified: / 06-06-2019 / 23:27:55 / Claus Gittinger" |
|
1 | 2223 |
! |
2224 |
||
701 | 2225 |
truncated |
2226 |
"return the receiver truncated towards zero" |
|
1 | 2227 |
|
701 | 2228 |
self negative ifTrue:[ |
2229 |
^ self ceiling |
|
2230 |
]. |
|
2231 |
^ self floor |
|
2232 |
! ! |
|
1 | 2233 |
|
701 | 2234 |
!ArithmeticValue class methodsFor:'documentation'! |
2235 |
||
2236 |
version |
|
18835 | 2237 |
^ '$Header$' |
12576 | 2238 |
! |
2239 |
||
2240 |
version_CVS |
|
18835 | 2241 |
^ '$Header$' |
1 | 2242 |
! ! |
6858 | 2243 |
|
14747 | 2244 |
|
701 | 2245 |
ArithmeticValue initialize! |