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