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