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