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