author | Stefan Vogel <sv@exept.de> |
Thu, 01 Nov 2018 12:35:48 +0100 | |
changeset 23499 | 6bdd727d4170 |
parent 23158 | 1d6551c846d0 |
child 23645 | c1d740fc821d |
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 |
|
23499 | 1084 |
bitAndFromInteger:anInteger |
1085 |
"anInteger does not know how to do bitAnd: with the receiver - |
|
1086 |
retry the operation by coercing to Integer" |
|
1087 |
||
1088 |
|selfAsInteger| |
|
1089 |
||
1090 |
selfAsInteger := self asInteger. |
|
1091 |
self ~= selfAsInteger ifTrue:[ |
|
1092 |
DomainError raiseRequestErrorString:'non-integer mask in bit-operation'. |
|
1093 |
]. |
|
1094 |
||
1095 |
^ anInteger bitAnd:selfAsInteger |
|
1096 |
||
1097 |
"Created: / 01-11-2018 / 11:49:27 / Stefan Vogel" |
|
1098 |
! |
|
1099 |
||
1100 |
bitOrFromInteger:anInteger |
|
1101 |
"anInteger does not know how to do bitOr: with the receiver - |
|
1102 |
retry the operation by coercing to Integer" |
|
1103 |
||
1104 |
|selfAsInteger| |
|
1105 |
||
1106 |
selfAsInteger := self asInteger. |
|
1107 |
self ~= selfAsInteger ifTrue:[ |
|
1108 |
DomainError raiseRequestErrorString:'non-integer mask in bit-operation'. |
|
1109 |
]. |
|
1110 |
||
1111 |
^ anInteger bitOr:selfAsInteger |
|
1112 |
||
1113 |
"Created: / 01-11-2018 / 11:49:09 / Stefan Vogel" |
|
1114 |
! |
|
1115 |
||
1116 |
bitXorFromInteger:anInteger |
|
1117 |
"anInteger does not know how to do bitXor: with the receiver - |
|
1118 |
retry the operation by coercing to Integer" |
|
1119 |
||
1120 |
|selfAsInteger| |
|
1121 |
||
1122 |
selfAsInteger := self asInteger. |
|
1123 |
self ~= selfAsInteger ifTrue:[ |
|
1124 |
DomainError raiseRequestErrorString:'non-integer mask in bit-operation'. |
|
1125 |
]. |
|
1126 |
||
1127 |
^ anInteger bitXor:selfAsInteger |
|
1128 |
||
1129 |
"Created: / 01-11-2018 / 11:58:53 / Stefan Vogel" |
|
1130 |
! |
|
1131 |
||
7355 | 1132 |
differenceFromComplex:aComplex |
21797 | 1133 |
"aComplex does not know how to subtract the receiver - |
7355 | 1134 |
retry the operation by coercing to higher generality" |
1135 |
||
1136 |
^ aComplex retry:#- coercing:self |
|
21797 | 1137 |
|
1138 |
"Modified (comment): / 12-06-2017 / 20:34:41 / cg" |
|
7355 | 1139 |
! |
1140 |
||
1888 | 1141 |
differenceFromFixedPoint:aFixedPoint |
21797 | 1142 |
"aFixedPoint does not know how to subtract the receiver - |
1888 | 1143 |
retry the operation by coercing to higher generality" |
1144 |
||
1145 |
^ aFixedPoint retry:#- coercing:self |
|
1146 |
||
21797 | 1147 |
"Created: / 05-11-1996 / 14:55:51 / cg" |
1148 |
"Modified (comment): / 12-06-2017 / 20:34:33 / cg" |
|
1888 | 1149 |
! |
1150 |
||
701 | 1151 |
differenceFromFloat:aFloat |
21797 | 1152 |
"aFloat does not know how to subtract the receiver - |
701 | 1153 |
retry the operation by coercing to higher generality" |
1 | 1154 |
|
701 | 1155 |
^ aFloat retry:#- coercing:self |
21797 | 1156 |
|
1157 |
"Modified (comment): / 12-06-2017 / 20:34:23 / cg" |
|
1 | 1158 |
! |
1159 |
||
701 | 1160 |
differenceFromFraction:aFraction |
21797 | 1161 |
"aFraction does not know how to subtract the receiver - |
701 | 1162 |
retry the operation by coercing to higher generality" |
1 | 1163 |
|
701 | 1164 |
^ aFraction retry:#- coercing:self |
21797 | 1165 |
|
1166 |
"Modified (comment): / 12-06-2017 / 20:34:16 / cg" |
|
1 | 1167 |
! |
1168 |
||
701 | 1169 |
differenceFromInteger:anInteger |
21797 | 1170 |
"anInteger does not know how to subtract the receiver - |
701 | 1171 |
retry the operation by coercing to higher generality" |
1 | 1172 |
|
701 | 1173 |
^ anInteger retry:#- coercing:self |
21797 | 1174 |
|
1175 |
"Modified (comment): / 12-06-2017 / 20:34:07 / cg" |
|
1 | 1176 |
! |
1177 |
||
7440 | 1178 |
differenceFromLargeFloat:aLargeFloat |
21797 | 1179 |
"aLargeFloat does not know how to subtract the receiver - |
7440 | 1180 |
retry the operation by coercing to higher generality" |
1181 |
||
1182 |
^ aLargeFloat retry:#- coercing:self |
|
21797 | 1183 |
|
1184 |
"Modified (comment): / 12-06-2017 / 20:34:00 / cg" |
|
7440 | 1185 |
! |
1186 |
||
7399 | 1187 |
differenceFromLongFloat:aLongFloat |
21797 | 1188 |
"aLongFloat does not know how to subtract the receiver - |
7399 | 1189 |
retry the operation by coercing to higher generality" |
1190 |
||
1191 |
^ aLongFloat retry:#- coercing:self |
|
1192 |
||
21797 | 1193 |
"Created: / 17-04-1996 / 12:33:16 / cg" |
1194 |
"Modified (comment): / 12-06-2017 / 20:33:50 / cg" |
|
7399 | 1195 |
! |
1196 |
||
1200 | 1197 |
differenceFromShortFloat:aShortFloat |
21797 | 1198 |
"aShortFloat does not know how to subtract the receiver - |
1200 | 1199 |
retry the operation by coercing to higher generality" |
1200 |
||
1201 |
^ aShortFloat retry:#- coercing:self |
|
1202 |
||
21797 | 1203 |
"Created: / 17-04-1996 / 12:33:16 / cg" |
1204 |
"Modified (comment): / 12-06-2017 / 20:33:43 / cg" |
|
1200 | 1205 |
! |
1206 |
||
7355 | 1207 |
equalFromComplex:aComplex |
21797 | 1208 |
"aComplex does not know how to compare to the receiver - |
7355 | 1209 |
retry the operation by coercing to higher generality" |
1210 |
||
1211 |
^ aComplex retry:#= coercing:self |
|
21797 | 1212 |
|
1213 |
"Modified (comment): / 12-06-2017 / 20:33:26 / cg" |
|
7355 | 1214 |
! |
1215 |
||
1216 |
equalFromFixedPoint:aFixedPoint |
|
21797 | 1217 |
"aFixedPoint does not know how to compare to the receiver - |
7355 | 1218 |
retry the operation by coercing to higher generality" |
1219 |
||
1220 |
^ aFixedPoint retry:#= coercing:self |
|
21797 | 1221 |
|
1222 |
"Modified (comment): / 12-06-2017 / 20:33:17 / cg" |
|
7355 | 1223 |
! |
1224 |
||
1225 |
equalFromFloat:aFloat |
|
21797 | 1226 |
"aFloat does not know how to compare to the receiver - |
7355 | 1227 |
retry the operation by coercing to higher generality" |
1228 |
||
1229 |
^ aFloat retry:#= coercing:self |
|
21797 | 1230 |
|
1231 |
"Modified (comment): / 12-06-2017 / 20:33:09 / cg" |
|
7355 | 1232 |
! |
1233 |
||
1234 |
equalFromFraction:aFraction |
|
21797 | 1235 |
"aFraction does not know how to compare to the receiver - |
7355 | 1236 |
retry the operation by coercing to higher generality" |
1237 |
||
1238 |
^ aFraction retry:#= coercing:self |
|
21797 | 1239 |
|
1240 |
"Modified (comment): / 12-06-2017 / 20:33:01 / cg" |
|
7355 | 1241 |
! |
1242 |
||
1243 |
equalFromInteger:anInteger |
|
21797 | 1244 |
"anInteger does not know how to compare to the receiver - |
7355 | 1245 |
retry the operation by coercing to higher generality" |
1246 |
||
1247 |
^ anInteger retry:#= coercing:self |
|
21797 | 1248 |
|
1249 |
"Modified (comment): / 12-06-2017 / 20:32:53 / cg" |
|
7355 | 1250 |
! |
1251 |
||
7440 | 1252 |
equalFromLargeFloat:aLargeFloat |
21797 | 1253 |
"aLargeFloat does not know how to compare to the receiver - |
7440 | 1254 |
retry the operation by coercing to higher generality" |
1255 |
||
1256 |
^ aLargeFloat retry:#= coercing:self |
|
21797 | 1257 |
|
1258 |
"Modified (comment): / 12-06-2017 / 20:32:45 / cg" |
|
7440 | 1259 |
! |
1260 |
||
7355 | 1261 |
equalFromLongFloat:aLongFloat |
21797 | 1262 |
"aLongFloat does not know how to compare to the receiver - |
7355 | 1263 |
retry the operation by coercing to higher generality" |
1264 |
||
1265 |
^ aLongFloat retry:#= coercing:self |
|
21797 | 1266 |
|
1267 |
"Modified (comment): / 12-06-2017 / 20:32:36 / cg" |
|
7355 | 1268 |
! |
1269 |
||
1270 |
equalFromShortFloat:aShortFloat |
|
21797 | 1271 |
"aShortFloat does not know how to compare to the receiver - |
7355 | 1272 |
retry the operation by coercing to higher generality" |
1273 |
||
1274 |
^ aShortFloat retry:#= coercing:self |
|
21797 | 1275 |
|
1276 |
"Modified (comment): / 12-06-2017 / 20:32:28 / cg" |
|
7355 | 1277 |
! |
1278 |
||
13007 | 1279 |
integerQuotientFromInteger:anInteger |
1280 |
"anInteger does not know how to divide by the receiver - |
|
1281 |
retry the operation by coercing to higher generality" |
|
1282 |
||
1283 |
^ anInteger retry:#// coercing:self |
|
1284 |
||
1285 |
"Created: / 09-08-2010 / 19:49:26 / cg" |
|
1286 |
! |
|
1287 |
||
16657 | 1288 |
isAlmostEqualToFromFloat:aFloat nEpsilon:nE |
1289 |
"the receiver does not know how to compare to a short float - |
|
1290 |
retry the operation by coercing to higher generality" |
|
1291 |
||
1292 |
^ aFloat retry:#isAlmostEqualTo:nEpsilon: coercing:self with:nE |
|
1293 |
! |
|
1294 |
||
1295 |
isAlmostEqualToFromShortFloat:aShortFloat nEpsilon:nE |
|
1296 |
"the receiver does not know how to compare to a short float - |
|
1297 |
retry the operation by coercing to higher generality" |
|
1298 |
||
1299 |
^ aShortFloat retry:#isAlmostEqualTo:nEpsilon: coercing:self with:nE |
|
1300 |
! |
|
1301 |
||
1888 | 1302 |
lessFromFixedPoint:aFixedPoint |
21797 | 1303 |
"aFixedPoint does not know how to compare to the receiver - |
18860 | 1304 |
Return true if aFixedPoint < self. |
1888 | 1305 |
retry the operation by coercing to higher generality" |
1306 |
||
1307 |
^ aFixedPoint retry:#< coercing:self |
|
1308 |
||
21797 | 1309 |
"Created: / 05-11-1996 / 14:56:12 / cg" |
1310 |
"Modified (comment): / 12-06-2017 / 20:32:12 / cg" |
|
1888 | 1311 |
! |
1312 |
||
701 | 1313 |
lessFromFloat:aFloat |
21797 | 1314 |
"aFloat does not know how to compare to the receiver - |
18860 | 1315 |
Return true if aFloat < self. |
701 | 1316 |
retry the operation by coercing to higher generality" |
1 | 1317 |
|
701 | 1318 |
^ aFloat retry:#< coercing:self |
21797 | 1319 |
|
1320 |
"Modified (comment): / 12-06-2017 / 20:32:02 / cg" |
|
1 | 1321 |
! |
1322 |
||
701 | 1323 |
lessFromFraction:aFraction |
21797 | 1324 |
"aFraction does not know how to compare to the receiver - |
18860 | 1325 |
Return true if aFraction < self. |
701 | 1326 |
retry the operation by coercing to higher generality" |
1 | 1327 |
|
701 | 1328 |
^ aFraction retry:#< coercing:self |
21797 | 1329 |
|
1330 |
"Modified (comment): / 12-06-2017 / 20:31:53 / cg" |
|
1 | 1331 |
! |
1332 |
||
701 | 1333 |
lessFromInteger:anInteger |
21797 | 1334 |
"anInteger does not know how to compare to the receiver - |
18860 | 1335 |
Return true if anInteger < self. |
701 | 1336 |
retry the operation by coercing to higher generality" |
1 | 1337 |
|
701 | 1338 |
^ anInteger retry:#< coercing:self |
21797 | 1339 |
|
1340 |
"Modified (comment): / 12-06-2017 / 20:31:44 / cg" |
|
1 | 1341 |
! |
1342 |
||
7440 | 1343 |
lessFromLargeFloat:aLargeFloat |
21797 | 1344 |
"aLargeFloat does not know how to compare to the receiver - |
18860 | 1345 |
Return true if aLargeFloat < self. |
7440 | 1346 |
retry the operation by coercing to higher generality" |
1347 |
||
1348 |
^ aLargeFloat retry:#< coercing:self |
|
21797 | 1349 |
|
1350 |
"Modified (comment): / 12-06-2017 / 20:31:36 / cg" |
|
7440 | 1351 |
! |
1352 |
||
7399 | 1353 |
lessFromLongFloat:aLongFloat |
21797 | 1354 |
"aLongFloat does not know how to compare to the receiver - |
18860 | 1355 |
Return true if aLongFloat < self. |
7399 | 1356 |
retry the operation by coercing to higher generality" |
1357 |
||
1358 |
^ aLongFloat retry:#< coercing:self |
|
1359 |
||
21797 | 1360 |
"Modified: / 17-04-1996 / 12:33:33 / cg" |
1361 |
"Modified (comment): / 12-06-2017 / 20:31:28 / cg" |
|
7399 | 1362 |
! |
1363 |
||
1200 | 1364 |
lessFromShortFloat:aShortFloat |
21797 | 1365 |
"aShortFloat does not know how to compare to the receiver - |
18860 | 1366 |
Return true if aShortFloat < self. |
1200 | 1367 |
retry the operation by coercing to higher generality" |
1368 |
||
1369 |
^ aShortFloat retry:#< coercing:self |
|
1370 |
||
21797 | 1371 |
"Modified: / 17-04-1996 / 12:33:33 / cg" |
1372 |
"Modified (comment): / 12-06-2017 / 20:31:18 / cg" |
|
1200 | 1373 |
! |
1374 |
||
14002 | 1375 |
moduloFromInteger:anInteger |
1376 |
"anInteger does not know how to compute the modulo from the receiver - |
|
1377 |
retry the operation by coercing to higher generality" |
|
1378 |
||
1379 |
^ anInteger retry:#\\ coercing:self |
|
1380 |
||
1381 |
"Created: / 12-02-2012 / 20:38:13 / cg" |
|
1382 |
! |
|
1383 |
||
7355 | 1384 |
productFromComplex:aComplex |
21797 | 1385 |
"aComplex does not know how to multiply the receiver - |
7355 | 1386 |
retry the operation by coercing to higher generality" |
1387 |
||
1388 |
^ aComplex retry:#* coercing:self |
|
21797 | 1389 |
|
1390 |
"Modified (comment): / 12-06-2017 / 20:31:06 / cg" |
|
7355 | 1391 |
! |
1392 |
||
1888 | 1393 |
productFromFixedPoint:aFixedPoint |
21797 | 1394 |
"aFixedPoint does not know how to multiply the receiver - |
1888 | 1395 |
retry the operation by coercing to higher generality" |
1396 |
||
1397 |
^ aFixedPoint retry:#* coercing:self |
|
1398 |
||
21797 | 1399 |
"Created: / 05-11-1996 / 14:56:28 / cg" |
1400 |
"Modified (comment): / 12-06-2017 / 20:30:56 / cg" |
|
1888 | 1401 |
! |
1402 |
||
701 | 1403 |
productFromFloat:aFloat |
21797 | 1404 |
"aFloat does not know how to multiply the receiver - |
701 | 1405 |
retry the operation by coercing to higher generality" |
1 | 1406 |
|
701 | 1407 |
^ aFloat retry:#* coercing:self |
21797 | 1408 |
|
1409 |
"Modified (comment): / 12-06-2017 / 20:30:46 / cg" |
|
1 | 1410 |
! |
1411 |
||
701 | 1412 |
productFromFraction:aFraction |
21797 | 1413 |
"aFraction does not know how to multiply the receiver - |
701 | 1414 |
retry the operation by coercing to higher generality" |
1 | 1415 |
|
701 | 1416 |
^ aFraction retry:#* coercing:self |
21797 | 1417 |
|
1418 |
"Modified (comment): / 12-06-2017 / 20:30:38 / cg" |
|
1 | 1419 |
! |
1420 |
||
701 | 1421 |
productFromInteger:anInteger |
21797 | 1422 |
"anInteger does not know how to multiply the receiver - |
701 | 1423 |
retry the operation by coercing to higher generality" |
1 | 1424 |
|
701 | 1425 |
^ anInteger retry:#* coercing:self |
21797 | 1426 |
|
1427 |
"Modified (comment): / 12-06-2017 / 20:29:21 / cg" |
|
1 | 1428 |
! |
1429 |
||
7440 | 1430 |
productFromLargeFloat:aLargeFloat |
21797 | 1431 |
"aLargeFloat does not know how to multiply the receiver - |
7440 | 1432 |
retry the operation by coercing to higher generality" |
1433 |
||
1434 |
^ aLargeFloat retry:#* coercing:self |
|
1435 |
||
21797 | 1436 |
"Created: / 17-04-1996 / 12:33:48 / cg" |
1437 |
"Modified (comment): / 12-06-2017 / 20:29:13 / cg" |
|
7440 | 1438 |
! |
1439 |
||
7399 | 1440 |
productFromLongFloat:aLongFloat |
21797 | 1441 |
"aLongFloat does not know how to multiply the receiver - |
7399 | 1442 |
retry the operation by coercing to higher generality" |
1443 |
||
1444 |
^ aLongFloat retry:#* coercing:self |
|
1445 |
||
21797 | 1446 |
"Created: / 17-04-1996 / 12:33:48 / cg" |
1447 |
"Modified (comment): / 12-06-2017 / 20:29:05 / cg" |
|
7399 | 1448 |
! |
1449 |
||
1200 | 1450 |
productFromShortFloat:aShortFloat |
21797 | 1451 |
"aShortFloat does not know how to multiply the receiver - |
1200 | 1452 |
retry the operation by coercing to higher generality" |
1453 |
||
1454 |
^ aShortFloat retry:#* coercing:self |
|
1455 |
||
21797 | 1456 |
"Created: / 17-04-1996 / 12:33:48 / cg" |
1457 |
"Modified (comment): / 12-06-2017 / 20:28:56 / cg" |
|
1200 | 1458 |
! |
1459 |
||
7355 | 1460 |
quotientFromComplex:aComplex |
7440 | 1461 |
"aComplex does not know how to divide by the receiver - |
7355 | 1462 |
retry the operation by coercing to higher generality" |
1463 |
||
1464 |
^ aComplex retry:#/ coercing:self |
|
1465 |
! |
|
1466 |
||
1888 | 1467 |
quotientFromFixedPoint:aFixedPoint |
7440 | 1468 |
"aFixedPoint does not know how to divide by the receiver - |
1888 | 1469 |
retry the operation by coercing to higher generality" |
1470 |
||
1471 |
^ aFixedPoint retry:#/ coercing:self |
|
1472 |
||
1473 |
"Created: 5.11.1996 / 14:56:41 / cg" |
|
1474 |
! |
|
1475 |
||
701 | 1476 |
quotientFromFloat:aFloat |
7440 | 1477 |
"aFloat does not know how to divide by the receiver - |
701 | 1478 |
retry the operation by coercing to higher generality" |
1 | 1479 |
|
701 | 1480 |
^ aFloat retry:#/ coercing:self |
1 | 1481 |
! |
1482 |
||
701 | 1483 |
quotientFromFraction:aFraction |
7440 | 1484 |
"aFraction does not know how to divide by the receiver - |
701 | 1485 |
retry the operation by coercing to higher generality" |
1 | 1486 |
|
701 | 1487 |
^ aFraction retry:#/ coercing:self |
1 | 1488 |
! |
1489 |
||
21797 | 1490 |
quotientFromInteger:aQDouble |
1491 |
"aQDouble does not know how to divide by the receiver - |
|
1 | 1492 |
retry the operation by coercing to higher generality" |
1493 |
||
21797 | 1494 |
^ aQDouble retry:#/ coercing:self |
1495 |
||
1496 |
"Modified (comment): / 12-06-2017 / 20:35:45 / cg" |
|
1 | 1497 |
! |
1498 |
||
7440 | 1499 |
quotientFromLargeFloat:aLargeFloat |
1500 |
"aLargeFloat does not know how to divide by the receiver - |
|
1501 |
retry the operation by coercing to higher generality" |
|
1502 |
||
1503 |
^ aLargeFloat retry:#/ coercing:self |
|
1504 |
||
1505 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
1506 |
! |
|
1507 |
||
7399 | 1508 |
quotientFromLongFloat:aLongFloat |
7440 | 1509 |
"aLongFloat does not know how to divide by the receiver - |
7399 | 1510 |
retry the operation by coercing to higher generality" |
1511 |
||
1512 |
^ aLongFloat retry:#/ coercing:self |
|
1513 |
||
1514 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
1515 |
! |
|
1516 |
||
1200 | 1517 |
quotientFromShortFloat:aShortFloat |
7440 | 1518 |
"aShortFloat does not know how to divide by the receiver - |
1200 | 1519 |
retry the operation by coercing to higher generality" |
1520 |
||
1521 |
^ aShortFloat retry:#/ coercing:self |
|
1522 |
||
1523 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
1524 |
! |
|
1525 |
||
21935 | 1526 |
raisedFromFloat:aFloat |
1527 |
"aFloat does not know how to be raised to the receiver" |
|
1528 |
||
1529 |
self isNumber ifTrue:[ |
|
1530 |
self isFloat ifFalse:[ |
|
1531 |
^ self asFloat raisedFromFloat:aFloat |
|
1532 |
]. |
|
1533 |
]. |
|
1534 |
^ self raisedFromNumber:aFloat |
|
1535 |
||
1536 |
"Created: / 01-07-2017 / 21:18:54 / cg" |
|
1537 |
"Modified: / 02-07-2017 / 00:44:47 / cg" |
|
1538 |
! |
|
1539 |
||
1540 |
raisedFromNumber:aNumber |
|
21967 | 1541 |
"aNumber does not know how to be raised to the receiver |
1542 |
(i.e. how to compute aNumber^self)" |
|
21935 | 1543 |
|
1544 |
^ self class |
|
1545 |
raise:#domainErrorSignal |
|
1546 |
receiver:aNumber |
|
1547 |
selector:#raisedTo: |
|
1548 |
arguments:(Array with:self) |
|
1549 |
errorString:'bad receiver/arg in raisedTo:' |
|
1550 |
||
1551 |
"Created: / 01-07-2017 / 21:17:06 / cg" |
|
21967 | 1552 |
"Modified (comment): / 03-07-2017 / 14:06:53 / cg" |
21935 | 1553 |
! |
1554 |
||
14747 | 1555 |
remainderFromFloat:aFloat |
1556 |
"aFloat does not know how to compute the remainder with the receiver - |
|
1557 |
retry the operation by coercing to higher generality" |
|
1558 |
||
1559 |
^ aFloat retry:#rem: coercing:self |
|
1560 |
! |
|
1561 |
||
1562 |
remainderFromShortFloat:aShortFloat |
|
1563 |
"aShortFloat does not know how to compute the remainder with the receiver - |
|
1564 |
retry the operation by coercing to higher generality" |
|
1565 |
||
1566 |
^ aShortFloat retry:#rem: coercing:self |
|
1567 |
! |
|
1568 |
||
7355 | 1569 |
sumFromComplex:aComplex |
21797 | 1570 |
"aComplex does not know how to add the receiver - |
7355 | 1571 |
retry the operation by coercing to higher generality" |
1572 |
||
1573 |
^ aComplex retry:#+ coercing:self |
|
21797 | 1574 |
|
1575 |
"Modified (comment): / 12-06-2017 / 20:27:25 / cg" |
|
7355 | 1576 |
! |
1577 |
||
1888 | 1578 |
sumFromFixedPoint:aFixedPoint |
21797 | 1579 |
"aFixedPoint does not know how to add the receiver - |
1888 | 1580 |
retry the operation by coercing to higher generality" |
1581 |
||
1582 |
^ aFixedPoint retry:#+ coercing:self |
|
1583 |
||
21797 | 1584 |
"Created: / 05-11-1996 / 14:56:56 / cg" |
1585 |
"Modified (comment): / 12-06-2017 / 20:27:38 / cg" |
|
1888 | 1586 |
! |
1587 |
||
1 | 1588 |
sumFromFloat:aFloat |
21797 | 1589 |
"aFloat does not know how to add the receiver - |
1 | 1590 |
retry the operation by coercing to higher generality" |
1591 |
||
1592 |
^ aFloat retry:#+ coercing:self |
|
21797 | 1593 |
|
1594 |
"Modified (comment): / 12-06-2017 / 20:27:47 / cg" |
|
1 | 1595 |
! |
1596 |
||
1597 |
sumFromFraction:aFraction |
|
21797 | 1598 |
"aFraction does not know how to add the receiver - |
1 | 1599 |
retry the operation by coercing to higher generality" |
1600 |
||
1601 |
^ aFraction retry:#+ coercing:self |
|
21797 | 1602 |
|
1603 |
"Modified (comment): / 12-06-2017 / 20:27:55 / cg" |
|
1 | 1604 |
! |
1605 |
||
701 | 1606 |
sumFromInteger:anInteger |
21797 | 1607 |
"anInteger does not know how to add the receiver - |
1 | 1608 |
retry the operation by coercing to higher generality" |
1609 |
||
701 | 1610 |
^ anInteger retry:#+ coercing:self |
21797 | 1611 |
|
1612 |
"Modified (comment): / 12-06-2017 / 20:28:03 / cg" |
|
1200 | 1613 |
! |
1614 |
||
7440 | 1615 |
sumFromLargeFloat:aLargeFloat |
21797 | 1616 |
"aLargeFloat does not know how to add the receiver - |
7440 | 1617 |
retry the operation by coercing to higher generality" |
1618 |
||
1619 |
^ aLargeFloat retry:#+ coercing:self |
|
1620 |
||
21797 | 1621 |
"Created: / 17-04-1996 / 12:34:10 / cg" |
1622 |
"Modified (comment): / 12-06-2017 / 20:28:11 / cg" |
|
7440 | 1623 |
! |
1624 |
||
7399 | 1625 |
sumFromLongFloat:aLongFloat |
21797 | 1626 |
"aLongFloat does not know how to add the receiver - |
7399 | 1627 |
retry the operation by coercing to higher generality" |
1628 |
||
1629 |
^ aLongFloat retry:#+ coercing:self |
|
1630 |
||
21797 | 1631 |
"Created: / 17-04-1996 / 12:34:10 / cg" |
1632 |
"Modified (comment): / 12-06-2017 / 20:28:19 / cg" |
|
7399 | 1633 |
! |
1634 |
||
1200 | 1635 |
sumFromShortFloat:aShortFloat |
21797 | 1636 |
"aShortFloat does not know how to add the receiver - |
1200 | 1637 |
retry the operation by coercing to higher generality" |
1638 |
||
1639 |
^ aShortFloat retry:#+ coercing:self |
|
1640 |
||
21797 | 1641 |
"Created: / 17-04-1996 / 12:34:10 / cg" |
1642 |
"Modified (comment): / 12-06-2017 / 20:28:39 / cg" |
|
1 | 1643 |
! ! |
1644 |
||
7384 | 1645 |
!ArithmeticValue methodsFor:'mathematical functions'! |
1 | 1646 |
|
10326 | 1647 |
** aNumber |
9443 | 1648 |
"Answer the receiver raised to the power of the argument, aNumber." |
1649 |
"same as Number>>raisedTo:" |
|
1650 |
||
1651 |
^self raisedTo: aNumber |
|
1652 |
||
1653 |
"Created: / 15-07-2006 / 15:11:05 / cg" |
|
1654 |
"Modified: / 17-07-2006 / 12:51:33 / cg" |
|
1655 |
! |
|
1656 |
||
18860 | 1657 |
basicRaisedToInteger:exp |
21804 | 1658 |
"return the receiver raised to exp. |
1659 |
Warning: if the receiver is a float/double, |
|
1660 |
currently INF may be returned on overflow. |
|
1661 |
This may be changed silently to raise an error in future versions." |
|
18860 | 1662 |
|
1663 |
|result e t| |
|
1664 |
||
1665 |
"use the addition chaining algorithm, |
|
1666 |
which is much faster for big exponents" |
|
6858 | 1667 |
|
18860 | 1668 |
result := 1. |
1669 |
t := self. |
|
1670 |
exp < 0 ifTrue:[ |
|
1671 |
e := exp negated. |
|
1672 |
] ifFalse:[ |
|
1673 |
e := exp. |
|
1674 |
]. |
|
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1675 |
|
18860 | 1676 |
[e ~~ 0] whileTrue:[ |
1677 |
[(e bitAnd:1) == 0] whileTrue:[ |
|
1678 |
e := e bitShift:-1. |
|
1679 |
t := t * t. |
|
1680 |
]. |
|
1681 |
e := e - 1. |
|
1682 |
result := result * t. |
|
1683 |
]. |
|
1684 |
||
1685 |
(exp < 0) ifTrue:[ |
|
1686 |
^ 1 / result |
|
1687 |
]. |
|
1688 |
||
1689 |
^ result |
|
18835 | 1690 |
|
1691 |
" |
|
1692 |
(2.0 raisedToInteger:216) |
|
1693 |
(2 raisedToInteger:216) |
|
1694 |
(2 raisedTo:216) |
|
1695 |
-> 105312291668557186697918027683670432318895095400549111254310977536 |
|
1696 |
||
1697 |
(2 raisedToInteger:216) asFloat |
|
1698 |
(2 raisedTo:216) asFloat |
|
1699 |
-> 1.05312E+65 |
|
1700 |
||
1701 |
(2 raisedToInteger:500) |
|
1702 |
(2 raisedTo:500) |
|
1703 |
-> 3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376 |
|
18860 | 1704 |
(2 raisedTo:-500) |
1705 |
-> (1/3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376) |
|
18835 | 1706 |
2 raisedToInteger:10 |
1707 |
-> 1024 |
|
1708 |
-2 raisedToInteger:10 |
|
1709 |
-> 1024 |
|
1710 |
-2 raisedToInteger:9 |
|
1711 |
-> -512 |
|
1712 |
10 raisedToInteger:-10 |
|
1713 |
-> (1/10000000000) |
|
1714 |
2 raisedToInteger:0 |
|
1715 |
-> 1 |
|
1716 |
2 raisedToInteger:-1 |
|
1717 |
-> (1/2) |
|
1718 |
||
1719 |
Time millisecondsToRun:[ |
|
1720 |
10000 timesRepeat:[ |
|
1721 |
(2 raisedToInteger:500) |
|
1722 |
] |
|
1723 |
] |
|
1724 |
||
1725 |
Time millisecondsToRun:[ |
|
1726 |
|bigNum| |
|
1727 |
bigNum := 2 raisedToInteger:500. |
|
1728 |
10 timesRepeat:[ |
|
1729 |
(bigNum raisedToInteger:500) |
|
1730 |
] |
|
1731 |
] |
|
1732 |
" |
|
1733 |
||
21804 | 1734 |
"Created: / 27-04-1999 / 15:19:22 / stefan" |
1735 |
"Modified (comment): / 13-06-2017 / 17:35:41 / cg" |
|
18835 | 1736 |
! |
1737 |
||
18860 | 1738 |
raisedTo: aNumber |
21935 | 1739 |
"return the receiver raised to aNumber (i.e. self ^ aNumber)" |
1740 |
||
18860 | 1741 |
aNumber isInteger ifTrue:[ |
21935 | 1742 |
^ self raisedToInteger:aNumber |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1743 |
]. |
23158 | 1744 |
self raisedFromNumber:aNumber. "/ this raises an error. |
21935 | 1745 |
|
1746 |
" |
|
1747 |
2 raisedTo:16 |
|
1748 |
2.0 raisedTo:16 |
|
1749 |
||
1750 |
3 raisedTo:4 |
|
23158 | 1751 |
10@10 raisedTo:4 |
1752 |
10@10 raisedTo:4.0 |
|
21935 | 1753 |
" |
1754 |
||
1755 |
"Modified (format): / 01-07-2017 / 20:59:35 / cg" |
|
18860 | 1756 |
! |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1757 |
|
18860 | 1758 |
raisedToInteger:exp |
1759 |
"return the receiver raised to exp. |
|
21804 | 1760 |
Warning: if the receiver is a float/double, |
1761 |
currently INF may be returned on overflow. |
|
1762 |
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
|
1763 |
|
18860 | 1764 |
^ self basicRaisedToInteger:exp |
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 |
" |
18860 | 1767 |
(2.0 raisedToInteger:10000) |
1768 |
(2 raisedToInteger:10000) |
|
1769 |
(2 raisedToInteger:-10000) |
|
1770 |
||
17662 | 1771 |
(2.0 raisedToInteger:216) |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1772 |
(2 raisedToInteger:216) |
10326 | 1773 |
(2 raisedTo:216) |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1774 |
-> 105312291668557186697918027683670432318895095400549111254310977536 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1775 |
|
10326 | 1776 |
(2 raisedToInteger:216) asFloat |
1777 |
(2 raisedTo:216) asFloat |
|
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1778 |
-> 1.05312E+65 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1779 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1780 |
(2 raisedToInteger:500) |
10326 | 1781 |
(2 raisedTo:500) |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1782 |
-> 3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376 |
10326 | 1783 |
2 raisedToInteger:10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1784 |
-> 1024 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1785 |
-2 raisedToInteger:10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1786 |
-> 1024 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1787 |
-2 raisedToInteger:9 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1788 |
-> -512 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1789 |
10 raisedToInteger:-10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1790 |
-> (1/10000000000) |
10326 | 1791 |
2 raisedToInteger:0 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1792 |
-> 1 |
10326 | 1793 |
2 raisedToInteger:-1 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1794 |
-> (1/2) |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1795 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1796 |
Time millisecondsToRun:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1797 |
10000 timesRepeat:[ |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1798 |
(2 raisedToInteger:500) |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1799 |
] |
10326 | 1800 |
] |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1801 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1802 |
Time millisecondsToRun:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1803 |
|bigNum| |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1804 |
bigNum := 2 raisedToInteger:500. |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1805 |
10 timesRepeat:[ |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1806 |
(bigNum raisedToInteger:500) |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1807 |
] |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1808 |
] |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1809 |
" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1810 |
|
21804 | 1811 |
"Created: / 27-04-1999 / 15:19:22 / stefan" |
1812 |
"Modified (comment): / 13-06-2017 / 17:35:20 / cg" |
|
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1813 |
! |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1814 |
|
701 | 1815 |
squared |
1816 |
"return receiver * receiver" |
|
1817 |
||
1818 |
^ self * self |
|
1819 |
! ! |
|
1820 |
||
23499 | 1821 |
|
701 | 1822 |
!ArithmeticValue methodsFor:'queries'! |
1823 |
||
1824 |
respondsToArithmetic |
|
1825 |
"return true, if the receiver responds to arithmetic messages" |
|
1826 |
||
1827 |
^ true |
|
1828 |
! ! |
|
1829 |
||
1830 |
!ArithmeticValue methodsFor:'testing'! |
|
1831 |
||
1832 |
denominator |
|
1833 |
"return the denominator of the receiver" |
|
1834 |
||
1835 |
^ 1 |
|
1836 |
! |
|
1837 |
||
1838 |
even |
|
17223 | 1839 |
"return true if the receiver is divisible by 2. |
21967 | 1840 |
This is only defined for whole-numbers (integers)." |
701 | 1841 |
|
17223 | 1842 |
^ self class |
21967 | 1843 |
raise:#domainErrorSignal |
17223 | 1844 |
receiver:self |
1845 |
selector:#even |
|
1846 |
arguments:#() |
|
1847 |
errorString:'Receiver must be a whole-number' |
|
1848 |
||
21967 | 1849 |
"/ No no no, the following is bad: |
1850 |
"/ ^ self truncated asInteger even |
|
17223 | 1851 |
|
1852 |
" |
|
21967 | 1853 |
2.4 even |
1854 |
2.0 even |
|
17223 | 1855 |
" |
21967 | 1856 |
|
1857 |
"Modified (comment): / 03-07-2017 / 14:10:01 / cg" |
|
701 | 1858 |
! |
1859 |
||
3635 | 1860 |
isComplex |
7355 | 1861 |
"Answer whether the receiver has an imaginary part |
1862 |
(i.e. if it is a complex number). Always false here." |
|
3635 | 1863 |
|
1864 |
^ false |
|
1865 |
||
1866 |
"Modified: / 9.7.1998 / 10:19:27 / cg" |
|
1867 |
! |
|
1868 |
||
8979
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1869 |
isFinite |
10326 | 1870 |
"return true, if the receiver is finite |
9400 | 1871 |
i.e. it can be represented as a rational number." |
1872 |
||
8979
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1873 |
^ true |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1874 |
! |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1875 |
|
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1876 |
isInfinite |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1877 |
^ false |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1878 |
! |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1879 |
|
7440 | 1880 |
isNegativeInfinity |
1881 |
^ self negative and:[self isInfinite] |
|
1882 |
! |
|
1883 |
||
1884 |
isNegativeZero |
|
1885 |
"return false - must be redefined by subclasses which can represent a negative zero |
|
1886 |
(i.e. limitedPrecisionReal classes)" |
|
1887 |
||
1888 |
^ false |
|
1889 |
! |
|
1890 |
||
1891 |
isPositiveInfinity |
|
1892 |
^ self positive and:[self isInfinite] |
|
1893 |
! |
|
1894 |
||
6156 | 1895 |
isReal |
1896 |
"return true, if the receiver is some kind of real number (as opposed to a complex); |
|
1897 |
false is returned here - the method is only redefined in Number (and Complex)." |
|
1898 |
||
1899 |
^ false |
|
1900 |
||
1901 |
"Modified: / 6.11.2001 / 13:20:20 / cg" |
|
1902 |
"Created: / 6.11.2001 / 13:26:29 / cg" |
|
1903 |
! |
|
1904 |
||
7440 | 1905 |
isZero |
1906 |
"return false - must be redefined by subclasses which can represent a negative zero |
|
1907 |
(i.e. limitedPrecisionReal classes)" |
|
701 | 1908 |
|
7440 | 1909 |
^ self = self class zero |
1910 |
! |
|
1911 |
||
1912 |
negative |
|
18860 | 1913 |
"return true if the receiver is less than zero." |
7440 | 1914 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
1915 |
^ self < self class zero |
701 | 1916 |
! |
1917 |
||
1918 |
numerator |
|
1919 |
"return the numerator of the receiver." |
|
1920 |
||
1921 |
^ self |
|
1922 |
! |
|
1923 |
||
1924 |
odd |
|
1925 |
"return true if the receiver is not divisible by 2" |
|
1926 |
||
1927 |
^ self even not |
|
1928 |
! |
|
1929 |
||
1930 |
positive |
|
18860 | 1931 |
"return true, if the receiver is greater or equal to zero (not negative)" |
701 | 1932 |
|
1933 |
^ self negative not |
|
1934 |
! |
|
1935 |
||
1936 |
sign |
|
7471 | 1937 |
"return the sign of the receiver (-1, 0 or 1)" |
701 | 1938 |
|
7440 | 1939 |
|zero| |
1940 |
||
1941 |
zero := self class zero. |
|
1942 |
(self < zero) ifTrue:[^ -1]. |
|
1943 |
(zero < self) ifTrue:[^ 1]. |
|
701 | 1944 |
^ 0 |
1945 |
! |
|
1946 |
||
1947 |
strictlyPositive |
|
18860 | 1948 |
"return true, if the receiver is greater than zero" |
701 | 1949 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
1950 |
^ self class zero < self |
1 | 1951 |
! ! |
1952 |
||
701 | 1953 |
!ArithmeticValue methodsFor:'truncation & rounding'! |
1 | 1954 |
|
701 | 1955 |
ceiling |
1956 |
"return the integer nearest the receiver towards positive infinity." |
|
1 | 1957 |
|
701 | 1958 |
|anInteger| |
1 | 1959 |
|
701 | 1960 |
anInteger := self // 1. "truncates towards negative infinity" |
1961 |
anInteger = self ifTrue:[^ anInteger]. |
|
1962 |
^ anInteger + 1 |
|
1 | 1963 |
! |
1964 |
||
701 | 1965 |
floor |
1966 |
"return the receiver truncated towards negative infinity" |
|
1 | 1967 |
|
701 | 1968 |
^ self // 1 |
1 | 1969 |
! |
1970 |
||
701 | 1971 |
roundTo:aNumber |
1972 |
"return the receiver rounded to multiples of aNumber" |
|
1 | 1973 |
|
701 | 1974 |
^ (self / aNumber) rounded * aNumber |
8449 | 1975 |
|
1976 |
" |
|
10326 | 1977 |
0 roundTo:4 |
8449 | 1978 |
1 roundTo:4 |
1979 |
2 roundTo:4 |
|
1980 |
3 roundTo:4 |
|
1981 |
4 roundTo:4 |
|
1982 |
5 roundTo:4 |
|
1983 |
6 roundTo:4 |
|
1984 |
7 roundTo:4 |
|
14497 | 1985 |
|
1986 |
7.123 roundTo:0.1 |
|
1987 |
7.523 roundTo:0.1 |
|
1988 |
7.583 roundTo:0.1 |
|
1989 |
7.623 roundTo:0.1 |
|
1990 |
7.623 roundTo:0.01 |
|
1991 |
7.628 roundTo:0.01 |
|
8449 | 1992 |
" |
1993 |
! |
|
1994 |
||
1995 |
roundUpTo:aNumber |
|
1996 |
"return the receiver rounded up to the next multiple of aNumber" |
|
1997 |
||
1998 |
^ (self / aNumber) ceiling * aNumber |
|
1999 |
||
2000 |
" |
|
10326 | 2001 |
0 roundUpTo:4 |
2002 |
1 roundUpTo:4 |
|
2003 |
2 roundUpTo:4 |
|
2004 |
3 roundUpTo:4 |
|
2005 |
4 roundUpTo:4 |
|
2006 |
5 roundUpTo:4 |
|
2007 |
6 roundUpTo:4 |
|
2008 |
7 roundUpTo:4 |
|
2009 |
8 roundUpTo:4 |
|
10911 | 2010 |
(3@4) roundUpTo:8 |
2011 |
(3@4) roundUpTo:(5 @ 4) |
|
2012 |
(3@3) roundUpTo:(5 @ 4) |
|
8449 | 2013 |
" |
701 | 2014 |
! |
2015 |
||
2016 |
rounded |
|
2017 |
"return the integer nearest the receiver" |
|
2018 |
||
1881 | 2019 |
self negative ifTrue:[ |
10326 | 2020 |
^ (self - 0.5) ceiling |
1881 | 2021 |
]. |
701 | 2022 |
^ (self + 0.5) floor |
1881 | 2023 |
|
2024 |
"Modified: 5.11.1996 / 11:31:59 / cg" |
|
1 | 2025 |
! |
2026 |
||
701 | 2027 |
truncateTo:aNumber |
2028 |
"return the receiver truncated to multiples of aNumber" |
|
1 | 2029 |
|
19056 | 2030 |
|t| |
2031 |
||
2032 |
aNumber <= 0 ifTrue:[self error:'argument must be positive']. |
|
2033 |
||
2034 |
t := (self / aNumber) floor * aNumber. |
|
2035 |
(aNumber isInteger) ifTrue:[ |
|
2036 |
^ t asInteger |
|
2037 |
]. |
|
2038 |
^ t |
|
2039 |
||
2040 |
" |
|
2041 |
truncate to multiples of 4 |
|
2042 |
123.456 truncateTo:4 |
|
2043 |
124.456 truncateTo:4 |
|
2044 |
truncate to multiples of 2 |
|
2045 |
122.456 truncateTo:2 |
|
2046 |
123.456 truncateTo:2 |
|
2047 |
124.456 truncateTo:2 |
|
2048 |
normal truncate |
|
2049 |
123.456 truncateTo:1 |
|
2050 |
124.456 truncateTo:1 |
|
2051 |
truncate to decimal digits |
|
2052 |
123.456 truncateTo:0.1 |
|
2053 |
123.987 truncateTo:0.1 |
|
2054 |
123.456 truncateTo:0.01 |
|
2055 |
" |
|
1 | 2056 |
! |
2057 |
||
701 | 2058 |
truncated |
2059 |
"return the receiver truncated towards zero" |
|
1 | 2060 |
|
701 | 2061 |
self negative ifTrue:[ |
2062 |
^ self ceiling |
|
2063 |
]. |
|
2064 |
^ self floor |
|
2065 |
! ! |
|
1 | 2066 |
|
701 | 2067 |
!ArithmeticValue class methodsFor:'documentation'! |
2068 |
||
2069 |
version |
|
18835 | 2070 |
^ '$Header$' |
12576 | 2071 |
! |
2072 |
||
2073 |
version_CVS |
|
18835 | 2074 |
^ '$Header$' |
1 | 2075 |
! ! |
6858 | 2076 |
|
14747 | 2077 |
|
701 | 2078 |
ArithmeticValue initialize! |