author | convert-repo |
Thu, 07 Feb 2013 04:25:37 +0000 | |
changeset 14762 | dbcf4ab2af2e |
parent 14747 | 18be9a3c2324 |
child 15279 | 7dd8da92779e |
child 18025 | 036717d61eda |
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 |
||
1 | 14 |
Magnitude subclass:#ArithmeticValue |
1172 | 15 |
instanceVariableNames:'' |
16 |
classVariableNames:'DivisionByZeroSignal DomainErrorSignal OverflowSignal |
|
6342
fbbe62d5b17c
Remove obsolete (since years) AnyArithmeticSignal definitions.
Stefan Vogel <sv@exept.de>
parents:
6156
diff
changeset
|
17 |
UnderflowSignal ArithmeticSignal UnorderedSignal |
fbbe62d5b17c
Remove obsolete (since years) AnyArithmeticSignal definitions.
Stefan Vogel <sv@exept.de>
parents:
6156
diff
changeset
|
18 |
ConversionErrorSignal' |
1172 | 19 |
poolDictionaries:'' |
20 |
category:'Magnitude-Numbers' |
|
1 | 21 |
! |
22 |
||
701 | 23 |
!ArithmeticValue class methodsFor:'documentation'! |
82 | 24 |
|
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
213 | 28 |
All Rights Reserved |
1 | 29 |
|
82 | 30 |
This software is furnished under a license and may be used |
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
1 | 38 |
|
82 | 39 |
documentation |
40 |
" |
|
41 |
ArithmeticValue is an abstract superclass for all things responding to |
|
155 | 42 |
arithmetic messages. It was inserted into the hierarchy, to allow objects |
43 |
like matrices, functions etc. to share the arithmetic methods defined here. |
|
44 |
||
5955 | 45 |
Notice, that what used to be signals are now exception classes - the class |
46 |
variables and signal accessors remain here for backward compatibility. |
|
47 |
||
1283 | 48 |
[class variables:] |
10326 | 49 |
ArithmeticSignal <Error> parent of all arithmetic signals |
50 |
(never raised itself) |
|
51 |
New: now a reference to ArithmeticError |
|
82 | 52 |
|
10326 | 53 |
DomainErrorSignal <Error> raised upon float errors |
54 |
(for example range in trigonometric) |
|
55 |
New: now a reference to DomainError |
|
1283 | 56 |
|
10326 | 57 |
DivisionByZeroSignal <Error> raised when division by 0 is attempted |
58 |
New: now a reference to ZeroDivide |
|
155 | 59 |
|
10326 | 60 |
OverflowSignal <Error> raised on overflow/underflow conditions |
61 |
UnderflowSignal in float arithmetic. |
|
62 |
Notice: some OperatingSystems do not |
|
63 |
provide enough information for ST/X to |
|
64 |
extract the real reason for the floatException |
|
65 |
thus raising DomainErrorSignal in these cases. |
|
155 | 66 |
|
1295 | 67 |
[author:] |
10326 | 68 |
Claus Gittinger |
1295 | 69 |
|
1283 | 70 |
[See also:] |
10326 | 71 |
Number |
82 | 72 |
" |
73 |
! ! |
|
1 | 74 |
|
701 | 75 |
!ArithmeticValue class methodsFor:'Signal constants'! |
76 |
||
77 |
arithmeticSignal |
|
78 |
"return the parent of all arithmetic signals" |
|
79 |
||
7403 | 80 |
^ ArithmeticError |
701 | 81 |
! |
82 |
||
83 |
divisionByZeroSignal |
|
84 |
"return the signal which is raised on division by zero" |
|
85 |
||
7403 | 86 |
^ ZeroDivide |
701 | 87 |
! |
1 | 88 |
|
89 |
domainErrorSignal |
|
90 |
"return the signal which is raised on math errors |
|
91 |
(such as log of 0 etc.)" |
|
92 |
||
7403 | 93 |
^ DomainError |
94 |
! |
|
95 |
||
96 |
imaginaryResultSignal |
|
97 |
"return the signal which is raised when an imaginary result would be created |
|
98 |
(such as when taking the sqrt of a negative number)" |
|
99 |
||
100 |
^ ImaginaryResultError |
|
1 | 101 |
! |
102 |
||
7440 | 103 |
operationNotPossibleSignal |
104 |
^ Error |
|
105 |
! |
|
106 |
||
1 | 107 |
overflowSignal |
108 |
"return the signal which is raised on overflow conditions (in floats)" |
|
109 |
||
7403 | 110 |
^ OverflowError |
1 | 111 |
! |
112 |
||
5955 | 113 |
rangeErrorSignal |
114 |
"return the parent of the overflow/underflow signals" |
|
115 |
||
116 |
^ RangeError |
|
117 |
! |
|
118 |
||
7440 | 119 |
undefinedResultSignal |
120 |
^ Error |
|
121 |
! |
|
122 |
||
1 | 123 |
underflowSignal |
124 |
"return the signal which is raised on underflow conditions (in floats)" |
|
125 |
||
7403 | 126 |
^ UnderflowError |
1 | 127 |
! |
128 |
||
345 | 129 |
unorderedSignal |
10326 | 130 |
"return the signal which is raised when numbers are compared, |
345 | 131 |
for which no ordering is defined (for example: complex numbers)" |
132 |
||
7403 | 133 |
^ UnorderedNumbersError |
1 | 134 |
! ! |
135 |
||
7440 | 136 |
!ArithmeticValue class methodsFor:'class initialization'! |
137 |
||
138 |
initialize |
|
139 |
"setup the signals" |
|
140 |
||
141 |
"/ notice that we now use class based exceptions; |
|
142 |
"/ however, for backward compatibility, the class variables |
|
143 |
"/ referring to those are kept for a while, for subclass compatibility. |
|
144 |
"/ Please convert your code to access those via the Signal-constants class |
|
145 |
"/ messages (accesors) instead of referring to the class variables. |
|
146 |
||
147 |
ArithmeticSignal := ArithmeticError. |
|
148 |
DomainErrorSignal := DomainError. |
|
149 |
DivisionByZeroSignal := ZeroDivide. |
|
150 |
UnorderedSignal := UnorderedNumbersError. |
|
151 |
ConversionErrorSignal := ConversionError. |
|
152 |
OverflowSignal := OverflowError. |
|
153 |
UnderflowSignal := UnderflowError. |
|
154 |
! ! |
|
155 |
||
156 |
!ArithmeticValue class methodsFor:'constants'! |
|
157 |
||
158 |
NaN |
|
8850 | 159 |
"return the constant NaN (not a Number)." |
160 |
||
7440 | 161 |
^ NotANumber new |
162 |
! |
|
163 |
||
164 |
infinity |
|
8850 | 165 |
"return something which represents infinity (for my instances)" |
166 |
||
7440 | 167 |
^ Infinity positive |
168 |
! |
|
169 |
||
7538
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
170 |
nan |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
171 |
"VW compatibility" |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
172 |
|
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
173 |
^ self NaN |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
174 |
! |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
175 |
|
7440 | 176 |
negativeInfinity |
8850 | 177 |
"return something which represents negative infinity (for my instances)" |
178 |
||
7440 | 179 |
^ Infinity negative |
180 |
! |
|
181 |
||
7726 | 182 |
unity |
11467 | 183 |
"return something which represents the unity element (for my instances). |
184 |
That is the neutral element for multiplication." |
|
8850 | 185 |
|
7726 | 186 |
self subclassResponsibility |
187 |
! |
|
188 |
||
7440 | 189 |
zero |
11467 | 190 |
"return something which represents the zero element (for my instances). |
191 |
That is the neutral element for addition." |
|
8850 | 192 |
|
7440 | 193 |
self subclassResponsibility |
194 |
! ! |
|
195 |
||
8892 | 196 |
!ArithmeticValue class methodsFor:'queries'! |
197 |
||
198 |
isAbstract |
|
11227 | 199 |
"Return if this class is an abstract class. |
200 |
True is returned for ArithmeticValue here; false for subclasses. |
|
201 |
Abstract subclasses must redefine again." |
|
202 |
||
8892 | 203 |
^ self == ArithmeticValue |
204 |
! ! |
|
205 |
||
701 | 206 |
!ArithmeticValue methodsFor:'arithmetic'! |
207 |
||
208 |
* something |
|
11735 | 209 |
"return the product of the receiver and the argument." |
701 | 210 |
|
211 |
^ self subclassResponsibility |
|
212 |
! |
|
213 |
||
214 |
+ something |
|
215 |
"return the sum of the receiver and the argument" |
|
216 |
||
217 |
^ self subclassResponsibility |
|
218 |
! |
|
219 |
||
220 |
- something |
|
221 |
"return the difference of the receiver and the argument" |
|
222 |
||
223 |
^ self subclassResponsibility |
|
224 |
! |
|
225 |
||
226 |
/ something |
|
227 |
"return the quotient of the receiver and the argument" |
|
1 | 228 |
|
701 | 229 |
^ self subclassResponsibility |
230 |
! |
|
231 |
||
232 |
// something |
|
1881 | 233 |
"return the integer quotient of dividing the receiver by aNumber with |
234 |
truncation towards negative infinity." |
|
701 | 235 |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
236 |
"/ Note: '^ (self / something) floor' may lead to infinite recursion |
1172 | 237 |
|
238 |
^ self subclassResponsibility |
|
239 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
240 |
"Modified: / 5.11.1996 / 11:46:27 / cg" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
241 |
"Modified: / 12.2.1998 / 16:31:36 / stefan" |
701 | 242 |
! |
243 |
||
244 |
\\ something |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
245 |
"return the receiver modulo something. |
6067 | 246 |
The remainder has the same sign as something. |
247 |
The following is always true: |
|
10326 | 248 |
(receiver // something) * something + (receiver \\ something) = receiver |
6067 | 249 |
" |
1 | 250 |
|
701 | 251 |
^ self - ((self // something) * something) |
3134 | 252 |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
253 |
" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
254 |
0.9 \\ 0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
255 |
0.9 \\ -0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
256 |
-0.9 \\ 0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
257 |
-0.9 \\ -0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
258 |
" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
259 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
260 |
"Modified: / 12.2.1998 / 19:14:37 / stefan" |
701 | 261 |
! |
262 |
||
263 |
abs |
|
264 |
"return the absolute value of the receiver" |
|
265 |
||
266 |
(self negative) ifTrue:[^ self negated]. |
|
267 |
^ self |
|
268 |
! |
|
269 |
||
10326 | 270 |
dist:arg |
8757 | 271 |
"return the distance between arg and the receiver." |
272 |
||
273 |
^ (arg - self) abs |
|
274 |
||
275 |
" |
|
10326 | 276 |
(1%1) dist:(0%0) |
277 |
(1@1) dist:(0@0) |
|
278 |
(1) dist:(0) |
|
8757 | 279 |
" |
280 |
! |
|
281 |
||
701 | 282 |
negated |
283 |
"return the receiver negated" |
|
284 |
||
285 |
^ self class zero - self |
|
1 | 286 |
! |
287 |
||
701 | 288 |
quo:something |
289 |
"Return the integer quotient of dividing the receiver by the argument |
|
6067 | 290 |
with truncation towards zero. |
291 |
The following is always true: |
|
10326 | 292 |
(receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver |
6067 | 293 |
" |
701 | 294 |
|
295 |
^ (self / something) truncated |
|
296 |
! |
|
297 |
||
298 |
reciprocal |
|
299 |
"return the receivers reciprocal" |
|
300 |
||
301 |
^ self class unity / self |
|
302 |
! |
|
1 | 303 |
|
701 | 304 |
rem:something |
305 |
"Return the integer remainder of dividing the receiver by the argument |
|
306 |
with truncation towards zero. |
|
6067 | 307 |
The remainder has the same sign as the receiver. |
308 |
The following is always true: |
|
10326 | 309 |
(receiver quo: something) * something + (receiver rem: something) = receiver |
6067 | 310 |
" |
701 | 311 |
|
312 |
^ self - ((self quo:something) * something) |
|
7440 | 313 |
! |
314 |
||
315 |
uncheckedDivide:aNumber |
|
316 |
"return the quotient of the receiver and the argument, aNumber. |
|
7471 | 317 |
Do not check for divide by zero (return NaN or Infinity). |
7440 | 318 |
This operation is provided for emulators of other languages/semantics, |
319 |
where no exception is raised for these results (i.e. Java). |
|
320 |
Its only defined if the arguments type is the same as the receivers." |
|
321 |
||
322 |
aNumber isZero ifTrue:[ |
|
10326 | 323 |
self isZero ifTrue:[^ self class NaN]. |
324 |
self negative ifTrue:[^ self class negativeInfinity]. |
|
325 |
^ self class infinity. |
|
7440 | 326 |
]. |
327 |
^ self / aNumber |
|
701 | 328 |
! ! |
329 |
||
12578 | 330 |
!ArithmeticValue methodsFor:'arithmetic destructive'! |
331 |
||
332 |
*= aNumber |
|
333 |
"Return the product of self multiplied by aNumber. |
|
334 |
The receiver MAY, but NEED NOT be changed to contain the product. |
|
335 |
So this method must be used as: 'a := a *= 5'. |
|
336 |
This method can be redefined for constructed datatypes to do optimisations" |
|
337 |
||
338 |
^ self * aNumber |
|
339 |
||
340 |
"Created: / 28.4.1999 / 11:46:11 / stefan" |
|
341 |
"Modified: / 28.4.1999 / 11:53:28 / stefan" |
|
342 |
! |
|
343 |
||
344 |
+= aNumber |
|
345 |
"Return the sum of self and aNumber. |
|
346 |
The receiver MAY, but NEED NOT be changed to contain the sum. |
|
347 |
So this method must be used as: 'a := a += 5'. |
|
348 |
This method can be redefined for constructed datatypes to do optimisations" |
|
349 |
||
350 |
^ self + aNumber |
|
351 |
||
352 |
"Created: / 28.4.1999 / 10:13:41 / stefan" |
|
353 |
"Modified: / 28.4.1999 / 11:54:11 / stefan" |
|
354 |
! |
|
355 |
||
356 |
-= aNumber |
|
357 |
"Return the difference of self and aNumber. |
|
358 |
The receiver MAY, but NEED NOT be changed to contain the difference. |
|
359 |
So this method must be used as: 'a := a -= 5'. |
|
360 |
This method can be redefined for constructed datatypes to do optimisations" |
|
361 |
||
362 |
^ self - aNumber |
|
363 |
||
364 |
"Created: / 28.4.1999 / 10:13:58 / stefan" |
|
365 |
"Modified: / 28.4.1999 / 11:54:37 / stefan" |
|
366 |
! |
|
367 |
||
368 |
/= aNumber |
|
369 |
"Return the quotient of self and aNumber. |
|
370 |
The receiver MAY, but NEED NOT be changed to contain the quotient. |
|
371 |
So this method must be used as: 'a := a /= 5'. |
|
372 |
This method can be redefined for constructed datatypes to do optimisations" |
|
373 |
||
374 |
^ self / aNumber |
|
375 |
||
376 |
"Created: / 28.4.1999 / 11:46:22 / stefan" |
|
377 |
"Modified: / 28.4.1999 / 11:55:06 / stefan" |
|
378 |
! |
|
379 |
||
380 |
div2 |
|
381 |
"Return the quotient of self divided by 2. |
|
382 |
The receiver MAY, but NEED NOT be changed to contain the result. |
|
383 |
So this method must be used as: 'a := a div2. |
|
384 |
This method can be redefined for constructed datatypes to do optimisations" |
|
385 |
||
386 |
^ self // 2 |
|
387 |
||
388 |
"Created: / 28.4.1999 / 10:12:44 / stefan" |
|
389 |
"Modified: / 28.4.1999 / 11:56:09 / stefan" |
|
390 |
! |
|
391 |
||
392 |
mul2 |
|
393 |
"Return the product of self multiplied by 2. |
|
394 |
The receiver MAY, but NEED NOT be changed to contain the result. |
|
395 |
So this method must be used as: 'a := a mul2. |
|
396 |
This method can be redefined for constructed datatypes to do optimisations" |
|
397 |
||
398 |
^ self * 2 |
|
399 |
||
400 |
"Created: / 28.4.1999 / 10:12:55 / stefan" |
|
401 |
"Modified: / 28.4.1999 / 11:56:38 / stefan" |
|
402 |
! ! |
|
403 |
||
5238 | 404 |
!ArithmeticValue methodsFor:'coercing & converting'! |
1888 | 405 |
|
406 |
coerce:aNumber |
|
11725 | 407 |
"convert the argument aNumber into an instance of the receivers class and return it." |
1888 | 408 |
|
409 |
^ self subclassResponsibility |
|
410 |
! |
|
411 |
||
412 |
generality |
|
413 |
"return a number giving the receivers generality, that number is |
|
10326 | 414 |
used to convert one of the arguments in a mixed expression. |
1888 | 415 |
The generality has to be defined in subclasses, |
10326 | 416 |
such that gen(a) > gen(b) iff, conversion of b into a's class |
1888 | 417 |
does not cut precision. For example, Integer has 40, Float has 80, |
418 |
meaning that if we convert a Float to an Integer, some precision may |
|
419 |
be lost. The generality is used by ArithmeticValue>>retry:coercing:, |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
420 |
which converts the lower-precision number to the higher precision |
1888 | 421 |
numbers class, when mixed-type arithmetic is performed." |
10326 | 422 |
|
1888 | 423 |
^ self subclassResponsibility |
424 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
425 |
"Modified: / 5.11.1996 / 15:05:30 / cg" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
426 |
"Modified: / 13.2.1998 / 15:36:01 / stefan" |
1888 | 427 |
! |
428 |
||
429 |
retry:aSymbol coercing:aNumber |
|
430 |
"arithmetic represented by the binary operator, aSymbol, |
|
10326 | 431 |
could not be performed with the receiver and the argument, aNumber, |
432 |
because of the differences in representation. |
|
433 |
Coerce either the receiver or the argument, depending on which has higher |
|
434 |
generality, and try again. |
|
1888 | 435 |
If the operation is compare for same value (=), return false if |
10326 | 436 |
the argument is not a Number. |
1888 | 437 |
If the generalities are the same, create an error message, since this |
438 |
means that a subclass has not been fully implemented." |
|
439 |
||
440 |
|hasGenerality myGenerality otherGenerality| |
|
441 |
||
442 |
hasGenerality := aNumber respondsTo:#generality. |
|
443 |
hasGenerality ifFalse:[ |
|
10326 | 444 |
(aSymbol == #=) ifTrue:[ |
445 |
^ false |
|
446 |
]. |
|
447 |
(aSymbol == #~=) ifTrue:[ |
|
448 |
^ true |
|
449 |
]. |
|
450 |
^ self error:'retry:coercing: argument is not a number'. |
|
1888 | 451 |
]. |
452 |
||
453 |
myGenerality := self generality. |
|
454 |
otherGenerality := aNumber generality. |
|
455 |
(myGenerality > otherGenerality) ifTrue:[ |
|
10326 | 456 |
^ self perform:aSymbol with:(self coerce:aNumber) |
1888 | 457 |
]. |
458 |
(myGenerality < otherGenerality) ifTrue:[ |
|
10326 | 459 |
^ (aNumber coerce:self) perform:aSymbol with:aNumber |
1888 | 460 |
]. |
10324 | 461 |
self error:'retry:coercing: oops - same generality; retry should not happen' |
1888 | 462 |
|
463 |
"Modified: 5.11.1996 / 15:03:38 / cg" |
|
464 |
! ! |
|
465 |
||
701 | 466 |
!ArithmeticValue methodsFor:'converting'! |
467 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
468 |
asDouble |
7429 | 469 |
"ST80 compatibility: return a double with receivers value. |
470 |
our floats are the identical to ST80 doubles" |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
471 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
472 |
^ self asFloat |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
473 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
474 |
"Created: / 13.2.1998 / 15:40:14 / stefan" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
475 |
! |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
476 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
477 |
asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
478 |
"return the receiver as fixedPoint number. |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
479 |
Q: what should the scale be here ?" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
480 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
481 |
^ self asFraction asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
482 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
483 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
484 |
0.3 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
485 |
0.5 asFixedPoint |
10326 | 486 |
(1/5) asFloat asFixedPoint |
487 |
(1/3) asFloat asFixedPoint |
|
488 |
(2/3) asFloat asFixedPoint |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
489 |
(1/8) asFloat asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
490 |
3.14159 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
491 |
0.0000001 asFraction |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
492 |
0.0000001 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
493 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
494 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
495 |
"Modified: / 25.10.1997 / 15:36:54 / cg" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
496 |
! |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
497 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
498 |
asFixedPoint:scale |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
499 |
"return the receiver as fixedPoint number with the given |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
500 |
number of post-decimal-digits." |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
501 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
502 |
^ self asFraction asFixedPoint:scale |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
503 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
504 |
" |
10326 | 505 |
0.3 asFixedPoint:4 |
506 |
0.3 asFixedPoint:3 |
|
507 |
0.3 asFixedPoint:2 |
|
508 |
0.3 asFixedPoint:1 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
509 |
0.3 asFixedPoint:0 |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
510 |
|
10326 | 511 |
0.5 asFixedPoint:3 |
512 |
(1/5) asFloat asFixedPoint:1 |
|
513 |
(1/8) asFloat asFixedPoint:1 |
|
514 |
1.0 asFixedPoint:2 |
|
515 |
3.14159 asFixedPoint:2 |
|
516 |
3.14159 asFixedPoint:3 |
|
517 |
(3.14159 asFixedPoint:2) asFixedPoint:5 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
518 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
519 |
! |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
520 |
|
12980 | 521 |
asFixedPointRoundedToScale |
522 |
"return the receiver as fixedPoint number, rounded to its scale." |
|
523 |
||
524 |
^ self asFixedPoint roundedToScale |
|
525 |
||
526 |
" |
|
527 |
0.3 asFixedPoint |
|
528 |
0.5 asFixedPoint |
|
529 |
(2/3) asFloat asFixedPoint |
|
530 |
(1/8) asFloat asFixedPoint |
|
531 |
3.14159 asFixedPoint |
|
532 |
||
533 |
0.3 asFixedPointRoundedToScale |
|
534 |
0.5 asFixedPointRoundedToScale |
|
535 |
(2/3) asFloat asFixedPointRoundedToScale |
|
536 |
(1/8) asFloat asFixedPointRoundedToScale |
|
537 |
3.14159 asFixedPointRoundedToScale |
|
538 |
" |
|
539 |
||
540 |
"Created: / 02-08-2010 / 13:32:16 / cg" |
|
541 |
! |
|
542 |
||
543 |
asFixedPointRoundedToScale:scale |
|
544 |
"return the receiver as fixedPoint number with the given |
|
545 |
number of post-decimal-digits, rounded to its scale" |
|
546 |
||
547 |
^ (self asFixedPoint:scale) roundedToScale |
|
548 |
||
549 |
"Created: / 02-08-2010 / 13:33:11 / cg" |
|
550 |
! |
|
551 |
||
1 | 552 |
asFloat |
553 |
"return a float with same value" |
|
554 |
||
555 |
^ self subclassResponsibility |
|
556 |
! |
|
557 |
||
5986 | 558 |
asFloatD |
559 |
"return a double precision float with same value. |
|
560 |
Added for ANSI compatibility" |
|
561 |
||
562 |
^ self asFloat |
|
563 |
||
564 |
"Created: / 7.9.2001 / 13:36:48 / cg" |
|
565 |
"Modified: / 7.9.2001 / 13:38:10 / cg" |
|
566 |
! |
|
567 |
||
568 |
asFloatE |
|
569 |
"return a single precision float with same value. |
|
570 |
Added for ANSI compatibility" |
|
571 |
||
572 |
^ self asShortFloat |
|
573 |
||
574 |
"Created: / 7.9.2001 / 13:37:06 / cg" |
|
575 |
"Modified: / 7.9.2001 / 13:38:15 / cg" |
|
576 |
! |
|
577 |
||
578 |
asFloatQ |
|
579 |
"return a quad precision float with same value. |
|
580 |
Added for ANSI compatibility" |
|
581 |
||
582 |
^ self asLongFloat |
|
583 |
||
584 |
"Created: / 7.9.2001 / 13:38:44 / cg" |
|
585 |
! |
|
586 |
||
1 | 587 |
asFraction |
588 |
"return a fraction with same value" |
|
589 |
||
590 |
^ self subclassResponsibility |
|
591 |
! |
|
592 |
||
701 | 593 |
asInteger |
594 |
"return an integer with same value - might truncate" |
|
595 |
||
596 |
^ self truncated |
|
597 |
! |
|
598 |
||
7378 | 599 |
asLimitedPrecisionReal |
600 |
"return a float of any precision with same value" |
|
601 |
||
602 |
^ self asFloat |
|
603 |
! |
|
604 |
||
5986 | 605 |
asLongFloat |
606 |
"return a longFloat with same value" |
|
607 |
||
10315 | 608 |
"WARNING: could loose precision here, if not redefined in concrete classes which |
609 |
have more than float precision (i.e. LargeIntegers and Fractions)" |
|
7355 | 610 |
|
10315 | 611 |
^ self asFloat asLongFloat "/ subclassResponsibility |
5986 | 612 |
|
613 |
"Modified: / 17.4.1996 / 12:21:35 / cg" |
|
614 |
"Created: / 7.9.2001 / 13:39:31 / cg" |
|
615 |
! |
|
616 |
||
5987 | 617 |
asScaledDecimal:scale |
618 |
"return a fixedPoint approximating the receivers value" |
|
619 |
||
620 |
^ self asFixedPoint:scale |
|
621 |
||
622 |
" |
|
623 |
1.234 asScaledDecimal:2 |
|
624 |
" |
|
625 |
||
626 |
"Created: / 7.9.2001 / 13:46:26 / cg" |
|
627 |
"Modified: / 7.9.2001 / 13:46:44 / cg" |
|
628 |
! |
|
629 |
||
1200 | 630 |
asShortFloat |
631 |
"return a shortFloat with same value" |
|
632 |
||
633 |
^ self asFloat asShortFloat |
|
634 |
||
635 |
"Modified: 17.4.1996 / 12:21:35 / cg" |
|
636 |
! |
|
637 |
||
701 | 638 |
degreesToRadians |
639 |
"interpreting the receiver as radians, return the degrees" |
|
640 |
||
641 |
^ self asFloat degreesToRadians |
|
642 |
! |
|
643 |
||
644 |
radiansToDegrees |
|
645 |
"interpreting the receiver as degrees, return the radians" |
|
646 |
||
647 |
^ self asFloat radiansToDegrees |
|
1 | 648 |
! ! |
649 |
||
701 | 650 |
!ArithmeticValue methodsFor:'double dispatching'! |
1 | 651 |
|
7355 | 652 |
differenceFromComplex:aComplex |
653 |
"the receiver does not know how to subtract from a complex - |
|
654 |
retry the operation by coercing to higher generality" |
|
655 |
||
656 |
^ aComplex retry:#- coercing:self |
|
657 |
! |
|
658 |
||
1888 | 659 |
differenceFromFixedPoint:aFixedPoint |
660 |
"the receiver does not know how to subtract from a fixedPoint number - |
|
661 |
retry the operation by coercing to higher generality" |
|
662 |
||
663 |
^ aFixedPoint retry:#- coercing:self |
|
664 |
||
665 |
"Created: 5.11.1996 / 14:55:51 / cg" |
|
666 |
! |
|
667 |
||
701 | 668 |
differenceFromFloat:aFloat |
669 |
"the receiver does not know how to subtract from a float - |
|
670 |
retry the operation by coercing to higher generality" |
|
1 | 671 |
|
701 | 672 |
^ aFloat retry:#- coercing:self |
1 | 673 |
! |
674 |
||
701 | 675 |
differenceFromFraction:aFraction |
676 |
"the receiver does not know how to subtract from a fraction - |
|
677 |
retry the operation by coercing to higher generality" |
|
1 | 678 |
|
701 | 679 |
^ aFraction retry:#- coercing:self |
1 | 680 |
! |
681 |
||
701 | 682 |
differenceFromInteger:anInteger |
683 |
"the receiver does not know how to subtract from an integer - |
|
684 |
retry the operation by coercing to higher generality" |
|
1 | 685 |
|
701 | 686 |
^ anInteger retry:#- coercing:self |
1 | 687 |
! |
688 |
||
7440 | 689 |
differenceFromLargeFloat:aLargeFloat |
690 |
"the receiver does not know how to subtract from a largeFloat - |
|
691 |
retry the operation by coercing to higher generality" |
|
692 |
||
693 |
^ aLargeFloat retry:#- coercing:self |
|
694 |
! |
|
695 |
||
7399 | 696 |
differenceFromLongFloat:aLongFloat |
697 |
"the receiver does not know how to subtract from a longFloat - |
|
698 |
retry the operation by coercing to higher generality" |
|
699 |
||
700 |
^ aLongFloat retry:#- coercing:self |
|
701 |
||
702 |
"Created: 17.4.1996 / 12:33:16 / cg" |
|
703 |
! |
|
704 |
||
1200 | 705 |
differenceFromShortFloat:aShortFloat |
706 |
"the receiver does not know how to subtract from a shortFloat - |
|
707 |
retry the operation by coercing to higher generality" |
|
708 |
||
709 |
^ aShortFloat retry:#- coercing:self |
|
710 |
||
711 |
"Created: 17.4.1996 / 12:33:16 / cg" |
|
712 |
! |
|
713 |
||
7355 | 714 |
equalFromComplex:aComplex |
715 |
"the receiver does not know how to compare to a complex number - |
|
716 |
retry the operation by coercing to higher generality" |
|
717 |
||
718 |
^ aComplex retry:#= coercing:self |
|
719 |
! |
|
720 |
||
721 |
equalFromFixedPoint:aFixedPoint |
|
722 |
"the receiver does not know how to compare to a fixed point - |
|
723 |
retry the operation by coercing to higher generality" |
|
724 |
||
725 |
^ aFixedPoint retry:#= coercing:self |
|
726 |
! |
|
727 |
||
728 |
equalFromFloat:aFloat |
|
729 |
"the receiver does not know how to compare to a float - |
|
730 |
retry the operation by coercing to higher generality" |
|
731 |
||
732 |
^ aFloat retry:#= coercing:self |
|
733 |
! |
|
734 |
||
735 |
equalFromFraction:aFraction |
|
736 |
"the receiver does not know how to compare to a fraction - |
|
737 |
retry the operation by coercing to higher generality" |
|
738 |
||
739 |
^ aFraction retry:#= coercing:self |
|
740 |
! |
|
741 |
||
742 |
equalFromInteger:anInteger |
|
743 |
"the receiver does not know how to compare to an integer - |
|
744 |
retry the operation by coercing to higher generality" |
|
745 |
||
746 |
^ anInteger retry:#= coercing:self |
|
747 |
! |
|
748 |
||
7440 | 749 |
equalFromLargeFloat:aLargeFloat |
750 |
"the receiver does not know how to compare to a large float - |
|
751 |
retry the operation by coercing to higher generality" |
|
752 |
||
753 |
^ aLargeFloat retry:#= coercing:self |
|
754 |
! |
|
755 |
||
7355 | 756 |
equalFromLongFloat:aLongFloat |
757 |
"the receiver does not know how to compare to a long float - |
|
758 |
retry the operation by coercing to higher generality" |
|
759 |
||
760 |
^ aLongFloat retry:#= coercing:self |
|
761 |
! |
|
762 |
||
763 |
equalFromShortFloat:aShortFloat |
|
764 |
"the receiver does not know how to compare to a short float - |
|
765 |
retry the operation by coercing to higher generality" |
|
766 |
||
767 |
^ aShortFloat retry:#= coercing:self |
|
768 |
! |
|
769 |
||
13007 | 770 |
integerQuotientFromInteger:anInteger |
771 |
"anInteger does not know how to divide by the receiver - |
|
772 |
retry the operation by coercing to higher generality" |
|
773 |
||
774 |
^ anInteger retry:#// coercing:self |
|
775 |
||
776 |
"Created: / 09-08-2010 / 19:49:26 / cg" |
|
777 |
! |
|
778 |
||
1888 | 779 |
lessFromFixedPoint:aFixedPoint |
780 |
"the receiver does not know how to compare to a fixedPoint number - |
|
781 |
retry the operation by coercing to higher generality" |
|
782 |
||
783 |
^ aFixedPoint retry:#< coercing:self |
|
784 |
||
785 |
"Created: 5.11.1996 / 14:56:12 / cg" |
|
786 |
! |
|
787 |
||
701 | 788 |
lessFromFloat:aFloat |
789 |
"the receiver does not know how to compare to a float - |
|
790 |
retry the operation by coercing to higher generality" |
|
1 | 791 |
|
701 | 792 |
^ aFloat retry:#< coercing:self |
1 | 793 |
! |
794 |
||
701 | 795 |
lessFromFraction:aFraction |
796 |
"the receiver does not know how to compare to a fraction - |
|
797 |
retry the operation by coercing to higher generality" |
|
1 | 798 |
|
701 | 799 |
^ aFraction retry:#< coercing:self |
1 | 800 |
! |
801 |
||
701 | 802 |
lessFromInteger:anInteger |
803 |
"the receiver does not know how to compare to an integer - |
|
804 |
retry the operation by coercing to higher generality" |
|
1 | 805 |
|
701 | 806 |
^ anInteger retry:#< coercing:self |
1 | 807 |
! |
808 |
||
7440 | 809 |
lessFromLargeFloat:aLargeFloat |
810 |
"the receiver does not know how to compare to a largeFloat - |
|
811 |
retry the operation by coercing to higher generality" |
|
812 |
||
813 |
^ aLargeFloat retry:#< coercing:self |
|
814 |
! |
|
815 |
||
7399 | 816 |
lessFromLongFloat:aLongFloat |
817 |
"the receiver does not know how to compare to a longFloat - |
|
818 |
retry the operation by coercing to higher generality" |
|
819 |
||
820 |
^ aLongFloat retry:#< coercing:self |
|
821 |
||
822 |
"Modified: 17.4.1996 / 12:33:33 / cg" |
|
823 |
! |
|
824 |
||
1200 | 825 |
lessFromShortFloat:aShortFloat |
826 |
"the receiver does not know how to compare to a shortFloat - |
|
827 |
retry the operation by coercing to higher generality" |
|
828 |
||
829 |
^ aShortFloat retry:#< coercing:self |
|
830 |
||
831 |
"Modified: 17.4.1996 / 12:33:33 / cg" |
|
832 |
! |
|
833 |
||
14002 | 834 |
moduloFromInteger:anInteger |
835 |
"anInteger does not know how to compute the modulo from the receiver - |
|
836 |
retry the operation by coercing to higher generality" |
|
837 |
||
838 |
^ anInteger retry:#\\ coercing:self |
|
839 |
||
840 |
"Created: / 12-02-2012 / 20:38:13 / cg" |
|
841 |
! |
|
842 |
||
7355 | 843 |
productFromComplex:aComplex |
844 |
"the receiver does not know how to multiply a complex - |
|
845 |
retry the operation by coercing to higher generality" |
|
846 |
||
847 |
^ aComplex retry:#* coercing:self |
|
848 |
! |
|
849 |
||
1888 | 850 |
productFromFixedPoint:aFixedPoint |
851 |
"the receiver does not know how to multiply a fixed point number - |
|
852 |
retry the operation by coercing to higher generality" |
|
853 |
||
854 |
^ aFixedPoint retry:#* coercing:self |
|
855 |
||
856 |
"Created: 5.11.1996 / 14:56:28 / cg" |
|
857 |
! |
|
858 |
||
701 | 859 |
productFromFloat:aFloat |
860 |
"the receiver does not know how to multiply a float - |
|
861 |
retry the operation by coercing to higher generality" |
|
1 | 862 |
|
701 | 863 |
^ aFloat retry:#* coercing:self |
1 | 864 |
! |
865 |
||
701 | 866 |
productFromFraction:aFraction |
867 |
"the receiver does not know how to multiply a fraction - |
|
868 |
retry the operation by coercing to higher generality" |
|
1 | 869 |
|
701 | 870 |
^ aFraction retry:#* coercing:self |
1 | 871 |
! |
872 |
||
701 | 873 |
productFromInteger:anInteger |
874 |
"the receiver does not know how to multiply an integer - |
|
875 |
retry the operation by coercing to higher generality" |
|
1 | 876 |
|
701 | 877 |
^ anInteger retry:#* coercing:self |
1 | 878 |
! |
879 |
||
7440 | 880 |
productFromLargeFloat:aLargeFloat |
881 |
"the receiver does not know how to multiply a largeFloat - |
|
882 |
retry the operation by coercing to higher generality" |
|
883 |
||
884 |
^ aLargeFloat retry:#* coercing:self |
|
885 |
||
886 |
"Created: 17.4.1996 / 12:33:48 / cg" |
|
887 |
! |
|
888 |
||
7399 | 889 |
productFromLongFloat:aLongFloat |
890 |
"the receiver does not know how to multiply a longFloat - |
|
891 |
retry the operation by coercing to higher generality" |
|
892 |
||
893 |
^ aLongFloat retry:#* coercing:self |
|
894 |
||
895 |
"Created: 17.4.1996 / 12:33:48 / cg" |
|
896 |
! |
|
897 |
||
1200 | 898 |
productFromShortFloat:aShortFloat |
899 |
"the receiver does not know how to multiply a shortFloat - |
|
900 |
retry the operation by coercing to higher generality" |
|
901 |
||
902 |
^ aShortFloat retry:#* coercing:self |
|
903 |
||
904 |
"Created: 17.4.1996 / 12:33:48 / cg" |
|
905 |
! |
|
906 |
||
7355 | 907 |
quotientFromComplex:aComplex |
7440 | 908 |
"aComplex does not know how to divide by the receiver - |
7355 | 909 |
retry the operation by coercing to higher generality" |
910 |
||
911 |
^ aComplex retry:#/ coercing:self |
|
912 |
! |
|
913 |
||
1888 | 914 |
quotientFromFixedPoint:aFixedPoint |
7440 | 915 |
"aFixedPoint does not know how to divide by the receiver - |
1888 | 916 |
retry the operation by coercing to higher generality" |
917 |
||
918 |
^ aFixedPoint retry:#/ coercing:self |
|
919 |
||
920 |
"Created: 5.11.1996 / 14:56:41 / cg" |
|
921 |
! |
|
922 |
||
701 | 923 |
quotientFromFloat:aFloat |
7440 | 924 |
"aFloat does not know how to divide by the receiver - |
701 | 925 |
retry the operation by coercing to higher generality" |
1 | 926 |
|
701 | 927 |
^ aFloat retry:#/ coercing:self |
1 | 928 |
! |
929 |
||
701 | 930 |
quotientFromFraction:aFraction |
7440 | 931 |
"aFraction does not know how to divide by the receiver - |
701 | 932 |
retry the operation by coercing to higher generality" |
1 | 933 |
|
701 | 934 |
^ aFraction retry:#/ coercing:self |
1 | 935 |
! |
936 |
||
701 | 937 |
quotientFromInteger:anInteger |
7440 | 938 |
"anInteger does not know how to divide by the receiver - |
1 | 939 |
retry the operation by coercing to higher generality" |
940 |
||
701 | 941 |
^ anInteger retry:#/ coercing:self |
1 | 942 |
! |
943 |
||
7440 | 944 |
quotientFromLargeFloat:aLargeFloat |
945 |
"aLargeFloat does not know how to divide by the receiver - |
|
946 |
retry the operation by coercing to higher generality" |
|
947 |
||
948 |
^ aLargeFloat retry:#/ coercing:self |
|
949 |
||
950 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
951 |
! |
|
952 |
||
7399 | 953 |
quotientFromLongFloat:aLongFloat |
7440 | 954 |
"aLongFloat does not know how to divide by the receiver - |
7399 | 955 |
retry the operation by coercing to higher generality" |
956 |
||
957 |
^ aLongFloat retry:#/ coercing:self |
|
958 |
||
959 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
960 |
! |
|
961 |
||
1200 | 962 |
quotientFromShortFloat:aShortFloat |
7440 | 963 |
"aShortFloat does not know how to divide by the receiver - |
1200 | 964 |
retry the operation by coercing to higher generality" |
965 |
||
966 |
^ aShortFloat retry:#/ coercing:self |
|
967 |
||
968 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
969 |
! |
|
970 |
||
14747 | 971 |
remainderFromFloat:aFloat |
972 |
"aFloat does not know how to compute the remainder with the receiver - |
|
973 |
retry the operation by coercing to higher generality" |
|
974 |
||
975 |
^ aFloat retry:#rem: coercing:self |
|
976 |
! |
|
977 |
||
978 |
remainderFromShortFloat:aShortFloat |
|
979 |
"aShortFloat does not know how to compute the remainder with the receiver - |
|
980 |
retry the operation by coercing to higher generality" |
|
981 |
||
982 |
^ aShortFloat retry:#rem: coercing:self |
|
983 |
! |
|
984 |
||
7355 | 985 |
sumFromComplex:aComplex |
986 |
"the receiver does not know how to add a complex - |
|
987 |
retry the operation by coercing to higher generality" |
|
988 |
||
989 |
^ aComplex retry:#+ coercing:self |
|
990 |
! |
|
991 |
||
1888 | 992 |
sumFromFixedPoint:aFixedPoint |
993 |
"the receiver does not know how to add a fixed point number - |
|
994 |
retry the operation by coercing to higher generality" |
|
995 |
||
996 |
^ aFixedPoint retry:#+ coercing:self |
|
997 |
||
998 |
"Created: 5.11.1996 / 14:56:56 / cg" |
|
999 |
! |
|
1000 |
||
1 | 1001 |
sumFromFloat:aFloat |
1002 |
"the receiver does not know how to add a float - |
|
1003 |
retry the operation by coercing to higher generality" |
|
1004 |
||
1005 |
^ aFloat retry:#+ coercing:self |
|
1006 |
! |
|
1007 |
||
1008 |
sumFromFraction:aFraction |
|
1009 |
"the receiver does not know how to add a fraction - |
|
1010 |
retry the operation by coercing to higher generality" |
|
1011 |
||
1012 |
^ aFraction retry:#+ coercing:self |
|
1013 |
! |
|
1014 |
||
701 | 1015 |
sumFromInteger:anInteger |
1016 |
"the receiver does not know how to add an integer - |
|
1 | 1017 |
retry the operation by coercing to higher generality" |
1018 |
||
701 | 1019 |
^ anInteger retry:#+ coercing:self |
1200 | 1020 |
! |
1021 |
||
7440 | 1022 |
sumFromLargeFloat:aLargeFloat |
1023 |
"the receiver does not know how to add a largeFloat - |
|
1024 |
retry the operation by coercing to higher generality" |
|
1025 |
||
1026 |
^ aLargeFloat retry:#+ coercing:self |
|
1027 |
||
1028 |
"Created: 17.4.1996 / 12:34:10 / cg" |
|
1029 |
! |
|
1030 |
||
7399 | 1031 |
sumFromLongFloat:aLongFloat |
1032 |
"the receiver does not know how to add a longFloat - |
|
1033 |
retry the operation by coercing to higher generality" |
|
1034 |
||
1035 |
^ aLongFloat retry:#+ coercing:self |
|
1036 |
||
1037 |
"Created: 17.4.1996 / 12:34:10 / cg" |
|
1038 |
! |
|
1039 |
||
1200 | 1040 |
sumFromShortFloat:aShortFloat |
1041 |
"the receiver does not know how to add a shortFloat - |
|
1042 |
retry the operation by coercing to higher generality" |
|
1043 |
||
1044 |
^ aShortFloat retry:#+ coercing:self |
|
1045 |
||
1046 |
"Created: 17.4.1996 / 12:34:10 / cg" |
|
1 | 1047 |
! ! |
1048 |
||
7384 | 1049 |
!ArithmeticValue methodsFor:'mathematical functions'! |
1 | 1050 |
|
10326 | 1051 |
** aNumber |
9443 | 1052 |
"Answer the receiver raised to the power of the argument, aNumber." |
1053 |
"same as Number>>raisedTo:" |
|
1054 |
||
1055 |
^self raisedTo: aNumber |
|
1056 |
||
1057 |
"Created: / 15-07-2006 / 15:11:05 / cg" |
|
1058 |
"Modified: / 17-07-2006 / 12:51:33 / cg" |
|
1059 |
! |
|
1060 |
||
7635 | 1061 |
raisedTo: aNumber |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1062 |
aNumber isInteger ifTrue:[ |
10326 | 1063 |
^ self raisedToInteger:aNumber |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1064 |
]. |
7635 | 1065 |
^ self subclassResponsibility |
6858 | 1066 |
! |
1067 |
||
10326 | 1068 |
raisedToInteger:exp |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1069 |
"return the receiver raised to exp" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1070 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1071 |
|result e t| |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1072 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1073 |
"use the addition chaining algorithm, |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1074 |
which is much faster for big exp-arguments" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1075 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1076 |
result := 1. |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1077 |
t := self. |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1078 |
exp < 0 ifTrue:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1079 |
e := exp negated. |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1080 |
] ifFalse:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1081 |
e := exp. |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1082 |
]. |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1083 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1084 |
[e ~~ 0] whileTrue:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1085 |
[(e bitAnd:1) == 0] whileTrue:[ |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1086 |
e := e bitShift:-1. |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1087 |
t := t * t. |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1088 |
]. |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1089 |
e := e - 1. |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1090 |
result := result * t. |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1091 |
]. |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1092 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1093 |
(exp < 0) ifTrue:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1094 |
^ 1 / result |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1095 |
]. |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1096 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1097 |
^ result |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1098 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1099 |
" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1100 |
(2 raisedToInteger:216) |
10326 | 1101 |
(2 raisedTo:216) |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1102 |
-> 105312291668557186697918027683670432318895095400549111254310977536 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1103 |
|
10326 | 1104 |
(2 raisedToInteger:216) asFloat |
1105 |
(2 raisedTo:216) asFloat |
|
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1106 |
-> 1.05312E+65 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1107 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1108 |
(2 raisedToInteger:500) |
10326 | 1109 |
(2 raisedTo:500) |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1110 |
-> 3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376 |
10326 | 1111 |
2 raisedToInteger:10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1112 |
-> 1024 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1113 |
-2 raisedToInteger:10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1114 |
-> 1024 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1115 |
-2 raisedToInteger:9 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1116 |
-> -512 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1117 |
10 raisedToInteger:-10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1118 |
-> (1/10000000000) |
10326 | 1119 |
2 raisedToInteger:0 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1120 |
-> 1 |
10326 | 1121 |
2 raisedToInteger:-1 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1122 |
-> (1/2) |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1123 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1124 |
Time millisecondsToRun:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1125 |
10000 timesRepeat:[ |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1126 |
(2 raisedToInteger:500) |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1127 |
] |
10326 | 1128 |
] |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1129 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1130 |
Time millisecondsToRun:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1131 |
|bigNum| |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1132 |
bigNum := 2 raisedToInteger:500. |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1133 |
10 timesRepeat:[ |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1134 |
(bigNum raisedToInteger:500) |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1135 |
] |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1136 |
] |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1137 |
" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1138 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1139 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1140 |
"Modified: / 27.4.1999 / 16:16:11 / stefan" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1141 |
! |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1142 |
|
701 | 1143 |
squared |
1144 |
"return receiver * receiver" |
|
1145 |
||
1146 |
^ self * self |
|
1147 |
! ! |
|
1148 |
||
1149 |
!ArithmeticValue methodsFor:'queries'! |
|
1150 |
||
1151 |
respondsToArithmetic |
|
1152 |
"return true, if the receiver responds to arithmetic messages" |
|
1153 |
||
1154 |
^ true |
|
1155 |
! ! |
|
1156 |
||
1157 |
!ArithmeticValue methodsFor:'testing'! |
|
1158 |
||
1159 |
denominator |
|
1160 |
"return the denominator of the receiver" |
|
1161 |
||
1162 |
^ 1 |
|
1163 |
! |
|
1164 |
||
1165 |
even |
|
1166 |
"return true if the receiver is divisible by 2" |
|
1167 |
||
1168 |
^ self truncated asInteger even |
|
1169 |
! |
|
1170 |
||
3635 | 1171 |
isComplex |
7355 | 1172 |
"Answer whether the receiver has an imaginary part |
1173 |
(i.e. if it is a complex number). Always false here." |
|
3635 | 1174 |
|
1175 |
^ false |
|
1176 |
||
1177 |
"Modified: / 9.7.1998 / 10:19:27 / cg" |
|
1178 |
! |
|
1179 |
||
8979
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1180 |
isFinite |
10326 | 1181 |
"return true, if the receiver is finite |
9400 | 1182 |
i.e. it can be represented as a rational number." |
1183 |
||
8979
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1184 |
^ true |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1185 |
! |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1186 |
|
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1187 |
isInfinite |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1188 |
^ false |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1189 |
! |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1190 |
|
7440 | 1191 |
isNegativeInfinity |
1192 |
^ self negative and:[self isInfinite] |
|
1193 |
! |
|
1194 |
||
1195 |
isNegativeZero |
|
1196 |
"return false - must be redefined by subclasses which can represent a negative zero |
|
1197 |
(i.e. limitedPrecisionReal classes)" |
|
1198 |
||
1199 |
^ false |
|
1200 |
! |
|
1201 |
||
1202 |
isPositiveInfinity |
|
1203 |
^ self positive and:[self isInfinite] |
|
1204 |
! |
|
1205 |
||
6156 | 1206 |
isReal |
1207 |
"return true, if the receiver is some kind of real number (as opposed to a complex); |
|
1208 |
false is returned here - the method is only redefined in Number (and Complex)." |
|
1209 |
||
1210 |
^ false |
|
1211 |
||
1212 |
"Modified: / 6.11.2001 / 13:20:20 / cg" |
|
1213 |
"Created: / 6.11.2001 / 13:26:29 / cg" |
|
1214 |
! |
|
1215 |
||
7440 | 1216 |
isZero |
1217 |
"return false - must be redefined by subclasses which can represent a negative zero |
|
1218 |
(i.e. limitedPrecisionReal classes)" |
|
701 | 1219 |
|
7440 | 1220 |
^ self = self class zero |
1221 |
! |
|
1222 |
||
1223 |
negative |
|
1224 |
"return true if the receiver is less than zero" |
|
1225 |
||
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
1226 |
^ self < self class zero |
701 | 1227 |
! |
1228 |
||
1229 |
numerator |
|
1230 |
"return the numerator of the receiver." |
|
1231 |
||
1232 |
^ self |
|
1233 |
! |
|
1234 |
||
1235 |
odd |
|
1236 |
"return true if the receiver is not divisible by 2" |
|
1237 |
||
1238 |
^ self even not |
|
1239 |
! |
|
1240 |
||
1241 |
positive |
|
1242 |
"return true, if the receiver is >= 0" |
|
1243 |
||
1244 |
^ self negative not |
|
1245 |
! |
|
1246 |
||
1247 |
sign |
|
7471 | 1248 |
"return the sign of the receiver (-1, 0 or 1)" |
701 | 1249 |
|
7440 | 1250 |
|zero| |
1251 |
||
1252 |
zero := self class zero. |
|
1253 |
(self < zero) ifTrue:[^ -1]. |
|
1254 |
(zero < self) ifTrue:[^ 1]. |
|
701 | 1255 |
^ 0 |
1256 |
! |
|
1257 |
||
1258 |
strictlyPositive |
|
1259 |
"return true, if the receiver is > 0" |
|
1260 |
||
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
1261 |
^ self class zero < self |
1 | 1262 |
! ! |
1263 |
||
701 | 1264 |
!ArithmeticValue methodsFor:'truncation & rounding'! |
1 | 1265 |
|
701 | 1266 |
ceiling |
1267 |
"return the integer nearest the receiver towards positive infinity." |
|
1 | 1268 |
|
701 | 1269 |
|anInteger| |
1 | 1270 |
|
701 | 1271 |
anInteger := self // 1. "truncates towards negative infinity" |
1272 |
anInteger = self ifTrue:[^ anInteger]. |
|
1273 |
^ anInteger + 1 |
|
1 | 1274 |
! |
1275 |
||
701 | 1276 |
floor |
1277 |
"return the receiver truncated towards negative infinity" |
|
1 | 1278 |
|
701 | 1279 |
^ self // 1 |
1 | 1280 |
! |
1281 |
||
701 | 1282 |
roundTo:aNumber |
1283 |
"return the receiver rounded to multiples of aNumber" |
|
1 | 1284 |
|
701 | 1285 |
^ (self / aNumber) rounded * aNumber |
8449 | 1286 |
|
1287 |
" |
|
10326 | 1288 |
0 roundTo:4 |
8449 | 1289 |
1 roundTo:4 |
1290 |
2 roundTo:4 |
|
1291 |
3 roundTo:4 |
|
1292 |
4 roundTo:4 |
|
1293 |
5 roundTo:4 |
|
1294 |
6 roundTo:4 |
|
1295 |
7 roundTo:4 |
|
14497 | 1296 |
|
1297 |
7.123 roundTo:0.1 |
|
1298 |
7.523 roundTo:0.1 |
|
1299 |
7.583 roundTo:0.1 |
|
1300 |
7.623 roundTo:0.1 |
|
1301 |
7.623 roundTo:0.01 |
|
1302 |
7.628 roundTo:0.01 |
|
8449 | 1303 |
" |
1304 |
! |
|
1305 |
||
1306 |
roundUpTo:aNumber |
|
1307 |
"return the receiver rounded up to the next multiple of aNumber" |
|
1308 |
||
1309 |
^ (self / aNumber) ceiling * aNumber |
|
1310 |
||
1311 |
" |
|
10326 | 1312 |
0 roundUpTo:4 |
1313 |
1 roundUpTo:4 |
|
1314 |
2 roundUpTo:4 |
|
1315 |
3 roundUpTo:4 |
|
1316 |
4 roundUpTo:4 |
|
1317 |
5 roundUpTo:4 |
|
1318 |
6 roundUpTo:4 |
|
1319 |
7 roundUpTo:4 |
|
1320 |
8 roundUpTo:4 |
|
10911 | 1321 |
(3@4) roundUpTo:8 |
1322 |
(3@4) roundUpTo:(5 @ 4) |
|
1323 |
(3@3) roundUpTo:(5 @ 4) |
|
8449 | 1324 |
" |
701 | 1325 |
! |
1326 |
||
1327 |
rounded |
|
1328 |
"return the integer nearest the receiver" |
|
1329 |
||
1881 | 1330 |
self negative ifTrue:[ |
10326 | 1331 |
^ (self - 0.5) ceiling |
1881 | 1332 |
]. |
701 | 1333 |
^ (self + 0.5) floor |
1881 | 1334 |
|
1335 |
"Modified: 5.11.1996 / 11:31:59 / cg" |
|
1 | 1336 |
! |
1337 |
||
701 | 1338 |
truncateTo:aNumber |
1339 |
"return the receiver truncated to multiples of aNumber" |
|
1 | 1340 |
|
701 | 1341 |
^ ((self / aNumber) floor * aNumber) asInteger |
1 | 1342 |
! |
1343 |
||
701 | 1344 |
truncated |
1345 |
"return the receiver truncated towards zero" |
|
1 | 1346 |
|
701 | 1347 |
self negative ifTrue:[ |
1348 |
^ self ceiling |
|
1349 |
]. |
|
1350 |
^ self floor |
|
1351 |
! ! |
|
1 | 1352 |
|
701 | 1353 |
!ArithmeticValue class methodsFor:'documentation'! |
1354 |
||
1355 |
version |
|
14747 | 1356 |
^ '$Header: /cvs/stx/stx/libbasic/ArithmeticValue.st,v 1.91 2013-02-05 14:24:46 cg Exp $' |
12576 | 1357 |
! |
1358 |
||
1359 |
version_CVS |
|
14747 | 1360 |
^ '$Header: /cvs/stx/stx/libbasic/ArithmeticValue.st,v 1.91 2013-02-05 14:24:46 cg Exp $' |
1 | 1361 |
! ! |
6858 | 1362 |
|
14747 | 1363 |
|
701 | 1364 |
ArithmeticValue initialize! |