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