author | Claus Gittinger <cg@exept.de> |
Sat, 23 Nov 2019 17:01:52 +0100 | |
changeset 24956 | dc04e2668278 |
parent 24861 | 68e1f568f097 |
child 24963 | 3f40782dfac4 |
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 |
||
701 | 521 |
!ArithmeticValue methodsFor:'arithmetic'! |
522 |
||
523 |
* something |
|
11735 | 524 |
"return the product of the receiver and the argument." |
701 | 525 |
|
526 |
^ self subclassResponsibility |
|
527 |
! |
|
528 |
||
529 |
+ something |
|
530 |
"return the sum of the receiver and the argument" |
|
531 |
||
532 |
^ self subclassResponsibility |
|
533 |
! |
|
534 |
||
535 |
- something |
|
536 |
"return the difference of the receiver and the argument" |
|
537 |
||
538 |
^ self subclassResponsibility |
|
539 |
! |
|
540 |
||
541 |
/ something |
|
542 |
"return the quotient of the receiver and the argument" |
|
1 | 543 |
|
701 | 544 |
^ self subclassResponsibility |
545 |
! |
|
546 |
||
22358 | 547 |
// aNumber |
1881 | 548 |
"return the integer quotient of dividing the receiver by aNumber with |
20337 | 549 |
truncation towards negative infinity. |
22358 | 550 |
|
20338 | 551 |
Please be aware of the effect of truncation on negative receivers, |
20337 | 552 |
and understand the difference between '//' vs. 'quo:' |
553 |
and the corresponding '\\' vs. 'rem:'" |
|
701 | 554 |
|
22358 | 555 |
"/ Note: '^ (self / aNumber) floor' may lead to infinite recursion |
1172 | 556 |
|
557 |
^ self subclassResponsibility |
|
558 |
||
22358 | 559 |
"Modified: / 05-11-1996 / 11:46:27 / cg" |
560 |
"Modified: / 12-02-1998 / 16:31:36 / stefan" |
|
561 |
"Modified (comment): / 16-11-2017 / 11:26:13 / mawalch" |
|
701 | 562 |
! |
563 |
||
564 |
\\ something |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
565 |
"return the receiver modulo something. |
17038 | 566 |
The remainder has the same sign as the argument, something. |
6067 | 567 |
The following is always true: |
17038 | 568 |
(receiver // something) * something + (receiver \\ something) = receiver |
20338 | 569 |
|
570 |
Please be aware of the effect of truncation on negative receivers, |
|
571 |
and understand the difference between '//' vs. 'quo:' |
|
572 |
and the corresponding '\\' vs. 'rem:'." |
|
1 | 573 |
|
701 | 574 |
^ self - ((self // something) * something) |
3134 | 575 |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
576 |
" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
577 |
0.9 \\ 0.4 |
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 |
" |
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 |
"Modified: / 12.2.1998 / 19:14:37 / stefan" |
701 | 584 |
! |
585 |
||
586 |
abs |
|
587 |
"return the absolute value of the receiver" |
|
588 |
||
589 |
(self negative) ifTrue:[^ self negated]. |
|
590 |
^ self |
|
591 |
! |
|
592 |
||
22723 | 593 |
copySignTo:aNumber |
594 |
"Return a number with same magnitude as aNumber and same sign as self" |
|
595 |
||
596 |
(self negative == aNumber negative) ifTrue:[^ aNumber]. |
|
597 |
^ aNumber negated |
|
598 |
||
599 |
" |
|
600 |
-15 copySignTo:1234 -> -1234 |
|
601 |
-15 copySignTo:-1234 -> -1234 |
|
602 |
15 copySignTo:1234 -> 1234 |
|
603 |
15 copySignTo:-1234 -> 1234 |
|
604 |
1.0 copySignTo:0.0 -> 0.0 |
|
605 |
-1.0 copySignTo:0.0 -> -0.0 |
|
606 |
1 copySignTo:(1/3) -> (1/3) |
|
607 |
-1 copySignTo:(1/3) -> (-1/3) |
|
608 |
" |
|
609 |
! |
|
610 |
||
10326 | 611 |
dist:arg |
20696 | 612 |
"return the distance between the arg and the receiver." |
8757 | 613 |
|
614 |
^ (arg - self) abs |
|
615 |
||
616 |
" |
|
10326 | 617 |
(1%1) dist:(0%0) |
618 |
(1@1) dist:(0@0) |
|
619 |
(1) dist:(0) |
|
8757 | 620 |
" |
621 |
! |
|
622 |
||
16121 | 623 |
modulusOf:aNumber |
20353 | 624 |
"return aNumber modulo the receiver. |
16121 | 625 |
The remainder has the same sign as something. |
626 |
Defined for protocol compatibility with ModuloNumber." |
|
627 |
||
628 |
^ aNumber \\ self |
|
629 |
! |
|
630 |
||
701 | 631 |
negated |
632 |
"return the receiver negated" |
|
633 |
||
634 |
^ self class zero - self |
|
1 | 635 |
! |
636 |
||
701 | 637 |
quo:something |
638 |
"Return the integer quotient of dividing the receiver by the argument |
|
6067 | 639 |
with truncation towards zero. |
20338 | 640 |
|
641 |
Please be aware of the effect of truncation on negative receivers, |
|
642 |
and understand the difference between '//' vs. 'quo:' |
|
643 |
and the corresponding '\\' vs. 'rem:'. |
|
644 |
||
6067 | 645 |
The following is always true: |
20337 | 646 |
(receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver |
647 |
For positive results, this is the same as #//, |
|
648 |
for negative results, the remainder is ignored. |
|
649 |
I.e.: '9 // 4 = 2' and '-9 // 4 = -3' |
|
650 |
in contrast: '9 quo: 4 = 2' and '-9 quo: 4 = -2'" |
|
701 | 651 |
|
652 |
^ (self / something) truncated |
|
653 |
! |
|
654 |
||
655 |
reciprocal |
|
18835 | 656 |
"return the receiver's reciprocal" |
701 | 657 |
|
658 |
^ self class unity / self |
|
22731 | 659 |
|
660 |
" |
|
661 |
(10 + 4i) class unity -> (1+0i) |
|
662 |
(10 + 4i) reciprocal -> ((5/58)-(1/29)i) |
|
663 |
(4/3) reciprocal -> (3/4) |
|
664 |
3 reciprocal -> (1/3) |
|
665 |
3.0 reciprocal -> 0.333333333333333 |
|
666 |
3.0 asLongFloat reciprocal -> 0.3333333333333333333 |
|
667 |
3.0 asShortFloat reciprocal -> 0.3333333 |
|
668 |
" |
|
701 | 669 |
! |
1 | 670 |
|
701 | 671 |
rem:something |
672 |
"Return the integer remainder of dividing the receiver by the argument |
|
673 |
with truncation towards zero. |
|
6067 | 674 |
The remainder has the same sign as the receiver. |
675 |
The following is always true: |
|
20338 | 676 |
(receiver quo: something) * something + (receiver rem: something) = receiver |
677 |
||
678 |
Please be aware of the effect of truncation on negative receivers, |
|
679 |
and understand the difference between '//' vs. 'quo:' |
|
680 |
and the corresponding '\\' vs. 'rem:'." |
|
701 | 681 |
|
682 |
^ self - ((self quo:something) * something) |
|
7440 | 683 |
! |
684 |
||
685 |
uncheckedDivide:aNumber |
|
686 |
"return the quotient of the receiver and the argument, aNumber. |
|
7471 | 687 |
Do not check for divide by zero (return NaN or Infinity). |
7440 | 688 |
This operation is provided for emulators of other languages/semantics, |
689 |
where no exception is raised for these results (i.e. Java). |
|
18835 | 690 |
It is only defined if the argument's type is the same as the receiver's." |
7440 | 691 |
|
692 |
aNumber isZero ifTrue:[ |
|
21831 | 693 |
"/ protect against NaN and negativeInfinity trying to use me... |
23158 | 694 |
thisContext isRecursive ifTrue:[ |
695 |
self error:'recursive #uncheckedDivide:'. |
|
696 |
]. |
|
21831 | 697 |
|
18835 | 698 |
self isZero ifTrue:[^ self class NaN]. |
699 |
self negative ifTrue:[^ self class negativeInfinity]. |
|
24301 | 700 |
^ self class positiveInfinity. |
7440 | 701 |
]. |
702 |
^ self / aNumber |
|
21831 | 703 |
|
704 |
"Modified (format): / 18-06-2017 / 23:43:13 / cg" |
|
24301 | 705 |
"Modified: / 09-06-2019 / 12:53:47 / Claus Gittinger" |
701 | 706 |
! ! |
707 |
||
12578 | 708 |
!ArithmeticValue methodsFor:'arithmetic destructive'! |
709 |
||
710 |
*= aNumber |
|
711 |
"Return the product of self multiplied by aNumber. |
|
712 |
The receiver MAY, but NEED NOT be changed to contain the product. |
|
713 |
So this method must be used as: 'a := a *= 5'. |
|
714 |
This method can be redefined for constructed datatypes to do optimisations" |
|
715 |
||
716 |
^ self * aNumber |
|
717 |
||
718 |
"Created: / 28.4.1999 / 11:46:11 / stefan" |
|
719 |
"Modified: / 28.4.1999 / 11:53:28 / stefan" |
|
720 |
! |
|
721 |
||
722 |
+= aNumber |
|
723 |
"Return the sum of self and aNumber. |
|
724 |
The receiver MAY, but NEED NOT be changed to contain the sum. |
|
725 |
So this method must be used as: 'a := a += 5'. |
|
726 |
This method can be redefined for constructed datatypes to do optimisations" |
|
727 |
||
728 |
^ self + aNumber |
|
729 |
||
730 |
"Created: / 28.4.1999 / 10:13:41 / stefan" |
|
731 |
"Modified: / 28.4.1999 / 11:54:11 / stefan" |
|
732 |
! |
|
733 |
||
734 |
-= aNumber |
|
735 |
"Return the difference of self and aNumber. |
|
736 |
The receiver MAY, but NEED NOT be changed to contain the difference. |
|
737 |
So this method must be used as: 'a := a -= 5'. |
|
738 |
This method can be redefined for constructed datatypes to do optimisations" |
|
739 |
||
740 |
^ self - aNumber |
|
741 |
||
742 |
"Created: / 28.4.1999 / 10:13:58 / stefan" |
|
743 |
"Modified: / 28.4.1999 / 11:54:37 / stefan" |
|
744 |
! |
|
745 |
||
746 |
/= aNumber |
|
747 |
"Return the quotient of self and aNumber. |
|
748 |
The receiver MAY, but NEED NOT be changed to contain the quotient. |
|
749 |
So this method must be used as: 'a := a /= 5'. |
|
750 |
This method can be redefined for constructed datatypes to do optimisations" |
|
751 |
||
752 |
^ self / aNumber |
|
753 |
||
754 |
"Created: / 28.4.1999 / 11:46:22 / stefan" |
|
755 |
"Modified: / 28.4.1999 / 11:55:06 / stefan" |
|
756 |
! |
|
757 |
||
758 |
div2 |
|
759 |
"Return the quotient of self divided by 2. |
|
760 |
The receiver MAY, but NEED NOT be changed to contain the result. |
|
761 |
So this method must be used as: 'a := a div2. |
|
762 |
This method can be redefined for constructed datatypes to do optimisations" |
|
763 |
||
764 |
^ self // 2 |
|
765 |
||
766 |
"Created: / 28.4.1999 / 10:12:44 / stefan" |
|
767 |
"Modified: / 28.4.1999 / 11:56:09 / stefan" |
|
768 |
! |
|
769 |
||
770 |
mul2 |
|
771 |
"Return the product of self multiplied by 2. |
|
772 |
The receiver MAY, but NEED NOT be changed to contain the result. |
|
15279 | 773 |
So this method must be used as: a := a mul2. |
12578 | 774 |
This method can be redefined for constructed datatypes to do optimisations" |
775 |
||
776 |
^ self * 2 |
|
777 |
||
778 |
"Created: / 28.4.1999 / 10:12:55 / stefan" |
|
779 |
"Modified: / 28.4.1999 / 11:56:38 / stefan" |
|
780 |
! ! |
|
781 |
||
5238 | 782 |
!ArithmeticValue methodsFor:'coercing & converting'! |
1888 | 783 |
|
784 |
coerce:aNumber |
|
18835 | 785 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
1888 | 786 |
|
21816 | 787 |
^ self class coerce:aNumber |
788 |
||
789 |
"Modified: / 15-06-2017 / 10:27:03 / cg" |
|
1888 | 790 |
! |
791 |
||
792 |
generality |
|
18835 | 793 |
"return a number giving the receiver's generality. |
794 |
That number is used to convert one of the arguments in a mixed expression. |
|
1888 | 795 |
The generality has to be defined in subclasses, |
10326 | 796 |
such that gen(a) > gen(b) iff, conversion of b into a's class |
18835 | 797 |
does not cut precision. |
798 |
For example, Integer has 40, Float has 80, meaning that if we convert a Float to an Integer, |
|
799 |
some precision may be lost. |
|
800 |
The generality is used by ArithmeticValue>>retry:coercing:, |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
801 |
which converts the lower-precision number to the higher precision |
18835 | 802 |
number's class, when mixed-type arithmetic is performed." |
10326 | 803 |
|
1888 | 804 |
^ self subclassResponsibility |
805 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
806 |
"Modified: / 5.11.1996 / 15:05:30 / cg" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
807 |
"Modified: / 13.2.1998 / 15:36:01 / stefan" |
1888 | 808 |
! |
809 |
||
810 |
retry:aSymbol coercing:aNumber |
|
811 |
"arithmetic represented by the binary operator, aSymbol, |
|
10326 | 812 |
could not be performed with the receiver and the argument, aNumber, |
813 |
because of the differences in representation. |
|
814 |
Coerce either the receiver or the argument, depending on which has higher |
|
815 |
generality, and try again. |
|
1888 | 816 |
If the operation is compare for same value (=), return false if |
10326 | 817 |
the argument is not a Number. |
1888 | 818 |
If the generalities are the same, create an error message, since this |
819 |
means that a subclass has not been fully implemented." |
|
820 |
||
821 |
|hasGenerality myGenerality otherGenerality| |
|
822 |
||
823 |
hasGenerality := aNumber respondsTo:#generality. |
|
824 |
hasGenerality ifFalse:[ |
|
20060 | 825 |
(aSymbol == #=) ifTrue:[ |
826 |
^ false |
|
827 |
]. |
|
828 |
(aSymbol == #~=) ifTrue:[ |
|
829 |
^ true |
|
830 |
]. |
|
24274 | 831 |
^ ArgumentError raiseErrorString:'retry:coercing: argument is not a number'. |
1888 | 832 |
]. |
833 |
||
834 |
myGenerality := self generality. |
|
835 |
otherGenerality := aNumber generality. |
|
836 |
(myGenerality > otherGenerality) ifTrue:[ |
|
20060 | 837 |
^ self perform:aSymbol with:(self coerce:aNumber) |
1888 | 838 |
]. |
839 |
(myGenerality < otherGenerality) ifTrue:[ |
|
20060 | 840 |
^ (aNumber coerce:self) perform:aSymbol with:aNumber |
1888 | 841 |
]. |
20060 | 842 |
"/ self error:'retry:coercing: oops - same generality; retry should not happen' |
843 |
"same generality may happen with Lazy and LazyValue" |
|
844 |
^ self perform:aSymbol with:aNumber |
|
845 |
||
846 |
" |
|
847 |
([3] lazyValue) * ([2] lazyValue) |
|
848 |
" |
|
1888 | 849 |
|
24274 | 850 |
"Modified: / 05-11-1996 / 15:03:38 / cg" |
851 |
"Modified: / 06-06-2019 / 23:27:43 / Claus Gittinger" |
|
16657 | 852 |
! |
853 |
||
854 |
retry:aSymbol coercing:aNumber with:anArgument |
|
855 |
"arithmetic represented by the binary operator, aSymbol, |
|
856 |
could not be performed with the receiver and the argument, aNumber, |
|
857 |
because of the differences in representation. |
|
858 |
Coerce either the receiver or the argument, depending on which has higher |
|
859 |
generality, and try again. |
|
860 |
If the operation is compare for same value (=), return false if |
|
861 |
the argument is not a Number. |
|
862 |
If the generalities are the same, create an error message, since this |
|
863 |
means that a subclass has not been fully implemented." |
|
864 |
||
865 |
|hasGenerality myGenerality otherGenerality| |
|
866 |
||
867 |
hasGenerality := aNumber respondsTo:#generality. |
|
868 |
hasGenerality ifFalse:[ |
|
24274 | 869 |
^ ArgumentError raiseErrorString:'retry:coercing: argument is not a number'. |
16657 | 870 |
]. |
871 |
||
872 |
myGenerality := self generality. |
|
873 |
otherGenerality := aNumber generality. |
|
874 |
(myGenerality > otherGenerality) ifTrue:[ |
|
875 |
^ self perform:aSymbol with:(self coerce:aNumber) with:anArgument |
|
876 |
]. |
|
877 |
(myGenerality < otherGenerality) ifTrue:[ |
|
878 |
^ (aNumber coerce:self) perform:aSymbol with:aNumber with:anArgument |
|
879 |
]. |
|
20060 | 880 |
"/ self error:'retry:coercing: oops - same generality; retry should not happen' |
881 |
"same generality may happen with Lazy and LazyValue" |
|
882 |
^ self perform:aSymbol with:aNumber with:anArgument |
|
883 |
||
24274 | 884 |
"Modified: / 05-11-1996 / 15:03:38 / cg" |
885 |
"Modified: / 06-06-2019 / 23:27:50 / Claus Gittinger" |
|
1888 | 886 |
! ! |
887 |
||
701 | 888 |
!ArithmeticValue methodsFor:'converting'! |
889 |
||
17222 | 890 |
as32BitIEEEFloatBytesMSB:msb |
891 |
^ self asShortFloat digitBytesMSB:msb |
|
892 |
||
893 |
" |
|
894 |
2 as32BitIEEEFloatBytesMSB:true |
|
895 |
2.0 as32BitIEEEFloatBytesMSB:true |
|
896 |
" |
|
897 |
! |
|
898 |
||
899 |
as64BitIEEEFloatBytesMSB:msb |
|
900 |
^ self asFloat digitBytesMSB:msb |
|
901 |
||
902 |
" |
|
903 |
2 as64BitIEEEFloatBytesMSB:true |
|
904 |
2.0 as64BitIEEEFloatBytesMSB:true |
|
905 |
" |
|
906 |
! |
|
907 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
908 |
asDouble |
18835 | 909 |
"ST80 compatibility: return a double with receiver's value. |
910 |
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
|
911 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
912 |
^ self asFloat |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
913 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
914 |
"Created: / 13.2.1998 / 15:40:14 / stefan" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
915 |
! |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
916 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
917 |
asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
918 |
"return the receiver as fixedPoint number. |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
919 |
Q: what should the scale be here ?" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
920 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
921 |
^ self asFraction asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
922 |
|
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 |
0.3 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
925 |
0.5 asFixedPoint |
10326 | 926 |
(1/5) asFloat asFixedPoint |
927 |
(1/3) asFloat asFixedPoint |
|
928 |
(2/3) asFloat asFixedPoint |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
929 |
(1/8) asFloat asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
930 |
3.14159 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
931 |
0.0000001 asFraction |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
932 |
0.0000001 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
933 |
" |
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 |
"Modified: / 25.10.1997 / 15:36:54 / cg" |
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 |
asFixedPoint:scale |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
939 |
"return the receiver as fixedPoint number with the given |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
940 |
number of post-decimal-digits." |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
941 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
942 |
^ self asFraction asFixedPoint:scale |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
943 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
944 |
" |
10326 | 945 |
0.3 asFixedPoint:4 |
946 |
0.3 asFixedPoint:3 |
|
947 |
0.3 asFixedPoint:2 |
|
948 |
0.3 asFixedPoint:1 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
949 |
0.3 asFixedPoint:0 |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
950 |
|
10326 | 951 |
0.5 asFixedPoint:3 |
952 |
(1/5) asFloat asFixedPoint:1 |
|
953 |
(1/8) asFloat asFixedPoint:1 |
|
954 |
1.0 asFixedPoint:2 |
|
955 |
3.14159 asFixedPoint:2 |
|
956 |
3.14159 asFixedPoint:3 |
|
957 |
(3.14159 asFixedPoint:2) asFixedPoint:5 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
958 |
" |
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 |
|
12980 | 961 |
asFixedPointRoundedToScale |
962 |
"return the receiver as fixedPoint number, rounded to its scale." |
|
963 |
||
964 |
^ self asFixedPoint roundedToScale |
|
965 |
||
966 |
" |
|
967 |
0.3 asFixedPoint |
|
968 |
0.5 asFixedPoint |
|
969 |
(2/3) asFloat asFixedPoint |
|
970 |
(1/8) asFloat asFixedPoint |
|
971 |
3.14159 asFixedPoint |
|
972 |
||
973 |
0.3 asFixedPointRoundedToScale |
|
974 |
0.5 asFixedPointRoundedToScale |
|
975 |
(2/3) asFloat asFixedPointRoundedToScale |
|
976 |
(1/8) asFloat asFixedPointRoundedToScale |
|
977 |
3.14159 asFixedPointRoundedToScale |
|
978 |
" |
|
979 |
||
980 |
"Created: / 02-08-2010 / 13:32:16 / cg" |
|
981 |
! |
|
982 |
||
983 |
asFixedPointRoundedToScale:scale |
|
984 |
"return the receiver as fixedPoint number with the given |
|
985 |
number of post-decimal-digits, rounded to its scale" |
|
986 |
||
987 |
^ (self asFixedPoint:scale) roundedToScale |
|
988 |
||
17336 | 989 |
" |
990 |
3.14159 asFixedPointRoundedToScale:1 |
|
991 |
3.14159 asFixedPointRoundedToScale:2 |
|
992 |
3.14159 asFixedPointRoundedToScale:3 |
|
993 |
3.14159 asFixedPointRoundedToScale:4 |
|
994 |
" |
|
995 |
||
12980 | 996 |
"Created: / 02-08-2010 / 13:33:11 / cg" |
997 |
! |
|
998 |
||
1 | 999 |
asFloat |
1000 |
"return a float with same value" |
|
1001 |
||
1002 |
^ self subclassResponsibility |
|
1003 |
! |
|
1004 |
||
5986 | 1005 |
asFloatD |
1006 |
"return a double precision float with same value. |
|
1007 |
Added for ANSI compatibility" |
|
1008 |
||
1009 |
^ self asFloat |
|
1010 |
||
1011 |
"Created: / 7.9.2001 / 13:36:48 / cg" |
|
1012 |
"Modified: / 7.9.2001 / 13:38:10 / cg" |
|
1013 |
! |
|
1014 |
||
1015 |
asFloatE |
|
1016 |
"return a single precision float with same value. |
|
1017 |
Added for ANSI compatibility" |
|
1018 |
||
1019 |
^ self asShortFloat |
|
1020 |
||
1021 |
"Created: / 7.9.2001 / 13:37:06 / cg" |
|
1022 |
"Modified: / 7.9.2001 / 13:38:15 / cg" |
|
1023 |
! |
|
1024 |
||
1025 |
asFloatQ |
|
1026 |
"return a quad precision float with same value. |
|
21879 | 1027 |
Notice that longFloats as returned here may or may not provide more |
21940 | 1028 |
precision than a double - depending on the machine's CPU |
1029 |
(and usually do not provide quad the number of bits of a float) |
|
5986 | 1030 |
Added for ANSI compatibility" |
1031 |
||
1032 |
^ self asLongFloat |
|
1033 |
||
21879 | 1034 |
"Created: / 07-09-2001 / 13:38:44 / cg" |
21940 | 1035 |
"Modified (comment): / 03-07-2017 / 10:19:13 / cg" |
1036 |
! |
|
1037 |
||
1038 |
asFloatQD |
|
1039 |
"return a quad double precision float with same value." |
|
1040 |
||
1041 |
^ self asQDouble |
|
1042 |
||
1043 |
"Created: / 03-07-2017 / 10:19:41 / cg" |
|
5986 | 1044 |
! |
1045 |
||
1 | 1046 |
asFraction |
1047 |
"return a fraction with same value" |
|
1048 |
||
1049 |
^ self subclassResponsibility |
|
1050 |
! |
|
1051 |
||
701 | 1052 |
asInteger |
1053 |
"return an integer with same value - might truncate" |
|
1054 |
||
1055 |
^ self truncated |
|
1056 |
! |
|
1057 |
||
24279 | 1058 |
asLargeFloat |
1059 |
"return a largeFloat with same value" |
|
1060 |
||
1061 |
"WARNING: could loose precision here, if not redefined in concrete classes which |
|
1062 |
have more than float precision (i.e. LargeIntegers and Fractions)" |
|
1063 |
||
1064 |
^ self asFloat asLargeFloat "/ subclassResponsibility |
|
1065 |
||
1066 |
"Created: / 07-06-2019 / 02:32:57 / Claus Gittinger" |
|
1067 |
! |
|
1068 |
||
7378 | 1069 |
asLimitedPrecisionReal |
1070 |
"return a float of any precision with same value" |
|
1071 |
||
1072 |
^ self asFloat |
|
1073 |
! |
|
1074 |
||
5986 | 1075 |
asLongFloat |
1076 |
"return a longFloat with same value" |
|
1077 |
||
10315 | 1078 |
"WARNING: could loose precision here, if not redefined in concrete classes which |
1079 |
have more than float precision (i.e. LargeIntegers and Fractions)" |
|
7355 | 1080 |
|
10315 | 1081 |
^ self asFloat asLongFloat "/ subclassResponsibility |
5986 | 1082 |
|
1083 |
"Modified: / 17.4.1996 / 12:21:35 / cg" |
|
1084 |
"Created: / 7.9.2001 / 13:39:31 / cg" |
|
1085 |
! |
|
1086 |
||
24279 | 1087 |
asQuadFloat |
1088 |
"return a quadFloat with same value" |
|
1089 |
||
1090 |
"WARNING: could loose precision here, if not redefined in concrete classes which |
|
1091 |
have more than float precision (i.e. LargeIntegers and Fractions)" |
|
1092 |
||
1093 |
^ self asFloat asQuadFloat "/ subclassResponsibility |
|
1094 |
||
1095 |
"Created: / 07-06-2019 / 02:28:54 / Claus Gittinger" |
|
1096 |
! |
|
1097 |
||
5987 | 1098 |
asScaledDecimal:scale |
18835 | 1099 |
"return a fixedPoint approximating the receiver's value" |
5987 | 1100 |
|
1101 |
^ self asFixedPoint:scale |
|
1102 |
||
1103 |
" |
|
1104 |
1.234 asScaledDecimal:2 |
|
1105 |
" |
|
1106 |
||
1107 |
"Created: / 7.9.2001 / 13:46:26 / cg" |
|
1108 |
"Modified: / 7.9.2001 / 13:46:44 / cg" |
|
1109 |
! |
|
1110 |
||
1200 | 1111 |
asShortFloat |
1112 |
"return a shortFloat with same value" |
|
1113 |
||
1114 |
^ self asFloat asShortFloat |
|
1115 |
||
1116 |
"Modified: 17.4.1996 / 12:21:35 / cg" |
|
1117 |
! |
|
1118 |
||
701 | 1119 |
degreesToRadians |
21106 | 1120 |
"interpreting the receiver as degrees, return the radians" |
701 | 1121 |
|
1122 |
^ self asFloat degreesToRadians |
|
1123 |
! |
|
1124 |
||
1125 |
radiansToDegrees |
|
21106 | 1126 |
"interpreting the receiver as radians, return the degrees" |
701 | 1127 |
|
1128 |
^ self asFloat radiansToDegrees |
|
1 | 1129 |
! ! |
1130 |
||
701 | 1131 |
!ArithmeticValue methodsFor:'double dispatching'! |
1 | 1132 |
|
23499 | 1133 |
bitAndFromInteger:anInteger |
1134 |
"anInteger does not know how to do bitAnd: with the receiver - |
|
1135 |
retry the operation by coercing to Integer" |
|
1136 |
||
1137 |
|selfAsInteger| |
|
1138 |
||
1139 |
selfAsInteger := self asInteger. |
|
1140 |
self ~= selfAsInteger ifTrue:[ |
|
1141 |
DomainError raiseRequestErrorString:'non-integer mask in bit-operation'. |
|
1142 |
]. |
|
1143 |
||
1144 |
^ anInteger bitAnd:selfAsInteger |
|
1145 |
||
1146 |
"Created: / 01-11-2018 / 11:49:27 / Stefan Vogel" |
|
1147 |
! |
|
1148 |
||
1149 |
bitOrFromInteger:anInteger |
|
1150 |
"anInteger does not know how to do bitOr: with the receiver - |
|
1151 |
retry the operation by coercing to Integer" |
|
1152 |
||
1153 |
|selfAsInteger| |
|
1154 |
||
1155 |
selfAsInteger := self asInteger. |
|
1156 |
self ~= selfAsInteger ifTrue:[ |
|
1157 |
DomainError raiseRequestErrorString:'non-integer mask in bit-operation'. |
|
1158 |
]. |
|
1159 |
||
1160 |
^ anInteger bitOr:selfAsInteger |
|
1161 |
||
1162 |
"Created: / 01-11-2018 / 11:49:09 / Stefan Vogel" |
|
1163 |
! |
|
1164 |
||
1165 |
bitXorFromInteger:anInteger |
|
1166 |
"anInteger does not know how to do bitXor: with the receiver - |
|
1167 |
retry the operation by coercing to Integer" |
|
1168 |
||
1169 |
|selfAsInteger| |
|
1170 |
||
1171 |
selfAsInteger := self asInteger. |
|
1172 |
self ~= selfAsInteger ifTrue:[ |
|
1173 |
DomainError raiseRequestErrorString:'non-integer mask in bit-operation'. |
|
1174 |
]. |
|
1175 |
||
1176 |
^ anInteger bitXor:selfAsInteger |
|
1177 |
||
1178 |
"Created: / 01-11-2018 / 11:58:53 / Stefan Vogel" |
|
1179 |
! |
|
1180 |
||
7355 | 1181 |
differenceFromComplex:aComplex |
21797 | 1182 |
"aComplex does not know how to subtract the receiver - |
7355 | 1183 |
retry the operation by coercing to higher generality" |
1184 |
||
1185 |
^ aComplex retry:#- coercing:self |
|
21797 | 1186 |
|
1187 |
"Modified (comment): / 12-06-2017 / 20:34:41 / cg" |
|
7355 | 1188 |
! |
1189 |
||
1888 | 1190 |
differenceFromFixedPoint:aFixedPoint |
21797 | 1191 |
"aFixedPoint does not know how to subtract the receiver - |
1888 | 1192 |
retry the operation by coercing to higher generality" |
1193 |
||
1194 |
^ aFixedPoint retry:#- coercing:self |
|
1195 |
||
21797 | 1196 |
"Created: / 05-11-1996 / 14:55:51 / cg" |
1197 |
"Modified (comment): / 12-06-2017 / 20:34:33 / cg" |
|
1888 | 1198 |
! |
1199 |
||
701 | 1200 |
differenceFromFloat:aFloat |
21797 | 1201 |
"aFloat does not know how to subtract the receiver - |
701 | 1202 |
retry the operation by coercing to higher generality" |
1 | 1203 |
|
701 | 1204 |
^ aFloat retry:#- coercing:self |
21797 | 1205 |
|
1206 |
"Modified (comment): / 12-06-2017 / 20:34:23 / cg" |
|
1 | 1207 |
! |
1208 |
||
701 | 1209 |
differenceFromFraction:aFraction |
21797 | 1210 |
"aFraction does not know how to subtract the receiver - |
701 | 1211 |
retry the operation by coercing to higher generality" |
1 | 1212 |
|
701 | 1213 |
^ aFraction retry:#- coercing:self |
21797 | 1214 |
|
1215 |
"Modified (comment): / 12-06-2017 / 20:34:16 / cg" |
|
1 | 1216 |
! |
1217 |
||
701 | 1218 |
differenceFromInteger:anInteger |
21797 | 1219 |
"anInteger does not know how to subtract the receiver - |
701 | 1220 |
retry the operation by coercing to higher generality" |
1 | 1221 |
|
701 | 1222 |
^ anInteger retry:#- coercing:self |
21797 | 1223 |
|
1224 |
"Modified (comment): / 12-06-2017 / 20:34:07 / cg" |
|
1 | 1225 |
! |
1226 |
||
7440 | 1227 |
differenceFromLargeFloat:aLargeFloat |
21797 | 1228 |
"aLargeFloat does not know how to subtract the receiver - |
7440 | 1229 |
retry the operation by coercing to higher generality" |
1230 |
||
1231 |
^ aLargeFloat retry:#- coercing:self |
|
21797 | 1232 |
|
1233 |
"Modified (comment): / 12-06-2017 / 20:34:00 / cg" |
|
7440 | 1234 |
! |
1235 |
||
7399 | 1236 |
differenceFromLongFloat:aLongFloat |
21797 | 1237 |
"aLongFloat does not know how to subtract the receiver - |
7399 | 1238 |
retry the operation by coercing to higher generality" |
1239 |
||
1240 |
^ aLongFloat retry:#- coercing:self |
|
1241 |
||
21797 | 1242 |
"Created: / 17-04-1996 / 12:33:16 / cg" |
1243 |
"Modified (comment): / 12-06-2017 / 20:33:50 / cg" |
|
7399 | 1244 |
! |
1245 |
||
24278 | 1246 |
differenceFromQuadFloat:aQuadFloat |
1247 |
"aQuadFloat does not know how to subtract the receiver - |
|
1248 |
retry the operation by coercing to higher generality" |
|
1249 |
||
1250 |
^ aQuadFloat retry:#- coercing:self |
|
1251 |
||
1252 |
"Created: / 07-06-2019 / 02:20:52 / Claus Gittinger" |
|
1253 |
! |
|
1254 |
||
1200 | 1255 |
differenceFromShortFloat:aShortFloat |
21797 | 1256 |
"aShortFloat does not know how to subtract the receiver - |
1200 | 1257 |
retry the operation by coercing to higher generality" |
1258 |
||
1259 |
^ aShortFloat retry:#- coercing:self |
|
1260 |
||
21797 | 1261 |
"Created: / 17-04-1996 / 12:33:16 / cg" |
1262 |
"Modified (comment): / 12-06-2017 / 20:33:43 / cg" |
|
1200 | 1263 |
! |
1264 |
||
7355 | 1265 |
equalFromComplex:aComplex |
21797 | 1266 |
"aComplex does not know how to compare to the receiver - |
7355 | 1267 |
retry the operation by coercing to higher generality" |
1268 |
||
1269 |
^ aComplex retry:#= coercing:self |
|
21797 | 1270 |
|
1271 |
"Modified (comment): / 12-06-2017 / 20:33:26 / cg" |
|
7355 | 1272 |
! |
1273 |
||
1274 |
equalFromFixedPoint:aFixedPoint |
|
21797 | 1275 |
"aFixedPoint does not know how to compare to the receiver - |
7355 | 1276 |
retry the operation by coercing to higher generality" |
1277 |
||
1278 |
^ aFixedPoint retry:#= coercing:self |
|
21797 | 1279 |
|
1280 |
"Modified (comment): / 12-06-2017 / 20:33:17 / cg" |
|
7355 | 1281 |
! |
1282 |
||
1283 |
equalFromFloat:aFloat |
|
21797 | 1284 |
"aFloat does not know how to compare to the receiver - |
7355 | 1285 |
retry the operation by coercing to higher generality" |
1286 |
||
1287 |
^ aFloat retry:#= coercing:self |
|
21797 | 1288 |
|
1289 |
"Modified (comment): / 12-06-2017 / 20:33:09 / cg" |
|
7355 | 1290 |
! |
1291 |
||
1292 |
equalFromFraction:aFraction |
|
21797 | 1293 |
"aFraction does not know how to compare to the receiver - |
7355 | 1294 |
retry the operation by coercing to higher generality" |
1295 |
||
1296 |
^ aFraction retry:#= coercing:self |
|
21797 | 1297 |
|
1298 |
"Modified (comment): / 12-06-2017 / 20:33:01 / cg" |
|
7355 | 1299 |
! |
1300 |
||
1301 |
equalFromInteger:anInteger |
|
21797 | 1302 |
"anInteger does not know how to compare to the receiver - |
7355 | 1303 |
retry the operation by coercing to higher generality" |
1304 |
||
1305 |
^ anInteger retry:#= coercing:self |
|
21797 | 1306 |
|
1307 |
"Modified (comment): / 12-06-2017 / 20:32:53 / cg" |
|
7355 | 1308 |
! |
1309 |
||
7440 | 1310 |
equalFromLargeFloat:aLargeFloat |
21797 | 1311 |
"aLargeFloat does not know how to compare to the receiver - |
7440 | 1312 |
retry the operation by coercing to higher generality" |
1313 |
||
1314 |
^ aLargeFloat retry:#= coercing:self |
|
21797 | 1315 |
|
1316 |
"Modified (comment): / 12-06-2017 / 20:32:45 / cg" |
|
7440 | 1317 |
! |
1318 |
||
7355 | 1319 |
equalFromLongFloat:aLongFloat |
21797 | 1320 |
"aLongFloat does not know how to compare to the receiver - |
7355 | 1321 |
retry the operation by coercing to higher generality" |
1322 |
||
1323 |
^ aLongFloat retry:#= coercing:self |
|
21797 | 1324 |
|
1325 |
"Modified (comment): / 12-06-2017 / 20:32:36 / cg" |
|
7355 | 1326 |
! |
1327 |
||
24278 | 1328 |
equalFromQuadFloat:aQuadFloat |
1329 |
"aQuadFloat does not know how to compare to the receiver - |
|
1330 |
retry the operation by coercing to higher generality" |
|
1331 |
||
1332 |
^ aQuadFloat retry:#= coercing:self |
|
1333 |
||
1334 |
"Created: / 07-06-2019 / 02:21:11 / Claus Gittinger" |
|
1335 |
! |
|
1336 |
||
7355 | 1337 |
equalFromShortFloat:aShortFloat |
21797 | 1338 |
"aShortFloat does not know how to compare to the receiver - |
7355 | 1339 |
retry the operation by coercing to higher generality" |
1340 |
||
1341 |
^ aShortFloat retry:#= coercing:self |
|
21797 | 1342 |
|
1343 |
"Modified (comment): / 12-06-2017 / 20:32:28 / cg" |
|
7355 | 1344 |
! |
1345 |
||
13007 | 1346 |
integerQuotientFromInteger:anInteger |
1347 |
"anInteger does not know how to divide by the receiver - |
|
1348 |
retry the operation by coercing to higher generality" |
|
1349 |
||
1350 |
^ anInteger retry:#// coercing:self |
|
1351 |
||
1352 |
"Created: / 09-08-2010 / 19:49:26 / cg" |
|
1353 |
! |
|
1354 |
||
16657 | 1355 |
isAlmostEqualToFromFloat:aFloat nEpsilon:nE |
24278 | 1356 |
"aFloat does not know how to compare to the receiver - |
16657 | 1357 |
retry the operation by coercing to higher generality" |
1358 |
||
1359 |
^ aFloat retry:#isAlmostEqualTo:nEpsilon: coercing:self with:nE |
|
24278 | 1360 |
|
1361 |
"Modified (comment): / 07-06-2019 / 02:23:00 / Claus Gittinger" |
|
1362 |
! |
|
1363 |
||
1364 |
isAlmostEqualToFromLargeFloat:aLargeFloat nEpsilon:nE |
|
1365 |
"aLargeFloat does not know how to compare to the receiver - |
|
1366 |
retry the operation by coercing to higher generality" |
|
1367 |
||
1368 |
^ aLargeFloat retry:#isAlmostEqualTo:nEpsilon: coercing:self with:nE |
|
1369 |
||
1370 |
"Created: / 07-06-2019 / 02:24:57 / Claus Gittinger" |
|
1371 |
! |
|
1372 |
||
1373 |
isAlmostEqualToFromLongFloat:aLongFloat nEpsilon:nE |
|
1374 |
"aLongFloat does not know how to compare to the receiver - |
|
1375 |
retry the operation by coercing to higher generality" |
|
1376 |
||
1377 |
^ aLongFloat retry:#isAlmostEqualTo:nEpsilon: coercing:self with:nE |
|
1378 |
||
1379 |
"Created: / 07-06-2019 / 02:22:01 / Claus Gittinger" |
|
1380 |
! |
|
1381 |
||
1382 |
isAlmostEqualToFromQuadFloat:aQuadFloat nEpsilon:nE |
|
1383 |
"aQuadFloat does not know how to compare to the receiver - |
|
1384 |
retry the operation by coercing to higher generality" |
|
1385 |
||
1386 |
^ aQuadFloat retry:#isAlmostEqualTo:nEpsilon: coercing:self with:nE |
|
1387 |
||
1388 |
"Created: / 07-06-2019 / 02:22:15 / Claus Gittinger" |
|
16657 | 1389 |
! |
1390 |
||
1391 |
isAlmostEqualToFromShortFloat:aShortFloat nEpsilon:nE |
|
24278 | 1392 |
"aShortFloat does not know how to compare to the receiver - |
16657 | 1393 |
retry the operation by coercing to higher generality" |
1394 |
||
1395 |
^ aShortFloat retry:#isAlmostEqualTo:nEpsilon: coercing:self with:nE |
|
24278 | 1396 |
|
1397 |
"Modified (comment): / 07-06-2019 / 02:23:25 / Claus Gittinger" |
|
16657 | 1398 |
! |
1399 |
||
1888 | 1400 |
lessFromFixedPoint:aFixedPoint |
21797 | 1401 |
"aFixedPoint does not know how to compare to the receiver - |
18860 | 1402 |
Return true if aFixedPoint < self. |
1888 | 1403 |
retry the operation by coercing to higher generality" |
1404 |
||
1405 |
^ aFixedPoint retry:#< coercing:self |
|
1406 |
||
21797 | 1407 |
"Created: / 05-11-1996 / 14:56:12 / cg" |
1408 |
"Modified (comment): / 12-06-2017 / 20:32:12 / cg" |
|
1888 | 1409 |
! |
1410 |
||
701 | 1411 |
lessFromFloat:aFloat |
21797 | 1412 |
"aFloat does not know how to compare to the receiver - |
18860 | 1413 |
Return true if aFloat < self. |
701 | 1414 |
retry the operation by coercing to higher generality" |
1 | 1415 |
|
701 | 1416 |
^ aFloat retry:#< coercing:self |
21797 | 1417 |
|
1418 |
"Modified (comment): / 12-06-2017 / 20:32:02 / cg" |
|
1 | 1419 |
! |
1420 |
||
701 | 1421 |
lessFromFraction:aFraction |
21797 | 1422 |
"aFraction does not know how to compare to the receiver - |
18860 | 1423 |
Return true if aFraction < self. |
701 | 1424 |
retry the operation by coercing to higher generality" |
1 | 1425 |
|
701 | 1426 |
^ aFraction retry:#< coercing:self |
21797 | 1427 |
|
1428 |
"Modified (comment): / 12-06-2017 / 20:31:53 / cg" |
|
1 | 1429 |
! |
1430 |
||
701 | 1431 |
lessFromInteger:anInteger |
21797 | 1432 |
"anInteger does not know how to compare to the receiver - |
18860 | 1433 |
Return true if anInteger < self. |
701 | 1434 |
retry the operation by coercing to higher generality" |
1 | 1435 |
|
701 | 1436 |
^ anInteger retry:#< coercing:self |
21797 | 1437 |
|
1438 |
"Modified (comment): / 12-06-2017 / 20:31:44 / cg" |
|
1 | 1439 |
! |
1440 |
||
7440 | 1441 |
lessFromLargeFloat:aLargeFloat |
21797 | 1442 |
"aLargeFloat does not know how to compare to the receiver - |
18860 | 1443 |
Return true if aLargeFloat < self. |
7440 | 1444 |
retry the operation by coercing to higher generality" |
1445 |
||
1446 |
^ aLargeFloat retry:#< coercing:self |
|
21797 | 1447 |
|
1448 |
"Modified (comment): / 12-06-2017 / 20:31:36 / cg" |
|
7440 | 1449 |
! |
1450 |
||
7399 | 1451 |
lessFromLongFloat:aLongFloat |
21797 | 1452 |
"aLongFloat does not know how to compare to the receiver - |
18860 | 1453 |
Return true if aLongFloat < self. |
7399 | 1454 |
retry the operation by coercing to higher generality" |
1455 |
||
1456 |
^ aLongFloat retry:#< coercing:self |
|
1457 |
||
21797 | 1458 |
"Modified: / 17-04-1996 / 12:33:33 / cg" |
1459 |
"Modified (comment): / 12-06-2017 / 20:31:28 / cg" |
|
7399 | 1460 |
! |
1461 |
||
24278 | 1462 |
lessFromQuadFloat:aQuadFloat |
1463 |
"aQuadFloat does not know how to compare to the receiver - |
|
1464 |
Return true if aQuadFloat < self. |
|
1465 |
retry the operation by coercing to higher generality" |
|
1466 |
||
1467 |
^ aQuadFloat retry:#< coercing:self |
|
1468 |
||
1469 |
"Created: / 07-06-2019 / 02:23:41 / Claus Gittinger" |
|
1470 |
! |
|
1471 |
||
1200 | 1472 |
lessFromShortFloat:aShortFloat |
21797 | 1473 |
"aShortFloat does not know how to compare to the receiver - |
18860 | 1474 |
Return true if aShortFloat < self. |
1200 | 1475 |
retry the operation by coercing to higher generality" |
1476 |
||
1477 |
^ aShortFloat retry:#< coercing:self |
|
1478 |
||
21797 | 1479 |
"Modified: / 17-04-1996 / 12:33:33 / cg" |
1480 |
"Modified (comment): / 12-06-2017 / 20:31:18 / cg" |
|
1200 | 1481 |
! |
1482 |
||
14002 | 1483 |
moduloFromInteger:anInteger |
1484 |
"anInteger does not know how to compute the modulo from the receiver - |
|
1485 |
retry the operation by coercing to higher generality" |
|
1486 |
||
1487 |
^ anInteger retry:#\\ coercing:self |
|
1488 |
||
1489 |
"Created: / 12-02-2012 / 20:38:13 / cg" |
|
1490 |
! |
|
1491 |
||
7355 | 1492 |
productFromComplex:aComplex |
21797 | 1493 |
"aComplex does not know how to multiply the receiver - |
7355 | 1494 |
retry the operation by coercing to higher generality" |
1495 |
||
1496 |
^ aComplex retry:#* coercing:self |
|
21797 | 1497 |
|
1498 |
"Modified (comment): / 12-06-2017 / 20:31:06 / cg" |
|
7355 | 1499 |
! |
1500 |
||
1888 | 1501 |
productFromFixedPoint:aFixedPoint |
21797 | 1502 |
"aFixedPoint does not know how to multiply the receiver - |
1888 | 1503 |
retry the operation by coercing to higher generality" |
1504 |
||
1505 |
^ aFixedPoint retry:#* coercing:self |
|
1506 |
||
21797 | 1507 |
"Created: / 05-11-1996 / 14:56:28 / cg" |
1508 |
"Modified (comment): / 12-06-2017 / 20:30:56 / cg" |
|
1888 | 1509 |
! |
1510 |
||
701 | 1511 |
productFromFloat:aFloat |
21797 | 1512 |
"aFloat does not know how to multiply the receiver - |
701 | 1513 |
retry the operation by coercing to higher generality" |
1 | 1514 |
|
701 | 1515 |
^ aFloat retry:#* coercing:self |
21797 | 1516 |
|
1517 |
"Modified (comment): / 12-06-2017 / 20:30:46 / cg" |
|
1 | 1518 |
! |
1519 |
||
701 | 1520 |
productFromFraction:aFraction |
21797 | 1521 |
"aFraction does not know how to multiply the receiver - |
701 | 1522 |
retry the operation by coercing to higher generality" |
1 | 1523 |
|
701 | 1524 |
^ aFraction retry:#* coercing:self |
21797 | 1525 |
|
1526 |
"Modified (comment): / 12-06-2017 / 20:30:38 / cg" |
|
1 | 1527 |
! |
1528 |
||
701 | 1529 |
productFromInteger:anInteger |
21797 | 1530 |
"anInteger does not know how to multiply the receiver - |
701 | 1531 |
retry the operation by coercing to higher generality" |
1 | 1532 |
|
701 | 1533 |
^ anInteger retry:#* coercing:self |
21797 | 1534 |
|
1535 |
"Modified (comment): / 12-06-2017 / 20:29:21 / cg" |
|
1 | 1536 |
! |
1537 |
||
7440 | 1538 |
productFromLargeFloat:aLargeFloat |
21797 | 1539 |
"aLargeFloat does not know how to multiply the receiver - |
7440 | 1540 |
retry the operation by coercing to higher generality" |
1541 |
||
1542 |
^ aLargeFloat retry:#* coercing:self |
|
1543 |
||
21797 | 1544 |
"Created: / 17-04-1996 / 12:33:48 / cg" |
1545 |
"Modified (comment): / 12-06-2017 / 20:29:13 / cg" |
|
7440 | 1546 |
! |
1547 |
||
7399 | 1548 |
productFromLongFloat:aLongFloat |
21797 | 1549 |
"aLongFloat does not know how to multiply the receiver - |
7399 | 1550 |
retry the operation by coercing to higher generality" |
1551 |
||
1552 |
^ aLongFloat retry:#* coercing:self |
|
1553 |
||
21797 | 1554 |
"Created: / 17-04-1996 / 12:33:48 / cg" |
1555 |
"Modified (comment): / 12-06-2017 / 20:29:05 / cg" |
|
7399 | 1556 |
! |
1557 |
||
24278 | 1558 |
productFromQuadFloat:aQuadFloat |
1559 |
"aQuadFloat does not know how to multiply the receiver - |
|
1560 |
retry the operation by coercing to higher generality" |
|
1561 |
||
1562 |
^ aQuadFloat retry:#* coercing:self |
|
1563 |
||
1564 |
"Created: / 07-06-2019 / 02:24:05 / Claus Gittinger" |
|
1565 |
! |
|
1566 |
||
1200 | 1567 |
productFromShortFloat:aShortFloat |
21797 | 1568 |
"aShortFloat does not know how to multiply the receiver - |
1200 | 1569 |
retry the operation by coercing to higher generality" |
1570 |
||
1571 |
^ aShortFloat retry:#* coercing:self |
|
1572 |
||
21797 | 1573 |
"Created: / 17-04-1996 / 12:33:48 / cg" |
1574 |
"Modified (comment): / 12-06-2017 / 20:28:56 / cg" |
|
1200 | 1575 |
! |
1576 |
||
7355 | 1577 |
quotientFromComplex:aComplex |
7440 | 1578 |
"aComplex does not know how to divide by the receiver - |
7355 | 1579 |
retry the operation by coercing to higher generality" |
1580 |
||
1581 |
^ aComplex retry:#/ coercing:self |
|
1582 |
! |
|
1583 |
||
1888 | 1584 |
quotientFromFixedPoint:aFixedPoint |
7440 | 1585 |
"aFixedPoint does not know how to divide by the receiver - |
1888 | 1586 |
retry the operation by coercing to higher generality" |
1587 |
||
1588 |
^ aFixedPoint retry:#/ coercing:self |
|
1589 |
||
1590 |
"Created: 5.11.1996 / 14:56:41 / cg" |
|
1591 |
! |
|
1592 |
||
701 | 1593 |
quotientFromFloat:aFloat |
7440 | 1594 |
"aFloat does not know how to divide by the receiver - |
701 | 1595 |
retry the operation by coercing to higher generality" |
1 | 1596 |
|
701 | 1597 |
^ aFloat retry:#/ coercing:self |
1 | 1598 |
! |
1599 |
||
701 | 1600 |
quotientFromFraction:aFraction |
7440 | 1601 |
"aFraction does not know how to divide by the receiver - |
701 | 1602 |
retry the operation by coercing to higher generality" |
1 | 1603 |
|
701 | 1604 |
^ aFraction retry:#/ coercing:self |
1 | 1605 |
! |
1606 |
||
21797 | 1607 |
quotientFromInteger:aQDouble |
1608 |
"aQDouble does not know how to divide by the receiver - |
|
1 | 1609 |
retry the operation by coercing to higher generality" |
1610 |
||
21797 | 1611 |
^ aQDouble retry:#/ coercing:self |
1612 |
||
1613 |
"Modified (comment): / 12-06-2017 / 20:35:45 / cg" |
|
1 | 1614 |
! |
1615 |
||
7440 | 1616 |
quotientFromLargeFloat:aLargeFloat |
1617 |
"aLargeFloat does not know how to divide by the receiver - |
|
1618 |
retry the operation by coercing to higher generality" |
|
1619 |
||
1620 |
^ aLargeFloat retry:#/ coercing:self |
|
1621 |
||
1622 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
1623 |
! |
|
1624 |
||
7399 | 1625 |
quotientFromLongFloat:aLongFloat |
7440 | 1626 |
"aLongFloat does not know how to divide by the receiver - |
7399 | 1627 |
retry the operation by coercing to higher generality" |
1628 |
||
1629 |
^ aLongFloat retry:#/ coercing:self |
|
1630 |
||
1631 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
1632 |
! |
|
1633 |
||
24278 | 1634 |
quotientFromQuadFloat:aQuadFloat |
1635 |
"aQuadFloat does not know how to divide by the receiver - |
|
1636 |
retry the operation by coercing to higher generality" |
|
1637 |
||
1638 |
^ aQuadFloat retry:#/ coercing:self |
|
1639 |
||
1640 |
"Created: / 07-06-2019 / 02:25:19 / Claus Gittinger" |
|
1641 |
! |
|
1642 |
||
1200 | 1643 |
quotientFromShortFloat:aShortFloat |
7440 | 1644 |
"aShortFloat does not know how to divide by the receiver - |
1200 | 1645 |
retry the operation by coercing to higher generality" |
1646 |
||
1647 |
^ aShortFloat retry:#/ coercing:self |
|
1648 |
||
1649 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
1650 |
! |
|
1651 |
||
21935 | 1652 |
raisedFromFloat:aFloat |
1653 |
"aFloat does not know how to be raised to the receiver" |
|
1654 |
||
1655 |
self isNumber ifTrue:[ |
|
1656 |
self isFloat ifFalse:[ |
|
1657 |
^ self asFloat raisedFromFloat:aFloat |
|
1658 |
]. |
|
1659 |
]. |
|
1660 |
^ self raisedFromNumber:aFloat |
|
1661 |
||
1662 |
"Created: / 01-07-2017 / 21:18:54 / cg" |
|
1663 |
"Modified: / 02-07-2017 / 00:44:47 / cg" |
|
1664 |
! |
|
1665 |
||
1666 |
raisedFromNumber:aNumber |
|
21967 | 1667 |
"aNumber does not know how to be raised to the receiver |
1668 |
(i.e. how to compute aNumber^self)" |
|
21935 | 1669 |
|
1670 |
^ self class |
|
1671 |
raise:#domainErrorSignal |
|
1672 |
receiver:aNumber |
|
1673 |
selector:#raisedTo: |
|
1674 |
arguments:(Array with:self) |
|
1675 |
errorString:'bad receiver/arg in raisedTo:' |
|
1676 |
||
1677 |
"Created: / 01-07-2017 / 21:17:06 / cg" |
|
21967 | 1678 |
"Modified (comment): / 03-07-2017 / 14:06:53 / cg" |
21935 | 1679 |
! |
1680 |
||
14747 | 1681 |
remainderFromFloat:aFloat |
1682 |
"aFloat does not know how to compute the remainder with the receiver - |
|
1683 |
retry the operation by coercing to higher generality" |
|
1684 |
||
1685 |
^ aFloat retry:#rem: coercing:self |
|
1686 |
! |
|
1687 |
||
24278 | 1688 |
remainderFromLargeFloat:aLargeFloat |
1689 |
"aLargeFloat does not know how to compute the remainder with the receiver - |
|
1690 |
retry the operation by coercing to higher generality" |
|
1691 |
||
1692 |
^ aLargeFloat retry:#rem: coercing:self |
|
1693 |
||
1694 |
"Created: / 07-06-2019 / 02:25:57 / Claus Gittinger" |
|
1695 |
! |
|
1696 |
||
1697 |
remainderFromLongFloat:aLongFloat |
|
1698 |
"aLongFloat does not know how to compute the remainder with the receiver - |
|
1699 |
retry the operation by coercing to higher generality" |
|
1700 |
||
1701 |
^ aLongFloat retry:#rem: coercing:self |
|
1702 |
||
1703 |
"Created: / 07-06-2019 / 02:25:43 / Claus Gittinger" |
|
1704 |
! |
|
1705 |
||
1706 |
remainderFromQuadFloat:aQuadFloat |
|
1707 |
"aQuadFloat does not know how to compute the remainder with the receiver - |
|
1708 |
retry the operation by coercing to higher generality" |
|
1709 |
||
1710 |
^ aQuadFloat retry:#rem: coercing:self |
|
1711 |
||
1712 |
"Created: / 07-06-2019 / 02:26:08 / Claus Gittinger" |
|
1713 |
! |
|
1714 |
||
14747 | 1715 |
remainderFromShortFloat:aShortFloat |
1716 |
"aShortFloat does not know how to compute the remainder with the receiver - |
|
1717 |
retry the operation by coercing to higher generality" |
|
1718 |
||
1719 |
^ aShortFloat retry:#rem: coercing:self |
|
1720 |
! |
|
1721 |
||
7355 | 1722 |
sumFromComplex:aComplex |
21797 | 1723 |
"aComplex does not know how to add the receiver - |
7355 | 1724 |
retry the operation by coercing to higher generality" |
1725 |
||
1726 |
^ aComplex retry:#+ coercing:self |
|
21797 | 1727 |
|
1728 |
"Modified (comment): / 12-06-2017 / 20:27:25 / cg" |
|
7355 | 1729 |
! |
1730 |
||
1888 | 1731 |
sumFromFixedPoint:aFixedPoint |
21797 | 1732 |
"aFixedPoint does not know how to add the receiver - |
1888 | 1733 |
retry the operation by coercing to higher generality" |
1734 |
||
1735 |
^ aFixedPoint retry:#+ coercing:self |
|
1736 |
||
21797 | 1737 |
"Created: / 05-11-1996 / 14:56:56 / cg" |
1738 |
"Modified (comment): / 12-06-2017 / 20:27:38 / cg" |
|
1888 | 1739 |
! |
1740 |
||
1 | 1741 |
sumFromFloat:aFloat |
21797 | 1742 |
"aFloat does not know how to add the receiver - |
1 | 1743 |
retry the operation by coercing to higher generality" |
1744 |
||
1745 |
^ aFloat retry:#+ coercing:self |
|
21797 | 1746 |
|
1747 |
"Modified (comment): / 12-06-2017 / 20:27:47 / cg" |
|
1 | 1748 |
! |
1749 |
||
1750 |
sumFromFraction:aFraction |
|
21797 | 1751 |
"aFraction does not know how to add the receiver - |
1 | 1752 |
retry the operation by coercing to higher generality" |
1753 |
||
1754 |
^ aFraction retry:#+ coercing:self |
|
21797 | 1755 |
|
1756 |
"Modified (comment): / 12-06-2017 / 20:27:55 / cg" |
|
1 | 1757 |
! |
1758 |
||
701 | 1759 |
sumFromInteger:anInteger |
21797 | 1760 |
"anInteger does not know how to add the receiver - |
1 | 1761 |
retry the operation by coercing to higher generality" |
1762 |
||
701 | 1763 |
^ anInteger retry:#+ coercing:self |
21797 | 1764 |
|
1765 |
"Modified (comment): / 12-06-2017 / 20:28:03 / cg" |
|
1200 | 1766 |
! |
1767 |
||
7440 | 1768 |
sumFromLargeFloat:aLargeFloat |
21797 | 1769 |
"aLargeFloat does not know how to add the receiver - |
7440 | 1770 |
retry the operation by coercing to higher generality" |
1771 |
||
1772 |
^ aLargeFloat retry:#+ coercing:self |
|
1773 |
||
21797 | 1774 |
"Created: / 17-04-1996 / 12:34:10 / cg" |
1775 |
"Modified (comment): / 12-06-2017 / 20:28:11 / cg" |
|
7440 | 1776 |
! |
1777 |
||
7399 | 1778 |
sumFromLongFloat:aLongFloat |
21797 | 1779 |
"aLongFloat does not know how to add the receiver - |
7399 | 1780 |
retry the operation by coercing to higher generality" |
1781 |
||
1782 |
^ aLongFloat retry:#+ coercing:self |
|
1783 |
||
21797 | 1784 |
"Created: / 17-04-1996 / 12:34:10 / cg" |
1785 |
"Modified (comment): / 12-06-2017 / 20:28:19 / cg" |
|
7399 | 1786 |
! |
1787 |
||
24278 | 1788 |
sumFromQuadFloat:aQuadFloat |
1789 |
"aQuadFloat does not know how to add the receiver - |
|
1790 |
retry the operation by coercing to higher generality" |
|
1791 |
||
1792 |
^ aQuadFloat retry:#+ coercing:self |
|
1793 |
||
1794 |
"Created: / 07-06-2019 / 02:26:36 / Claus Gittinger" |
|
1795 |
! |
|
1796 |
||
1200 | 1797 |
sumFromShortFloat:aShortFloat |
21797 | 1798 |
"aShortFloat does not know how to add the receiver - |
1200 | 1799 |
retry the operation by coercing to higher generality" |
1800 |
||
1801 |
^ aShortFloat retry:#+ coercing:self |
|
1802 |
||
21797 | 1803 |
"Created: / 17-04-1996 / 12:34:10 / cg" |
1804 |
"Modified (comment): / 12-06-2017 / 20:28:39 / cg" |
|
1 | 1805 |
! ! |
1806 |
||
7384 | 1807 |
!ArithmeticValue methodsFor:'mathematical functions'! |
1 | 1808 |
|
10326 | 1809 |
** aNumber |
9443 | 1810 |
"Answer the receiver raised to the power of the argument, aNumber." |
1811 |
"same as Number>>raisedTo:" |
|
1812 |
||
1813 |
^self raisedTo: aNumber |
|
1814 |
||
1815 |
"Created: / 15-07-2006 / 15:11:05 / cg" |
|
1816 |
"Modified: / 17-07-2006 / 12:51:33 / cg" |
|
1817 |
! |
|
1818 |
||
18860 | 1819 |
basicRaisedToInteger:exp |
21804 | 1820 |
"return the receiver raised to exp. |
1821 |
Warning: if the receiver is a float/double, |
|
1822 |
currently INF may be returned on overflow. |
|
1823 |
This may be changed silently to raise an error in future versions." |
|
18860 | 1824 |
|
1825 |
|result e t| |
|
1826 |
||
1827 |
"use the addition chaining algorithm, |
|
1828 |
which is much faster for big exponents" |
|
6858 | 1829 |
|
18860 | 1830 |
result := 1. |
1831 |
t := self. |
|
24956 | 1832 |
(e := exp) < 0 ifTrue:[ |
18860 | 1833 |
e := exp negated. |
1834 |
]. |
|
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1835 |
|
18860 | 1836 |
[e ~~ 0] whileTrue:[ |
1837 |
[(e bitAnd:1) == 0] whileTrue:[ |
|
1838 |
e := e bitShift:-1. |
|
1839 |
t := t * t. |
|
1840 |
]. |
|
1841 |
e := e - 1. |
|
1842 |
result := result * t. |
|
1843 |
]. |
|
1844 |
||
1845 |
(exp < 0) ifTrue:[ |
|
1846 |
^ 1 / result |
|
1847 |
]. |
|
1848 |
||
1849 |
^ result |
|
18835 | 1850 |
|
1851 |
" |
|
1852 |
(2 raisedToInteger:216) |
|
24956 | 1853 |
(2.0 raisedToInteger:216) |
1854 |
(2.0 asLongFloat) raisedToInteger:216 |
|
1855 |
(2.0 asShortFloat) raisedToInteger:216 |
|
1856 |
(2.0 asQDouble) raisedToInteger:216 |
|
1857 |
||
18835 | 1858 |
(2 raisedTo:216) |
1859 |
-> 105312291668557186697918027683670432318895095400549111254310977536 |
|
24956 | 1860 |
(2.0 raisedToInteger:216) asInteger - (2 raisedToInteger:216) |
1861 |
(2.0 raisedToInteger:400) asInteger - (2 raisedToInteger:400) |
|
1862 |
(2.0 raisedToInteger:500) asInteger - (2 raisedToInteger:500) |
|
1863 |
(2.0 raisedToInteger:1000) asInteger - (2 raisedToInteger:1000) |
|
18835 | 1864 |
|
1865 |
(2 raisedToInteger:216) asFloat |
|
1866 |
(2 raisedTo:216) asFloat |
|
1867 |
-> 1.05312E+65 |
|
1868 |
||
1869 |
(2 raisedToInteger:500) |
|
1870 |
(2 raisedTo:500) |
|
1871 |
-> 3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376 |
|
18860 | 1872 |
(2 raisedTo:-500) |
1873 |
-> (1/3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376) |
|
18835 | 1874 |
2 raisedToInteger:10 |
1875 |
-> 1024 |
|
1876 |
-2 raisedToInteger:10 |
|
1877 |
-> 1024 |
|
1878 |
-2 raisedToInteger:9 |
|
1879 |
-> -512 |
|
1880 |
10 raisedToInteger:-10 |
|
1881 |
-> (1/10000000000) |
|
1882 |
2 raisedToInteger:0 |
|
1883 |
-> 1 |
|
1884 |
2 raisedToInteger:-1 |
|
1885 |
-> (1/2) |
|
1886 |
||
1887 |
Time millisecondsToRun:[ |
|
1888 |
10000 timesRepeat:[ |
|
1889 |
(2 raisedToInteger:500) |
|
1890 |
] |
|
1891 |
] |
|
1892 |
||
1893 |
Time millisecondsToRun:[ |
|
1894 |
|bigNum| |
|
1895 |
bigNum := 2 raisedToInteger:500. |
|
1896 |
10 timesRepeat:[ |
|
1897 |
(bigNum raisedToInteger:500) |
|
1898 |
] |
|
1899 |
] |
|
1900 |
" |
|
1901 |
||
21804 | 1902 |
"Created: / 27-04-1999 / 15:19:22 / stefan" |
1903 |
"Modified (comment): / 13-06-2017 / 17:35:41 / cg" |
|
18835 | 1904 |
! |
1905 |
||
18860 | 1906 |
raisedTo: aNumber |
21935 | 1907 |
"return the receiver raised to aNumber (i.e. self ^ aNumber)" |
1908 |
||
18860 | 1909 |
aNumber isInteger ifTrue:[ |
21935 | 1910 |
^ self raisedToInteger:aNumber |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1911 |
]. |
23158 | 1912 |
self raisedFromNumber:aNumber. "/ this raises an error. |
21935 | 1913 |
|
1914 |
" |
|
1915 |
2 raisedTo:16 |
|
1916 |
2.0 raisedTo:16 |
|
1917 |
||
1918 |
3 raisedTo:4 |
|
23158 | 1919 |
10@10 raisedTo:4 |
1920 |
10@10 raisedTo:4.0 |
|
21935 | 1921 |
" |
1922 |
||
1923 |
"Modified (format): / 01-07-2017 / 20:59:35 / cg" |
|
18860 | 1924 |
! |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1925 |
|
18860 | 1926 |
raisedToInteger:exp |
1927 |
"return the receiver raised to exp. |
|
21804 | 1928 |
Warning: if the receiver is a float/double, |
1929 |
currently INF may be returned on overflow. |
|
1930 |
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
|
1931 |
|
18860 | 1932 |
^ self basicRaisedToInteger:exp |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1933 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1934 |
" |
18860 | 1935 |
(2.0 raisedToInteger:10000) |
1936 |
(2 raisedToInteger:10000) |
|
1937 |
(2 raisedToInteger:-10000) |
|
1938 |
||
17662 | 1939 |
(2.0 raisedToInteger:216) |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1940 |
(2 raisedToInteger:216) |
10326 | 1941 |
(2 raisedTo:216) |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1942 |
-> 105312291668557186697918027683670432318895095400549111254310977536 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1943 |
|
10326 | 1944 |
(2 raisedToInteger:216) asFloat |
1945 |
(2 raisedTo:216) asFloat |
|
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1946 |
-> 1.05312E+65 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1947 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1948 |
(2 raisedToInteger:500) |
10326 | 1949 |
(2 raisedTo:500) |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1950 |
-> 3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376 |
10326 | 1951 |
2 raisedToInteger:10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1952 |
-> 1024 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1953 |
-2 raisedToInteger:10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1954 |
-> 1024 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1955 |
-2 raisedToInteger:9 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1956 |
-> -512 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1957 |
10 raisedToInteger:-10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1958 |
-> (1/10000000000) |
10326 | 1959 |
2 raisedToInteger:0 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1960 |
-> 1 |
10326 | 1961 |
2 raisedToInteger:-1 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1962 |
-> (1/2) |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1963 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1964 |
Time millisecondsToRun:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1965 |
10000 timesRepeat:[ |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1966 |
(2 raisedToInteger:500) |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1967 |
] |
10326 | 1968 |
] |
8633
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 |
Time millisecondsToRun:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1971 |
|bigNum| |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1972 |
bigNum := 2 raisedToInteger:500. |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1973 |
10 timesRepeat:[ |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1974 |
(bigNum raisedToInteger:500) |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1975 |
] |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1976 |
] |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1977 |
" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1978 |
|
21804 | 1979 |
"Created: / 27-04-1999 / 15:19:22 / stefan" |
1980 |
"Modified (comment): / 13-06-2017 / 17:35:20 / cg" |
|
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1981 |
! |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1982 |
|
701 | 1983 |
squared |
1984 |
"return receiver * receiver" |
|
1985 |
||
1986 |
^ self * self |
|
1987 |
! ! |
|
1988 |
||
1989 |
!ArithmeticValue methodsFor:'queries'! |
|
1990 |
||
1991 |
respondsToArithmetic |
|
1992 |
"return true, if the receiver responds to arithmetic messages" |
|
1993 |
||
1994 |
^ true |
|
1995 |
! ! |
|
1996 |
||
1997 |
!ArithmeticValue methodsFor:'testing'! |
|
1998 |
||
1999 |
denominator |
|
2000 |
"return the denominator of the receiver" |
|
2001 |
||
2002 |
^ 1 |
|
2003 |
! |
|
2004 |
||
2005 |
even |
|
17223 | 2006 |
"return true if the receiver is divisible by 2. |
21967 | 2007 |
This is only defined for whole-numbers (integers)." |
701 | 2008 |
|
17223 | 2009 |
^ self class |
21967 | 2010 |
raise:#domainErrorSignal |
17223 | 2011 |
receiver:self |
2012 |
selector:#even |
|
2013 |
arguments:#() |
|
2014 |
errorString:'Receiver must be a whole-number' |
|
2015 |
||
21967 | 2016 |
"/ No no no, the following is bad: |
2017 |
"/ ^ self truncated asInteger even |
|
17223 | 2018 |
|
2019 |
" |
|
21967 | 2020 |
2.4 even |
2021 |
2.0 even |
|
17223 | 2022 |
" |
21967 | 2023 |
|
2024 |
"Modified (comment): / 03-07-2017 / 14:10:01 / cg" |
|
701 | 2025 |
! |
2026 |
||
3635 | 2027 |
isComplex |
7355 | 2028 |
"Answer whether the receiver has an imaginary part |
2029 |
(i.e. if it is a complex number). Always false here." |
|
3635 | 2030 |
|
2031 |
^ false |
|
2032 |
||
2033 |
"Modified: / 9.7.1998 / 10:19:27 / cg" |
|
2034 |
! |
|
2035 |
||
8979
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
2036 |
isFinite |
24711 | 2037 |
"return true, if the receiver is finite (not NaN and not +/-INF) |
9400 | 2038 |
i.e. it can be represented as a rational number." |
2039 |
||
8979
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
2040 |
^ true |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
2041 |
! |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
2042 |
|
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
2043 |
isInfinite |
24711 | 2044 |
"return true, if the receiver is an infinite number (+Inf or -Inf)" |
2045 |
||
8979
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
2046 |
^ false |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
2047 |
! |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
2048 |
|
7440 | 2049 |
isNegativeInfinity |
2050 |
^ self negative and:[self isInfinite] |
|
2051 |
! |
|
2052 |
||
2053 |
isNegativeZero |
|
2054 |
"return false - must be redefined by subclasses which can represent a negative zero |
|
2055 |
(i.e. limitedPrecisionReal classes)" |
|
2056 |
||
2057 |
^ false |
|
2058 |
! |
|
2059 |
||
2060 |
isPositiveInfinity |
|
2061 |
^ self positive and:[self isInfinite] |
|
2062 |
! |
|
2063 |
||
6156 | 2064 |
isReal |
2065 |
"return true, if the receiver is some kind of real number (as opposed to a complex); |
|
2066 |
false is returned here - the method is only redefined in Number (and Complex)." |
|
2067 |
||
2068 |
^ false |
|
2069 |
||
2070 |
"Modified: / 6.11.2001 / 13:20:20 / cg" |
|
2071 |
"Created: / 6.11.2001 / 13:26:29 / cg" |
|
2072 |
! |
|
2073 |
||
7440 | 2074 |
isZero |
2075 |
"return false - must be redefined by subclasses which can represent a negative zero |
|
2076 |
(i.e. limitedPrecisionReal classes)" |
|
701 | 2077 |
|
7440 | 2078 |
^ self = self class zero |
2079 |
! |
|
2080 |
||
2081 |
negative |
|
18860 | 2082 |
"return true if the receiver is less than zero." |
7440 | 2083 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
2084 |
^ self < self class zero |
701 | 2085 |
! |
2086 |
||
2087 |
numerator |
|
2088 |
"return the numerator of the receiver." |
|
2089 |
||
2090 |
^ self |
|
2091 |
! |
|
2092 |
||
2093 |
odd |
|
2094 |
"return true if the receiver is not divisible by 2" |
|
2095 |
||
2096 |
^ self even not |
|
2097 |
! |
|
2098 |
||
2099 |
positive |
|
18860 | 2100 |
"return true, if the receiver is greater or equal to zero (not negative)" |
701 | 2101 |
|
2102 |
^ self negative not |
|
2103 |
! |
|
2104 |
||
2105 |
sign |
|
7471 | 2106 |
"return the sign of the receiver (-1, 0 or 1)" |
701 | 2107 |
|
7440 | 2108 |
|zero| |
2109 |
||
2110 |
zero := self class zero. |
|
2111 |
(self < zero) ifTrue:[^ -1]. |
|
2112 |
(zero < self) ifTrue:[^ 1]. |
|
701 | 2113 |
^ 0 |
2114 |
! |
|
2115 |
||
2116 |
strictlyPositive |
|
18860 | 2117 |
"return true, if the receiver is greater than zero" |
701 | 2118 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
2119 |
^ self class zero < self |
1 | 2120 |
! ! |
2121 |
||
701 | 2122 |
!ArithmeticValue methodsFor:'truncation & rounding'! |
1 | 2123 |
|
701 | 2124 |
ceiling |
2125 |
"return the integer nearest the receiver towards positive infinity." |
|
1 | 2126 |
|
701 | 2127 |
|anInteger| |
1 | 2128 |
|
701 | 2129 |
anInteger := self // 1. "truncates towards negative infinity" |
2130 |
anInteger = self ifTrue:[^ anInteger]. |
|
2131 |
^ anInteger + 1 |
|
1 | 2132 |
! |
2133 |
||
701 | 2134 |
floor |
2135 |
"return the receiver truncated towards negative infinity" |
|
1 | 2136 |
|
701 | 2137 |
^ self // 1 |
1 | 2138 |
! |
2139 |
||
701 | 2140 |
roundTo:aNumber |
2141 |
"return the receiver rounded to multiples of aNumber" |
|
1 | 2142 |
|
701 | 2143 |
^ (self / aNumber) rounded * aNumber |
8449 | 2144 |
|
2145 |
" |
|
10326 | 2146 |
0 roundTo:4 |
8449 | 2147 |
1 roundTo:4 |
2148 |
2 roundTo:4 |
|
2149 |
3 roundTo:4 |
|
2150 |
4 roundTo:4 |
|
2151 |
5 roundTo:4 |
|
2152 |
6 roundTo:4 |
|
2153 |
7 roundTo:4 |
|
14497 | 2154 |
|
24861 | 2155 |
1.15 roundTo:0.1 |
2156 |
1.14 roundTo:0.1 |
|
2157 |
||
2158 |
7.15 roundTo:0.1 |
|
2159 |
7.149 roundTo:0.1 |
|
2160 |
||
14497 | 2161 |
7.123 roundTo:0.1 |
2162 |
7.523 roundTo:0.1 |
|
2163 |
7.583 roundTo:0.1 |
|
2164 |
7.623 roundTo:0.1 |
|
2165 |
7.623 roundTo:0.01 |
|
2166 |
7.628 roundTo:0.01 |
|
8449 | 2167 |
" |
2168 |
! |
|
2169 |
||
2170 |
roundUpTo:aNumber |
|
2171 |
"return the receiver rounded up to the next multiple of aNumber" |
|
2172 |
||
2173 |
^ (self / aNumber) ceiling * aNumber |
|
2174 |
||
2175 |
" |
|
10326 | 2176 |
0 roundUpTo:4 |
2177 |
1 roundUpTo:4 |
|
2178 |
2 roundUpTo:4 |
|
2179 |
3 roundUpTo:4 |
|
2180 |
4 roundUpTo:4 |
|
2181 |
5 roundUpTo:4 |
|
2182 |
6 roundUpTo:4 |
|
2183 |
7 roundUpTo:4 |
|
2184 |
8 roundUpTo:4 |
|
10911 | 2185 |
(3@4) roundUpTo:8 |
2186 |
(3@4) roundUpTo:(5 @ 4) |
|
2187 |
(3@3) roundUpTo:(5 @ 4) |
|
8449 | 2188 |
" |
701 | 2189 |
! |
2190 |
||
2191 |
rounded |
|
2192 |
"return the integer nearest the receiver" |
|
2193 |
||
1881 | 2194 |
self negative ifTrue:[ |
10326 | 2195 |
^ (self - 0.5) ceiling |
1881 | 2196 |
]. |
701 | 2197 |
^ (self + 0.5) floor |
1881 | 2198 |
|
2199 |
"Modified: 5.11.1996 / 11:31:59 / cg" |
|
1 | 2200 |
! |
2201 |
||
701 | 2202 |
truncateTo:aNumber |
2203 |
"return the receiver truncated to multiples of aNumber" |
|
1 | 2204 |
|
19056 | 2205 |
|t| |
2206 |
||
24274 | 2207 |
aNumber <= 0 ifTrue:[ArgumentError raiseErrorString:'argument must be positive']. |
19056 | 2208 |
|
2209 |
t := (self / aNumber) floor * aNumber. |
|
2210 |
(aNumber isInteger) ifTrue:[ |
|
2211 |
^ t asInteger |
|
2212 |
]. |
|
2213 |
^ t |
|
2214 |
||
2215 |
" |
|
2216 |
truncate to multiples of 4 |
|
2217 |
123.456 truncateTo:4 |
|
2218 |
124.456 truncateTo:4 |
|
2219 |
truncate to multiples of 2 |
|
2220 |
122.456 truncateTo:2 |
|
2221 |
123.456 truncateTo:2 |
|
2222 |
124.456 truncateTo:2 |
|
2223 |
normal truncate |
|
2224 |
123.456 truncateTo:1 |
|
2225 |
124.456 truncateTo:1 |
|
2226 |
truncate to decimal digits |
|
2227 |
123.456 truncateTo:0.1 |
|
2228 |
123.987 truncateTo:0.1 |
|
2229 |
123.456 truncateTo:0.01 |
|
2230 |
" |
|
24274 | 2231 |
|
2232 |
"Modified: / 06-06-2019 / 23:27:55 / Claus Gittinger" |
|
1 | 2233 |
! |
2234 |
||
701 | 2235 |
truncated |
2236 |
"return the receiver truncated towards zero" |
|
1 | 2237 |
|
701 | 2238 |
self negative ifTrue:[ |
2239 |
^ self ceiling |
|
2240 |
]. |
|
2241 |
^ self floor |
|
2242 |
! ! |
|
1 | 2243 |
|
701 | 2244 |
!ArithmeticValue class methodsFor:'documentation'! |
2245 |
||
2246 |
version |
|
18835 | 2247 |
^ '$Header$' |
12576 | 2248 |
! |
2249 |
||
2250 |
version_CVS |
|
18835 | 2251 |
^ '$Header$' |
1 | 2252 |
! ! |
6858 | 2253 |
|
14747 | 2254 |
|
701 | 2255 |
ArithmeticValue initialize! |