author | Stefan Vogel <sv@exept.de> |
Wed, 01 Sep 2004 16:45:23 +0200 | |
changeset 8508 | 68cd95545196 |
parent 8449 | 9ec3ade5a78b |
child 8633 | 239f2bd03afe |
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 |
|
176 |
^ NotANumber new |
|
177 |
! |
|
178 |
||
179 |
infinity |
|
180 |
^ Infinity positive |
|
181 |
! |
|
182 |
||
7538
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
183 |
nan |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
184 |
"VW compatibility" |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
185 |
|
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
186 |
^ self NaN |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
187 |
! |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
188 |
|
7440 | 189 |
negativeInfinity |
190 |
^ Infinity negative |
|
191 |
! |
|
192 |
||
7726 | 193 |
unity |
194 |
self subclassResponsibility |
|
195 |
! |
|
196 |
||
7440 | 197 |
zero |
198 |
self subclassResponsibility |
|
199 |
! ! |
|
200 |
||
701 | 201 |
!ArithmeticValue methodsFor:'arithmetic'! |
202 |
||
203 |
* something |
|
204 |
"return the product of the receiver and the argument" |
|
205 |
||
206 |
^ self subclassResponsibility |
|
207 |
! |
|
208 |
||
209 |
+ something |
|
210 |
"return the sum of the receiver and the argument" |
|
211 |
||
212 |
^ self subclassResponsibility |
|
213 |
! |
|
214 |
||
215 |
- something |
|
216 |
"return the difference of the receiver and the argument" |
|
217 |
||
218 |
^ self subclassResponsibility |
|
219 |
! |
|
220 |
||
221 |
/ something |
|
222 |
"return the quotient of the receiver and the argument" |
|
1 | 223 |
|
701 | 224 |
^ self subclassResponsibility |
225 |
! |
|
226 |
||
227 |
// something |
|
1881 | 228 |
"return the integer quotient of dividing the receiver by aNumber with |
229 |
truncation towards negative infinity." |
|
701 | 230 |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
231 |
"/ Note: '^ (self / something) floor' may lead to infinite recursion |
1172 | 232 |
|
233 |
^ self subclassResponsibility |
|
234 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
235 |
"Modified: / 5.11.1996 / 11:46:27 / cg" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
236 |
"Modified: / 12.2.1998 / 16:31:36 / stefan" |
701 | 237 |
! |
238 |
||
239 |
\\ something |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
240 |
"return the receiver modulo something. |
6067 | 241 |
The remainder has the same sign as something. |
242 |
The following is always true: |
|
243 |
(receiver // something) * something + (receiver \\ something) = receiver |
|
244 |
" |
|
1 | 245 |
|
701 | 246 |
^ self - ((self // something) * something) |
3134 | 247 |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
248 |
" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
249 |
0.9 \\ 0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
250 |
0.9 \\ -0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
251 |
-0.9 \\ 0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
252 |
-0.9 \\ -0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
253 |
" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
254 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
255 |
"Modified: / 12.2.1998 / 19:14:37 / stefan" |
701 | 256 |
! |
257 |
||
258 |
abs |
|
259 |
"return the absolute value of the receiver" |
|
260 |
||
261 |
(self negative) ifTrue:[^ self negated]. |
|
262 |
^ self |
|
263 |
! |
|
264 |
||
265 |
negated |
|
266 |
"return the receiver negated" |
|
267 |
||
268 |
^ self class zero - self |
|
1 | 269 |
! |
270 |
||
701 | 271 |
quo:something |
272 |
"Return the integer quotient of dividing the receiver by the argument |
|
6067 | 273 |
with truncation towards zero. |
274 |
The following is always true: |
|
275 |
(receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver |
|
276 |
" |
|
701 | 277 |
|
278 |
^ (self / something) truncated |
|
279 |
! |
|
280 |
||
281 |
reciprocal |
|
282 |
"return the receivers reciprocal" |
|
283 |
||
284 |
^ self class unity / self |
|
285 |
! |
|
1 | 286 |
|
701 | 287 |
rem:something |
288 |
"Return the integer remainder of dividing the receiver by the argument |
|
289 |
with truncation towards zero. |
|
6067 | 290 |
The remainder has the same sign as the receiver. |
291 |
The following is always true: |
|
292 |
(receiver quo: something) * something + (receiver rem: something) = receiver |
|
293 |
" |
|
701 | 294 |
|
295 |
^ self - ((self quo:something) * something) |
|
7440 | 296 |
! |
297 |
||
298 |
uncheckedDivide:aNumber |
|
299 |
"return the quotient of the receiver and the argument, aNumber. |
|
7471 | 300 |
Do not check for divide by zero (return NaN or Infinity). |
7440 | 301 |
This operation is provided for emulators of other languages/semantics, |
302 |
where no exception is raised for these results (i.e. Java). |
|
303 |
Its only defined if the arguments type is the same as the receivers." |
|
304 |
||
305 |
aNumber isZero ifTrue:[ |
|
306 |
self isZero ifTrue:[^ self class NaN]. |
|
307 |
self negative ifTrue:[^ self class negativeInfinity]. |
|
308 |
^ self class infinity. |
|
309 |
]. |
|
310 |
^ self / aNumber |
|
701 | 311 |
! ! |
312 |
||
5238 | 313 |
!ArithmeticValue methodsFor:'coercing & converting'! |
1888 | 314 |
|
315 |
coerce:aNumber |
|
316 |
"convert aNumber into an instance of the receivers class and return it." |
|
317 |
||
318 |
^ self subclassResponsibility |
|
319 |
! |
|
320 |
||
321 |
generality |
|
322 |
"return a number giving the receivers generality, that number is |
|
323 |
used to convert one of the arguments in a mixed expression. |
|
324 |
The generality has to be defined in subclasses, |
|
325 |
such that gen(a) > gen(b) iff, conversion of b into a's class |
|
326 |
does not cut precision. For example, Integer has 40, Float has 80, |
|
327 |
meaning that if we convert a Float to an Integer, some precision may |
|
328 |
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
|
329 |
which converts the lower-precision number to the higher precision |
1888 | 330 |
numbers class, when mixed-type arithmetic is performed." |
331 |
||
332 |
^ self subclassResponsibility |
|
333 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
334 |
"Modified: / 5.11.1996 / 15:05:30 / cg" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
335 |
"Modified: / 13.2.1998 / 15:36:01 / stefan" |
1888 | 336 |
! |
337 |
||
338 |
retry:aSymbol coercing:aNumber |
|
339 |
"arithmetic represented by the binary operator, aSymbol, |
|
340 |
could not be performed with the receiver and the argument, aNumber, |
|
341 |
because of the differences in representation. |
|
342 |
Coerce either the receiver or the argument, depending on which has higher |
|
343 |
generality, and try again. |
|
344 |
If the operation is compare for same value (=), return false if |
|
345 |
the argument is not a Number. |
|
346 |
If the generalities are the same, create an error message, since this |
|
347 |
means that a subclass has not been fully implemented." |
|
348 |
||
349 |
|hasGenerality myGenerality otherGenerality| |
|
350 |
||
351 |
hasGenerality := aNumber respondsTo:#generality. |
|
352 |
hasGenerality ifFalse:[ |
|
353 |
(aSymbol == #=) ifTrue:[ |
|
354 |
^ false |
|
355 |
]. |
|
356 |
(aSymbol == #~=) ifTrue:[ |
|
357 |
^ true |
|
358 |
]. |
|
359 |
^ self error:'retry:coercing: argument is not a number'. |
|
360 |
]. |
|
361 |
||
362 |
myGenerality := self generality. |
|
363 |
otherGenerality := aNumber generality. |
|
364 |
(myGenerality > otherGenerality) ifTrue:[ |
|
365 |
^ self perform:aSymbol with:(self coerce:aNumber) |
|
366 |
]. |
|
367 |
(myGenerality < otherGenerality) ifTrue:[ |
|
368 |
^ (aNumber coerce:self) perform:aSymbol with:aNumber |
|
369 |
]. |
|
370 |
self error:'retry:coercing: oops - same generality' |
|
371 |
||
372 |
"Modified: 5.11.1996 / 15:03:38 / cg" |
|
373 |
! ! |
|
374 |
||
701 | 375 |
!ArithmeticValue methodsFor:'converting'! |
376 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
377 |
asDouble |
7429 | 378 |
"ST80 compatibility: return a double with receivers value. |
379 |
our floats are the identical to ST80 doubles" |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
380 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
381 |
^ self asFloat |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
382 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
383 |
"Created: / 13.2.1998 / 15:40:14 / stefan" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
384 |
! |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
385 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
386 |
asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
387 |
"return the receiver as fixedPoint number. |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
388 |
Q: what should the scale be here ?" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
389 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
390 |
^ self asFraction asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
391 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
392 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
393 |
0.3 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
394 |
0.5 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
395 |
(1/5) asFloat asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
396 |
(1/3) asFloat asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
397 |
(2/3) asFloat asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
398 |
(1/8) asFloat asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
399 |
3.14159 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
400 |
0.0000001 asFraction |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
401 |
0.0000001 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
402 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
403 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
404 |
"Modified: / 25.10.1997 / 15:36:54 / cg" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
405 |
! |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
406 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
407 |
asFixedPoint:scale |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
408 |
"return the receiver as fixedPoint number with the given |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
409 |
number of post-decimal-digits." |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
410 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
411 |
^ self asFraction asFixedPoint:scale |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
412 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
413 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
414 |
0.3 asFixedPoint:4 |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
415 |
0.3 asFixedPoint:3 |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
416 |
0.3 asFixedPoint:2 |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
417 |
0.3 asFixedPoint:1 |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
418 |
0.3 asFixedPoint:0 |
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.5 asFixedPoint:3 |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
421 |
(1/5) asFloat asFixedPoint:1 |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
422 |
(1/8) asFloat asFixedPoint:1 |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
423 |
1.0 asFixedPoint:2 |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
424 |
3.14159 asFixedPoint:2 |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
425 |
3.14159 asFixedPoint:3 |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
426 |
(3.14159 asFixedPoint:2) asFixedPoint:5 |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
427 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
428 |
! |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
429 |
|
1 | 430 |
asFloat |
431 |
"return a float with same value" |
|
432 |
||
433 |
^ self subclassResponsibility |
|
434 |
! |
|
435 |
||
5986 | 436 |
asFloatD |
437 |
"return a double precision float with same value. |
|
438 |
Added for ANSI compatibility" |
|
439 |
||
440 |
^ self asFloat |
|
441 |
||
442 |
"Created: / 7.9.2001 / 13:36:48 / cg" |
|
443 |
"Modified: / 7.9.2001 / 13:38:10 / cg" |
|
444 |
! |
|
445 |
||
446 |
asFloatE |
|
447 |
"return a single precision float with same value. |
|
448 |
Added for ANSI compatibility" |
|
449 |
||
450 |
^ self asShortFloat |
|
451 |
||
452 |
"Created: / 7.9.2001 / 13:37:06 / cg" |
|
453 |
"Modified: / 7.9.2001 / 13:38:15 / cg" |
|
454 |
! |
|
455 |
||
456 |
asFloatQ |
|
457 |
"return a quad precision float with same value. |
|
458 |
Added for ANSI compatibility" |
|
459 |
||
460 |
^ self asLongFloat |
|
461 |
||
462 |
"Created: / 7.9.2001 / 13:38:44 / cg" |
|
463 |
! |
|
464 |
||
1 | 465 |
asFraction |
466 |
"return a fraction with same value" |
|
467 |
||
468 |
^ self subclassResponsibility |
|
469 |
! |
|
470 |
||
701 | 471 |
asInteger |
472 |
"return an integer with same value - might truncate" |
|
473 |
||
474 |
^ self truncated |
|
475 |
! |
|
476 |
||
7378 | 477 |
asLimitedPrecisionReal |
478 |
"return a float of any precision with same value" |
|
479 |
||
480 |
^ self asFloat |
|
481 |
! |
|
482 |
||
5986 | 483 |
asLongFloat |
484 |
"return a longFloat with same value" |
|
485 |
||
7355 | 486 |
"WARNING: could loose precision here, if not redefined in concrete classes which |
487 |
have more than float precision (i.e. LargeIntegers and Fractions)" |
|
488 |
||
489 |
^ self asFloat asLongFloat "/ subclassResponsibility |
|
5986 | 490 |
|
491 |
"Modified: / 17.4.1996 / 12:21:35 / cg" |
|
492 |
"Created: / 7.9.2001 / 13:39:31 / cg" |
|
493 |
! |
|
494 |
||
5987 | 495 |
asScaledDecimal:scale |
496 |
"return a fixedPoint approximating the receivers value" |
|
497 |
||
498 |
^ self asFixedPoint:scale |
|
499 |
||
500 |
" |
|
501 |
1.234 asScaledDecimal:2 |
|
502 |
" |
|
503 |
||
504 |
"Created: / 7.9.2001 / 13:46:26 / cg" |
|
505 |
"Modified: / 7.9.2001 / 13:46:44 / cg" |
|
506 |
! |
|
507 |
||
1200 | 508 |
asShortFloat |
509 |
"return a shortFloat with same value" |
|
510 |
||
511 |
^ self asFloat asShortFloat |
|
512 |
||
513 |
"Modified: 17.4.1996 / 12:21:35 / cg" |
|
514 |
! |
|
515 |
||
701 | 516 |
degreesToRadians |
517 |
"interpreting the receiver as radians, return the degrees" |
|
518 |
||
519 |
^ self asFloat degreesToRadians |
|
520 |
! |
|
521 |
||
522 |
radiansToDegrees |
|
523 |
"interpreting the receiver as degrees, return the radians" |
|
524 |
||
525 |
^ self asFloat radiansToDegrees |
|
1 | 526 |
! ! |
527 |
||
4138
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
528 |
!ArithmeticValue methodsFor:'destructive arithmethic'! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
529 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
530 |
*= aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
531 |
"Return the product of self multiplied by aNumber. |
7228 | 532 |
The receiver MAY, but NEED NOT be changed to contain the product. |
533 |
So this method must be used as: 'a := a *= 5'. |
|
534 |
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
|
535 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
536 |
^ self * aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
537 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
538 |
"Created: / 28.4.1999 / 11:46:11 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
539 |
"Modified: / 28.4.1999 / 11:53:28 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
540 |
! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
541 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
542 |
+= aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
543 |
"Return the sum of self and aNumber. |
7228 | 544 |
The receiver MAY, but NEED NOT be changed to contain the sum. |
545 |
So this method must be used as: 'a := a += 5'. |
|
546 |
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
|
547 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
548 |
^ self + aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
549 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
550 |
"Created: / 28.4.1999 / 10:13:41 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
551 |
"Modified: / 28.4.1999 / 11:54:11 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
552 |
! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
553 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
554 |
-= aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
555 |
"Return the difference of self and aNumber. |
7228 | 556 |
The receiver MAY, but NEED NOT be changed to contain the difference. |
557 |
So this method must be used as: 'a := a -= 5'. |
|
558 |
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
|
559 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
560 |
^ self - aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
561 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
562 |
"Created: / 28.4.1999 / 10:13:58 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
563 |
"Modified: / 28.4.1999 / 11:54:37 / stefan" |
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 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
566 |
/= aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
567 |
"Return the quotient of self and aNumber. |
7228 | 568 |
The receiver MAY, but NEED NOT be changed to contain the quotient. |
569 |
So this method must be used as: 'a := a /= 5'. |
|
570 |
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
|
571 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
572 |
^ self / aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
573 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
574 |
"Created: / 28.4.1999 / 11:46:22 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
575 |
"Modified: / 28.4.1999 / 11:55:06 / stefan" |
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 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
578 |
div2 |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
579 |
"Return the quotient of self divided by 2. |
7228 | 580 |
The receiver MAY, but NEED NOT be changed to contain the result. |
581 |
So this method must be used as: 'a := a div2. |
|
582 |
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
|
583 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
584 |
^ self // 2 |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
585 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
586 |
"Created: / 28.4.1999 / 10:12:44 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
587 |
"Modified: / 28.4.1999 / 11:56:09 / stefan" |
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 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
590 |
mul2 |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
591 |
"Return the product of self multiplied by 2. |
7228 | 592 |
The receiver MAY, but NEED NOT be changed to contain the result. |
593 |
So this method must be used as: 'a := a mul2. |
|
594 |
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
|
595 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
596 |
^ self * 2 |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
597 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
598 |
"Created: / 28.4.1999 / 10:12:55 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
599 |
"Modified: / 28.4.1999 / 11:56:38 / stefan" |
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 |
|
701 | 602 |
!ArithmeticValue methodsFor:'double dispatching'! |
1 | 603 |
|
7355 | 604 |
differenceFromComplex:aComplex |
605 |
"the receiver does not know how to subtract from a complex - |
|
606 |
retry the operation by coercing to higher generality" |
|
607 |
||
608 |
^ aComplex retry:#- coercing:self |
|
609 |
! |
|
610 |
||
1888 | 611 |
differenceFromFixedPoint:aFixedPoint |
612 |
"the receiver does not know how to subtract from a fixedPoint number - |
|
613 |
retry the operation by coercing to higher generality" |
|
614 |
||
615 |
^ aFixedPoint retry:#- coercing:self |
|
616 |
||
617 |
"Created: 5.11.1996 / 14:55:51 / cg" |
|
618 |
! |
|
619 |
||
701 | 620 |
differenceFromFloat:aFloat |
621 |
"the receiver does not know how to subtract from a float - |
|
622 |
retry the operation by coercing to higher generality" |
|
1 | 623 |
|
701 | 624 |
^ aFloat retry:#- coercing:self |
1 | 625 |
! |
626 |
||
701 | 627 |
differenceFromFraction:aFraction |
628 |
"the receiver does not know how to subtract from a fraction - |
|
629 |
retry the operation by coercing to higher generality" |
|
1 | 630 |
|
701 | 631 |
^ aFraction retry:#- coercing:self |
1 | 632 |
! |
633 |
||
701 | 634 |
differenceFromInteger:anInteger |
635 |
"the receiver does not know how to subtract from an integer - |
|
636 |
retry the operation by coercing to higher generality" |
|
1 | 637 |
|
701 | 638 |
^ anInteger retry:#- coercing:self |
1 | 639 |
! |
640 |
||
7440 | 641 |
differenceFromLargeFloat:aLargeFloat |
642 |
"the receiver does not know how to subtract from a largeFloat - |
|
643 |
retry the operation by coercing to higher generality" |
|
644 |
||
645 |
^ aLargeFloat retry:#- coercing:self |
|
646 |
! |
|
647 |
||
7399 | 648 |
differenceFromLongFloat:aLongFloat |
649 |
"the receiver does not know how to subtract from a longFloat - |
|
650 |
retry the operation by coercing to higher generality" |
|
651 |
||
652 |
^ aLongFloat retry:#- coercing:self |
|
653 |
||
654 |
"Created: 17.4.1996 / 12:33:16 / cg" |
|
655 |
! |
|
656 |
||
1200 | 657 |
differenceFromShortFloat:aShortFloat |
658 |
"the receiver does not know how to subtract from a shortFloat - |
|
659 |
retry the operation by coercing to higher generality" |
|
660 |
||
661 |
^ aShortFloat retry:#- coercing:self |
|
662 |
||
663 |
"Created: 17.4.1996 / 12:33:16 / cg" |
|
664 |
! |
|
665 |
||
7355 | 666 |
equalFromComplex:aComplex |
667 |
"the receiver does not know how to compare to a complex number - |
|
668 |
retry the operation by coercing to higher generality" |
|
669 |
||
670 |
^ aComplex retry:#= coercing:self |
|
671 |
! |
|
672 |
||
673 |
equalFromFixedPoint:aFixedPoint |
|
674 |
"the receiver does not know how to compare to a fixed point - |
|
675 |
retry the operation by coercing to higher generality" |
|
676 |
||
677 |
^ aFixedPoint retry:#= coercing:self |
|
678 |
! |
|
679 |
||
680 |
equalFromFloat:aFloat |
|
681 |
"the receiver does not know how to compare to a float - |
|
682 |
retry the operation by coercing to higher generality" |
|
683 |
||
684 |
^ aFloat retry:#= coercing:self |
|
685 |
! |
|
686 |
||
687 |
equalFromFraction:aFraction |
|
688 |
"the receiver does not know how to compare to a fraction - |
|
689 |
retry the operation by coercing to higher generality" |
|
690 |
||
691 |
^ aFraction retry:#= coercing:self |
|
692 |
! |
|
693 |
||
694 |
equalFromInteger:anInteger |
|
695 |
"the receiver does not know how to compare to an integer - |
|
696 |
retry the operation by coercing to higher generality" |
|
697 |
||
698 |
^ anInteger retry:#= coercing:self |
|
699 |
! |
|
700 |
||
7440 | 701 |
equalFromLargeFloat:aLargeFloat |
702 |
"the receiver does not know how to compare to a large float - |
|
703 |
retry the operation by coercing to higher generality" |
|
704 |
||
705 |
^ aLargeFloat retry:#= coercing:self |
|
706 |
! |
|
707 |
||
7355 | 708 |
equalFromLongFloat:aLongFloat |
709 |
"the receiver does not know how to compare to a long float - |
|
710 |
retry the operation by coercing to higher generality" |
|
711 |
||
712 |
^ aLongFloat retry:#= coercing:self |
|
713 |
! |
|
714 |
||
715 |
equalFromShortFloat:aShortFloat |
|
716 |
"the receiver does not know how to compare to a short float - |
|
717 |
retry the operation by coercing to higher generality" |
|
718 |
||
719 |
^ aShortFloat retry:#= coercing:self |
|
720 |
! |
|
721 |
||
1888 | 722 |
lessFromFixedPoint:aFixedPoint |
723 |
"the receiver does not know how to compare to a fixedPoint number - |
|
724 |
retry the operation by coercing to higher generality" |
|
725 |
||
726 |
^ aFixedPoint retry:#< coercing:self |
|
727 |
||
728 |
"Created: 5.11.1996 / 14:56:12 / cg" |
|
729 |
! |
|
730 |
||
701 | 731 |
lessFromFloat:aFloat |
732 |
"the receiver does not know how to compare to a float - |
|
733 |
retry the operation by coercing to higher generality" |
|
1 | 734 |
|
701 | 735 |
^ aFloat retry:#< coercing:self |
1 | 736 |
! |
737 |
||
701 | 738 |
lessFromFraction:aFraction |
739 |
"the receiver does not know how to compare to a fraction - |
|
740 |
retry the operation by coercing to higher generality" |
|
1 | 741 |
|
701 | 742 |
^ aFraction retry:#< coercing:self |
1 | 743 |
! |
744 |
||
701 | 745 |
lessFromInteger:anInteger |
746 |
"the receiver does not know how to compare to an integer - |
|
747 |
retry the operation by coercing to higher generality" |
|
1 | 748 |
|
701 | 749 |
^ anInteger retry:#< coercing:self |
1 | 750 |
! |
751 |
||
7440 | 752 |
lessFromLargeFloat:aLargeFloat |
753 |
"the receiver does not know how to compare to a largeFloat - |
|
754 |
retry the operation by coercing to higher generality" |
|
755 |
||
756 |
^ aLargeFloat retry:#< coercing:self |
|
757 |
! |
|
758 |
||
7399 | 759 |
lessFromLongFloat:aLongFloat |
760 |
"the receiver does not know how to compare to a longFloat - |
|
761 |
retry the operation by coercing to higher generality" |
|
762 |
||
763 |
^ aLongFloat retry:#< coercing:self |
|
764 |
||
765 |
"Modified: 17.4.1996 / 12:33:33 / cg" |
|
766 |
! |
|
767 |
||
1200 | 768 |
lessFromShortFloat:aShortFloat |
769 |
"the receiver does not know how to compare to a shortFloat - |
|
770 |
retry the operation by coercing to higher generality" |
|
771 |
||
772 |
^ aShortFloat retry:#< coercing:self |
|
773 |
||
774 |
"Modified: 17.4.1996 / 12:33:33 / cg" |
|
775 |
! |
|
776 |
||
7355 | 777 |
productFromComplex:aComplex |
778 |
"the receiver does not know how to multiply a complex - |
|
779 |
retry the operation by coercing to higher generality" |
|
780 |
||
781 |
^ aComplex retry:#* coercing:self |
|
782 |
! |
|
783 |
||
1888 | 784 |
productFromFixedPoint:aFixedPoint |
785 |
"the receiver does not know how to multiply a fixed point number - |
|
786 |
retry the operation by coercing to higher generality" |
|
787 |
||
788 |
^ aFixedPoint retry:#* coercing:self |
|
789 |
||
790 |
"Created: 5.11.1996 / 14:56:28 / cg" |
|
791 |
! |
|
792 |
||
701 | 793 |
productFromFloat:aFloat |
794 |
"the receiver does not know how to multiply a float - |
|
795 |
retry the operation by coercing to higher generality" |
|
1 | 796 |
|
701 | 797 |
^ aFloat retry:#* coercing:self |
1 | 798 |
! |
799 |
||
701 | 800 |
productFromFraction:aFraction |
801 |
"the receiver does not know how to multiply a fraction - |
|
802 |
retry the operation by coercing to higher generality" |
|
1 | 803 |
|
701 | 804 |
^ aFraction retry:#* coercing:self |
1 | 805 |
! |
806 |
||
701 | 807 |
productFromInteger:anInteger |
808 |
"the receiver does not know how to multiply an integer - |
|
809 |
retry the operation by coercing to higher generality" |
|
1 | 810 |
|
701 | 811 |
^ anInteger retry:#* coercing:self |
1 | 812 |
! |
813 |
||
7440 | 814 |
productFromLargeFloat:aLargeFloat |
815 |
"the receiver does not know how to multiply a largeFloat - |
|
816 |
retry the operation by coercing to higher generality" |
|
817 |
||
818 |
^ aLargeFloat retry:#* coercing:self |
|
819 |
||
820 |
"Created: 17.4.1996 / 12:33:48 / cg" |
|
821 |
! |
|
822 |
||
7399 | 823 |
productFromLongFloat:aLongFloat |
824 |
"the receiver does not know how to multiply a longFloat - |
|
825 |
retry the operation by coercing to higher generality" |
|
826 |
||
827 |
^ aLongFloat retry:#* coercing:self |
|
828 |
||
829 |
"Created: 17.4.1996 / 12:33:48 / cg" |
|
830 |
! |
|
831 |
||
1200 | 832 |
productFromShortFloat:aShortFloat |
833 |
"the receiver does not know how to multiply a shortFloat - |
|
834 |
retry the operation by coercing to higher generality" |
|
835 |
||
836 |
^ aShortFloat retry:#* coercing:self |
|
837 |
||
838 |
"Created: 17.4.1996 / 12:33:48 / cg" |
|
839 |
! |
|
840 |
||
7355 | 841 |
quotientFromComplex:aComplex |
7440 | 842 |
"aComplex does not know how to divide by the receiver - |
7355 | 843 |
retry the operation by coercing to higher generality" |
844 |
||
845 |
^ aComplex retry:#/ coercing:self |
|
846 |
! |
|
847 |
||
1888 | 848 |
quotientFromFixedPoint:aFixedPoint |
7440 | 849 |
"aFixedPoint does not know how to divide by the receiver - |
1888 | 850 |
retry the operation by coercing to higher generality" |
851 |
||
852 |
^ aFixedPoint retry:#/ coercing:self |
|
853 |
||
854 |
"Created: 5.11.1996 / 14:56:41 / cg" |
|
855 |
! |
|
856 |
||
701 | 857 |
quotientFromFloat:aFloat |
7440 | 858 |
"aFloat does not know how to divide by the receiver - |
701 | 859 |
retry the operation by coercing to higher generality" |
1 | 860 |
|
701 | 861 |
^ aFloat retry:#/ coercing:self |
1 | 862 |
! |
863 |
||
701 | 864 |
quotientFromFraction:aFraction |
7440 | 865 |
"aFraction does not know how to divide by the receiver - |
701 | 866 |
retry the operation by coercing to higher generality" |
1 | 867 |
|
701 | 868 |
^ aFraction retry:#/ coercing:self |
1 | 869 |
! |
870 |
||
701 | 871 |
quotientFromInteger:anInteger |
7440 | 872 |
"anInteger does not know how to divide by the receiver - |
1 | 873 |
retry the operation by coercing to higher generality" |
874 |
||
701 | 875 |
^ anInteger retry:#/ coercing:self |
1 | 876 |
! |
877 |
||
7440 | 878 |
quotientFromLargeFloat:aLargeFloat |
879 |
"aLargeFloat does not know how to divide by the receiver - |
|
880 |
retry the operation by coercing to higher generality" |
|
881 |
||
882 |
^ aLargeFloat retry:#/ coercing:self |
|
883 |
||
884 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
885 |
! |
|
886 |
||
7399 | 887 |
quotientFromLongFloat:aLongFloat |
7440 | 888 |
"aLongFloat does not know how to divide by the receiver - |
7399 | 889 |
retry the operation by coercing to higher generality" |
890 |
||
891 |
^ aLongFloat retry:#/ coercing:self |
|
892 |
||
893 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
894 |
! |
|
895 |
||
1200 | 896 |
quotientFromShortFloat:aShortFloat |
7440 | 897 |
"aShortFloat does not know how to divide by the receiver - |
1200 | 898 |
retry the operation by coercing to higher generality" |
899 |
||
900 |
^ aShortFloat retry:#/ coercing:self |
|
901 |
||
902 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
903 |
! |
|
904 |
||
7355 | 905 |
sumFromComplex:aComplex |
906 |
"the receiver does not know how to add a complex - |
|
907 |
retry the operation by coercing to higher generality" |
|
908 |
||
909 |
^ aComplex retry:#+ coercing:self |
|
910 |
! |
|
911 |
||
1888 | 912 |
sumFromFixedPoint:aFixedPoint |
913 |
"the receiver does not know how to add a fixed point number - |
|
914 |
retry the operation by coercing to higher generality" |
|
915 |
||
916 |
^ aFixedPoint retry:#+ coercing:self |
|
917 |
||
918 |
"Created: 5.11.1996 / 14:56:56 / cg" |
|
919 |
! |
|
920 |
||
1 | 921 |
sumFromFloat:aFloat |
922 |
"the receiver does not know how to add a float - |
|
923 |
retry the operation by coercing to higher generality" |
|
924 |
||
925 |
^ aFloat retry:#+ coercing:self |
|
926 |
! |
|
927 |
||
928 |
sumFromFraction:aFraction |
|
929 |
"the receiver does not know how to add a fraction - |
|
930 |
retry the operation by coercing to higher generality" |
|
931 |
||
932 |
^ aFraction retry:#+ coercing:self |
|
933 |
! |
|
934 |
||
701 | 935 |
sumFromInteger:anInteger |
936 |
"the receiver does not know how to add an integer - |
|
1 | 937 |
retry the operation by coercing to higher generality" |
938 |
||
701 | 939 |
^ anInteger retry:#+ coercing:self |
1200 | 940 |
! |
941 |
||
7440 | 942 |
sumFromLargeFloat:aLargeFloat |
943 |
"the receiver does not know how to add a largeFloat - |
|
944 |
retry the operation by coercing to higher generality" |
|
945 |
||
946 |
^ aLargeFloat retry:#+ coercing:self |
|
947 |
||
948 |
"Created: 17.4.1996 / 12:34:10 / cg" |
|
949 |
! |
|
950 |
||
7399 | 951 |
sumFromLongFloat:aLongFloat |
952 |
"the receiver does not know how to add a longFloat - |
|
953 |
retry the operation by coercing to higher generality" |
|
954 |
||
955 |
^ aLongFloat retry:#+ coercing:self |
|
956 |
||
957 |
"Created: 17.4.1996 / 12:34:10 / cg" |
|
958 |
! |
|
959 |
||
1200 | 960 |
sumFromShortFloat:aShortFloat |
961 |
"the receiver does not know how to add a shortFloat - |
|
962 |
retry the operation by coercing to higher generality" |
|
963 |
||
964 |
^ aShortFloat retry:#+ coercing:self |
|
965 |
||
966 |
"Created: 17.4.1996 / 12:34:10 / cg" |
|
1 | 967 |
! ! |
968 |
||
7384 | 969 |
!ArithmeticValue methodsFor:'mathematical functions'! |
1 | 970 |
|
7635 | 971 |
raisedTo: aNumber |
972 |
^ self subclassResponsibility |
|
6858 | 973 |
! |
974 |
||
701 | 975 |
squared |
976 |
"return receiver * receiver" |
|
977 |
||
978 |
^ self * self |
|
979 |
! ! |
|
980 |
||
981 |
!ArithmeticValue methodsFor:'queries'! |
|
982 |
||
983 |
respondsToArithmetic |
|
984 |
"return true, if the receiver responds to arithmetic messages" |
|
985 |
||
986 |
^ true |
|
987 |
! ! |
|
988 |
||
989 |
!ArithmeticValue methodsFor:'testing'! |
|
990 |
||
991 |
denominator |
|
992 |
"return the denominator of the receiver" |
|
993 |
||
994 |
^ 1 |
|
995 |
! |
|
996 |
||
997 |
even |
|
998 |
"return true if the receiver is divisible by 2" |
|
999 |
||
1000 |
^ self truncated asInteger even |
|
1001 |
! |
|
1002 |
||
3635 | 1003 |
isComplex |
7355 | 1004 |
"Answer whether the receiver has an imaginary part |
1005 |
(i.e. if it is a complex number). Always false here." |
|
3635 | 1006 |
|
1007 |
^ false |
|
1008 |
||
1009 |
"Modified: / 9.7.1998 / 10:19:27 / cg" |
|
1010 |
! |
|
1011 |
||
7440 | 1012 |
isNegativeInfinity |
1013 |
^ self negative and:[self isInfinite] |
|
1014 |
! |
|
1015 |
||
1016 |
isNegativeZero |
|
1017 |
"return false - must be redefined by subclasses which can represent a negative zero |
|
1018 |
(i.e. limitedPrecisionReal classes)" |
|
1019 |
||
1020 |
^ false |
|
1021 |
! |
|
1022 |
||
1023 |
isPositiveInfinity |
|
1024 |
^ self positive and:[self isInfinite] |
|
1025 |
! |
|
1026 |
||
6156 | 1027 |
isReal |
1028 |
"return true, if the receiver is some kind of real number (as opposed to a complex); |
|
1029 |
false is returned here - the method is only redefined in Number (and Complex)." |
|
1030 |
||
1031 |
^ false |
|
1032 |
||
1033 |
"Modified: / 6.11.2001 / 13:20:20 / cg" |
|
1034 |
"Created: / 6.11.2001 / 13:26:29 / cg" |
|
1035 |
! |
|
1036 |
||
7440 | 1037 |
isZero |
1038 |
"return false - must be redefined by subclasses which can represent a negative zero |
|
1039 |
(i.e. limitedPrecisionReal classes)" |
|
701 | 1040 |
|
7440 | 1041 |
^ self = self class zero |
1042 |
! |
|
1043 |
||
1044 |
negative |
|
1045 |
"return true if the receiver is less than zero" |
|
1046 |
||
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
1047 |
^ self < self class zero |
701 | 1048 |
! |
1049 |
||
1050 |
numerator |
|
1051 |
"return the numerator of the receiver." |
|
1052 |
||
1053 |
^ self |
|
1054 |
! |
|
1055 |
||
1056 |
odd |
|
1057 |
"return true if the receiver is not divisible by 2" |
|
1058 |
||
1059 |
^ self even not |
|
1060 |
! |
|
1061 |
||
1062 |
positive |
|
1063 |
"return true, if the receiver is >= 0" |
|
1064 |
||
1065 |
^ self negative not |
|
1066 |
! |
|
1067 |
||
1068 |
sign |
|
7471 | 1069 |
"return the sign of the receiver (-1, 0 or 1)" |
701 | 1070 |
|
7440 | 1071 |
|zero| |
1072 |
||
1073 |
zero := self class zero. |
|
1074 |
(self < zero) ifTrue:[^ -1]. |
|
1075 |
(zero < self) ifTrue:[^ 1]. |
|
701 | 1076 |
^ 0 |
1077 |
! |
|
1078 |
||
1079 |
strictlyPositive |
|
1080 |
"return true, if the receiver is > 0" |
|
1081 |
||
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
1082 |
^ self class zero < self |
1 | 1083 |
! ! |
1084 |
||
701 | 1085 |
!ArithmeticValue methodsFor:'truncation & rounding'! |
1 | 1086 |
|
701 | 1087 |
ceiling |
1088 |
"return the integer nearest the receiver towards positive infinity." |
|
1 | 1089 |
|
701 | 1090 |
|anInteger| |
1 | 1091 |
|
701 | 1092 |
anInteger := self // 1. "truncates towards negative infinity" |
1093 |
anInteger = self ifTrue:[^ anInteger]. |
|
1094 |
^ anInteger + 1 |
|
1 | 1095 |
! |
1096 |
||
701 | 1097 |
floor |
1098 |
"return the receiver truncated towards negative infinity" |
|
1 | 1099 |
|
701 | 1100 |
^ self // 1 |
1 | 1101 |
! |
1102 |
||
701 | 1103 |
roundTo:aNumber |
1104 |
"return the receiver rounded to multiples of aNumber" |
|
1 | 1105 |
|
701 | 1106 |
^ (self / aNumber) rounded * aNumber |
8449 | 1107 |
|
1108 |
" |
|
1109 |
0 roundTo:4 |
|
1110 |
1 roundTo:4 |
|
1111 |
2 roundTo:4 |
|
1112 |
3 roundTo:4 |
|
1113 |
4 roundTo:4 |
|
1114 |
5 roundTo:4 |
|
1115 |
6 roundTo:4 |
|
1116 |
7 roundTo:4 |
|
1117 |
" |
|
1118 |
! |
|
1119 |
||
1120 |
roundUpTo:aNumber |
|
1121 |
"return the receiver rounded up to the next multiple of aNumber" |
|
1122 |
||
1123 |
^ (self / aNumber) ceiling * aNumber |
|
1124 |
||
1125 |
" |
|
1126 |
0 roundUpTo:4 |
|
1127 |
1 roundUpTo:4 |
|
1128 |
2 roundUpTo:4 |
|
1129 |
3 roundUpTo:4 |
|
1130 |
4 roundUpTo:4 |
|
1131 |
5 roundUpTo:4 |
|
1132 |
6 roundUpTo:4 |
|
1133 |
7 roundUpTo:4 |
|
1134 |
8 roundUpTo:4 |
|
1135 |
" |
|
701 | 1136 |
! |
1137 |
||
1138 |
rounded |
|
1139 |
"return the integer nearest the receiver" |
|
1140 |
||
1881 | 1141 |
self negative ifTrue:[ |
1142 |
^ (self - 0.5) ceiling |
|
1143 |
]. |
|
701 | 1144 |
^ (self + 0.5) floor |
1881 | 1145 |
|
1146 |
"Modified: 5.11.1996 / 11:31:59 / cg" |
|
1 | 1147 |
! |
1148 |
||
701 | 1149 |
truncateTo:aNumber |
1150 |
"return the receiver truncated to multiples of aNumber" |
|
1 | 1151 |
|
701 | 1152 |
^ ((self / aNumber) floor * aNumber) asInteger |
1 | 1153 |
! |
1154 |
||
701 | 1155 |
truncated |
1156 |
"return the receiver truncated towards zero" |
|
1 | 1157 |
|
701 | 1158 |
self negative ifTrue:[ |
1159 |
^ self ceiling |
|
1160 |
]. |
|
1161 |
^ self floor |
|
1162 |
! ! |
|
1 | 1163 |
|
701 | 1164 |
!ArithmeticValue class methodsFor:'documentation'! |
1165 |
||
1166 |
version |
|
8449 | 1167 |
^ '$Header: /cvs/stx/stx/libbasic/ArithmeticValue.st,v 1.66 2004-07-14 14:54:59 cg Exp $' |
1 | 1168 |
! ! |
6858 | 1169 |
|
701 | 1170 |
ArithmeticValue initialize! |