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