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