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