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