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