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