author | Stefan Vogel <sv@exept.de> |
Wed, 19 Dec 2001 14:56:24 +0100 | |
changeset 6342 | fbbe62d5b17c |
parent 6156 | ff2df95a69bf |
child 6691 | 48ba39288455 |
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:] |
50 |
ArithmeticSignal <Signal> parent of all arithmetic signals |
|
51 |
(never raised itself) |
|
5957 | 52 |
New: now a reference to ArithmeticError |
82 | 53 |
|
1283 | 54 |
DomainErrorSignal <Signal> raised upon float errors |
55 |
(for example range in trigonometric) |
|
5957 | 56 |
New: now a reference to DomainError |
1283 | 57 |
|
58 |
DivisionByZeroSignal <Signal> raised when division by 0 is attempted |
|
5957 | 59 |
New: now a reference to ZeroDivide |
155 | 60 |
|
1283 | 61 |
OverflowSignal <Signal> raised on overflow/underflow conditions |
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:'initialization'! |
1 | 77 |
|
5954 | 78 |
initialize |
1 | 79 |
"setup the signals" |
80 |
||
5948 | 81 |
ArithmeticSignal := ArithmeticError. |
82 |
ArithmeticError notifierString:'arithmetic error'. |
|
83 |
||
84 |
DomainErrorSignal := DomainError. |
|
85 |
DomainError notifierString:'domain error'. |
|
86 |
||
87 |
RangeError notifierString:'numeric range error'. |
|
88 |
||
89 |
DivisionByZeroSignal := ZeroDivide. |
|
90 |
ZeroDivide notifierString:'division by zero'. |
|
4453
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
91 |
|
5957 | 92 |
UnorderedSignal := UnorderedNumbersError. |
93 |
UnorderedNumbersError notifierString:'unordered'. |
|
94 |
||
5948 | 95 |
"/ ArithmeticSignal isNil ifTrue:[ |
96 |
"/ ArithmeticSignal := ErrorSignal newSignalMayProceed:true. |
|
97 |
"/ ArithmeticSignal nameClass:self message:#arithmeticSignal. |
|
98 |
"/ ArithmeticSignal notifierString:'arithmetic error'. |
|
99 |
"/ ]. |
|
155 | 100 |
|
5948 | 101 |
"/ DomainErrorSignal isNil ifTrue:[ |
102 |
"/ DomainErrorSignal := ArithmeticSignal newSignal. |
|
103 |
"/ DomainErrorSignal nameClass:self message:#domainErrorSignal. |
|
104 |
"/ DomainErrorSignal notifierString:'domain error'. |
|
105 |
"/ ]. |
|
106 |
||
107 |
ConversionErrorSignal isNil ifTrue:[ |
|
4995
9556c41783f5
added a conversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4453
diff
changeset
|
108 |
ConversionErrorSignal := ArithmeticSignal newSignal. |
9556c41783f5
added a conversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4453
diff
changeset
|
109 |
ConversionErrorSignal nameClass:self message:#conversionErrorSignal. |
9556c41783f5
added a conversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4453
diff
changeset
|
110 |
ConversionErrorSignal notifierString:'conversion error'. |
5948 | 111 |
]. |
4995
9556c41783f5
added a conversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4453
diff
changeset
|
112 |
|
5948 | 113 |
"/ DivisionByZeroSignal isNil ifTrue:[ |
114 |
"/ DivisionByZeroSignal := ArithmeticSignal newSignal. |
|
115 |
"/ DivisionByZeroSignal nameClass:self message:#divisionByZeroSignal. |
|
116 |
"/ DivisionByZeroSignal notifierString:'division by zero'. |
|
117 |
"/ ]. |
|
1 | 118 |
|
5948 | 119 |
OverflowSignal isNil ifTrue:[ |
120 |
OverflowSignal := RangeError newSignal. |
|
4453
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
121 |
OverflowSignal nameClass:self message:#overflowSignal. |
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
122 |
OverflowSignal notifierString:'overflow'. |
5948 | 123 |
]. |
1 | 124 |
|
5948 | 125 |
UnderflowSignal isNil ifTrue:[ |
126 |
UnderflowSignal := RangeError newSignal. |
|
4453
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
127 |
UnderflowSignal nameClass:self message:#underflowSignal. |
721c9dd0e0c6
Arithmethic signals are proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4138
diff
changeset
|
128 |
UnderflowSignal notifierString:'underflow'. |
5948 | 129 |
]. |
54 | 130 |
|
5957 | 131 |
"/ UnorderedSignal isNil ifTrue:[ |
132 |
"/ UnorderedSignal := ArithmeticSignal newSignal. |
|
133 |
"/ UnorderedSignal nameClass:self message:#unorderedSignal. |
|
134 |
"/ UnorderedSignal notifierString:'unordered'. |
|
135 |
"/ ]. |
|
1 | 136 |
! ! |
137 |
||
701 | 138 |
!ArithmeticValue class methodsFor:'Signal constants'! |
139 |
||
140 |
arithmeticSignal |
|
141 |
"return the parent of all arithmetic signals" |
|
142 |
||
143 |
^ ArithmeticSignal |
|
144 |
! |
|
145 |
||
4995
9556c41783f5
added a conversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4453
diff
changeset
|
146 |
conversionErrorSignal |
9556c41783f5
added a conversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4453
diff
changeset
|
147 |
"return the signal which is raised when a conversion fails |
9556c41783f5
added a conversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4453
diff
changeset
|
148 |
(such as bcd decoding of an invalid BCD number)" |
9556c41783f5
added a conversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4453
diff
changeset
|
149 |
|
9556c41783f5
added a conversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4453
diff
changeset
|
150 |
^ ConversionErrorSignal |
9556c41783f5
added a conversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4453
diff
changeset
|
151 |
|
9556c41783f5
added a conversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4453
diff
changeset
|
152 |
"Created: / 15.11.1999 / 20:34:18 / cg" |
9556c41783f5
added a conversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4453
diff
changeset
|
153 |
! |
9556c41783f5
added a conversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4453
diff
changeset
|
154 |
|
701 | 155 |
divisionByZeroSignal |
156 |
"return the signal which is raised on division by zero" |
|
157 |
||
158 |
^ DivisionByZeroSignal |
|
159 |
! |
|
1 | 160 |
|
161 |
domainErrorSignal |
|
162 |
"return the signal which is raised on math errors |
|
163 |
(such as log of 0 etc.)" |
|
164 |
||
165 |
^ DomainErrorSignal |
|
166 |
! |
|
167 |
||
168 |
overflowSignal |
|
169 |
"return the signal which is raised on overflow conditions (in floats)" |
|
170 |
||
171 |
^ OverflowSignal |
|
172 |
! |
|
173 |
||
5955 | 174 |
rangeErrorSignal |
175 |
"return the parent of the overflow/underflow signals" |
|
176 |
||
177 |
^ RangeError |
|
178 |
! |
|
179 |
||
1 | 180 |
underflowSignal |
181 |
"return the signal which is raised on underflow conditions (in floats)" |
|
182 |
||
183 |
^ UnderflowSignal |
|
184 |
! |
|
185 |
||
345 | 186 |
unorderedSignal |
187 |
"return the signal which is raised when numbers are compared, |
|
188 |
for which no ordering is defined (for example: complex numbers)" |
|
189 |
||
3393
f07a758f8c69
oops - unorderedSignal did not
Claus Gittinger <cg@exept.de>
parents:
3134
diff
changeset
|
190 |
^ UnorderedSignal |
1 | 191 |
! ! |
192 |
||
701 | 193 |
!ArithmeticValue methodsFor:'arithmetic'! |
194 |
||
195 |
* something |
|
196 |
"return the product of the receiver and the argument" |
|
197 |
||
198 |
^ self subclassResponsibility |
|
199 |
! |
|
200 |
||
201 |
+ something |
|
202 |
"return the sum of the receiver and the argument" |
|
203 |
||
204 |
^ self subclassResponsibility |
|
205 |
! |
|
206 |
||
207 |
- something |
|
208 |
"return the difference of the receiver and the argument" |
|
209 |
||
210 |
^ self subclassResponsibility |
|
211 |
! |
|
212 |
||
213 |
/ something |
|
214 |
"return the quotient of the receiver and the argument" |
|
1 | 215 |
|
701 | 216 |
^ self subclassResponsibility |
217 |
! |
|
218 |
||
219 |
// something |
|
1881 | 220 |
"return the integer quotient of dividing the receiver by aNumber with |
221 |
truncation towards negative infinity." |
|
701 | 222 |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
223 |
"/ Note: '^ (self / something) floor' may lead to infinite recursion |
1172 | 224 |
|
225 |
^ self subclassResponsibility |
|
226 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
227 |
"Modified: / 5.11.1996 / 11:46:27 / cg" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
228 |
"Modified: / 12.2.1998 / 16:31:36 / stefan" |
701 | 229 |
! |
230 |
||
231 |
\\ something |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
232 |
"return the receiver modulo something. |
6067 | 233 |
The remainder has the same sign as something. |
234 |
The following is always true: |
|
235 |
(receiver // something) * something + (receiver \\ something) = receiver |
|
236 |
" |
|
1 | 237 |
|
701 | 238 |
^ self - ((self // something) * something) |
3134 | 239 |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
240 |
" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
241 |
0.9 \\ 0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
242 |
0.9 \\ -0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
243 |
-0.9 \\ 0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
244 |
-0.9 \\ -0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
245 |
" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
246 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
247 |
"Modified: / 12.2.1998 / 19:14:37 / stefan" |
701 | 248 |
! |
249 |
||
250 |
abs |
|
251 |
"return the absolute value of the receiver" |
|
252 |
||
253 |
(self negative) ifTrue:[^ self negated]. |
|
254 |
^ self |
|
255 |
! |
|
256 |
||
257 |
negated |
|
258 |
"return the receiver negated" |
|
259 |
||
260 |
^ self class zero - self |
|
1 | 261 |
! |
262 |
||
701 | 263 |
quo:something |
264 |
"Return the integer quotient of dividing the receiver by the argument |
|
6067 | 265 |
with truncation towards zero. |
266 |
The following is always true: |
|
267 |
(receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver |
|
268 |
" |
|
701 | 269 |
|
270 |
^ (self / something) truncated |
|
271 |
! |
|
272 |
||
273 |
reciprocal |
|
274 |
"return the receivers reciprocal" |
|
275 |
||
276 |
^ self class unity / self |
|
277 |
! |
|
1 | 278 |
|
701 | 279 |
rem:something |
280 |
"Return the integer remainder of dividing the receiver by the argument |
|
281 |
with truncation towards zero. |
|
6067 | 282 |
The remainder has the same sign as the receiver. |
283 |
The following is always true: |
|
284 |
(receiver quo: something) * something + (receiver rem: something) = receiver |
|
285 |
" |
|
701 | 286 |
|
287 |
^ self - ((self quo:something) * something) |
|
288 |
! ! |
|
289 |
||
5238 | 290 |
!ArithmeticValue methodsFor:'coercing & converting'! |
1888 | 291 |
|
292 |
coerce:aNumber |
|
293 |
"convert aNumber into an instance of the receivers class and return it." |
|
294 |
||
295 |
^ self subclassResponsibility |
|
296 |
! |
|
297 |
||
298 |
generality |
|
299 |
"return a number giving the receivers generality, that number is |
|
300 |
used to convert one of the arguments in a mixed expression. |
|
301 |
The generality has to be defined in subclasses, |
|
302 |
such that gen(a) > gen(b) iff, conversion of b into a's class |
|
303 |
does not cut precision. For example, Integer has 40, Float has 80, |
|
304 |
meaning that if we convert a Float to an Integer, some precision may |
|
305 |
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
|
306 |
which converts the lower-precision number to the higher precision |
1888 | 307 |
numbers class, when mixed-type arithmetic is performed." |
308 |
||
309 |
^ self subclassResponsibility |
|
310 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
311 |
"Modified: / 5.11.1996 / 15:05:30 / cg" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
312 |
"Modified: / 13.2.1998 / 15:36:01 / stefan" |
1888 | 313 |
! |
314 |
||
315 |
retry:aSymbol coercing:aNumber |
|
316 |
"arithmetic represented by the binary operator, aSymbol, |
|
317 |
could not be performed with the receiver and the argument, aNumber, |
|
318 |
because of the differences in representation. |
|
319 |
Coerce either the receiver or the argument, depending on which has higher |
|
320 |
generality, and try again. |
|
321 |
If the operation is compare for same value (=), return false if |
|
322 |
the argument is not a Number. |
|
323 |
If the generalities are the same, create an error message, since this |
|
324 |
means that a subclass has not been fully implemented." |
|
325 |
||
326 |
|hasGenerality myGenerality otherGenerality| |
|
327 |
||
328 |
hasGenerality := aNumber respondsTo:#generality. |
|
329 |
hasGenerality ifFalse:[ |
|
330 |
(aSymbol == #=) ifTrue:[ |
|
331 |
^ false |
|
332 |
]. |
|
333 |
(aSymbol == #~=) ifTrue:[ |
|
334 |
^ true |
|
335 |
]. |
|
336 |
^ self error:'retry:coercing: argument is not a number'. |
|
337 |
]. |
|
338 |
||
339 |
myGenerality := self generality. |
|
340 |
otherGenerality := aNumber generality. |
|
341 |
(myGenerality > otherGenerality) ifTrue:[ |
|
342 |
^ self perform:aSymbol with:(self coerce:aNumber) |
|
343 |
]. |
|
344 |
(myGenerality < otherGenerality) ifTrue:[ |
|
345 |
^ (aNumber coerce:self) perform:aSymbol with:aNumber |
|
346 |
]. |
|
347 |
self error:'retry:coercing: oops - same generality' |
|
348 |
||
349 |
"Modified: 5.11.1996 / 15:03:38 / cg" |
|
350 |
! ! |
|
351 |
||
701 | 352 |
!ArithmeticValue methodsFor:'comparing'! |
353 |
||
354 |
< something |
|
355 |
"return true, if the argument is greater than the receiver" |
|
356 |
||
357 |
^ self subclassResponsibility |
|
1 | 358 |
! |
359 |
||
701 | 360 |
<= something |
361 |
"return true, if the argument is greater or equal than the receiver" |
|
362 |
||
363 |
^ (something < self) not |
|
364 |
! |
|
365 |
||
366 |
> something |
|
367 |
"return true, if the argument is less than the receiver" |
|
368 |
||
369 |
^ something < self |
|
370 |
! |
|
371 |
||
372 |
>= something |
|
373 |
"return true, if the argument is less or equal than the receiver" |
|
1 | 374 |
|
701 | 375 |
^ (self < something) not |
1 | 376 |
! |
377 |
||
701 | 378 |
compare:arg ifLess:lessBlock ifEqual:equalBlock ifGreater:greaterBlock |
379 |
"three-way compare - thanks to Self for this idea. |
|
380 |
Can be redefined in subclasses to do it with a single comparison if |
|
381 |
comparison is expensive." |
|
382 |
||
383 |
self < arg ifTrue:[ |
|
384 |
^ lessBlock value |
|
385 |
]. |
|
386 |
self = arg ifTrue:[ |
|
387 |
^ equalBlock value |
|
388 |
]. |
|
389 |
^ greaterBlock value |
|
390 |
! ! |
|
391 |
||
392 |
!ArithmeticValue methodsFor:'converting'! |
|
393 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
394 |
asDouble |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
395 |
"ST80 compatibility: return our floats are the identical to ST80 doubles" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
396 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
397 |
^ self asFloat |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
398 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
399 |
"Created: / 13.2.1998 / 15:40:14 / stefan" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
400 |
! |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
401 |
|
1 | 402 |
asFloat |
403 |
"return a float with same value" |
|
404 |
||
405 |
^ self subclassResponsibility |
|
406 |
! |
|
407 |
||
5986 | 408 |
asFloatD |
409 |
"return a double precision float with same value. |
|
410 |
Added for ANSI compatibility" |
|
411 |
||
412 |
^ self asFloat |
|
413 |
||
414 |
"Created: / 7.9.2001 / 13:36:48 / cg" |
|
415 |
"Modified: / 7.9.2001 / 13:38:10 / cg" |
|
416 |
! |
|
417 |
||
418 |
asFloatE |
|
419 |
"return a single precision float with same value. |
|
420 |
Added for ANSI compatibility" |
|
421 |
||
422 |
^ self asShortFloat |
|
423 |
||
424 |
"Created: / 7.9.2001 / 13:37:06 / cg" |
|
425 |
"Modified: / 7.9.2001 / 13:38:15 / cg" |
|
426 |
! |
|
427 |
||
428 |
asFloatQ |
|
429 |
"return a quad precision float with same value. |
|
430 |
Added for ANSI compatibility" |
|
431 |
||
432 |
^ self asLongFloat |
|
433 |
||
434 |
"Created: / 7.9.2001 / 13:38:44 / cg" |
|
435 |
! |
|
436 |
||
1 | 437 |
asFraction |
438 |
"return a fraction with same value" |
|
439 |
||
440 |
^ self subclassResponsibility |
|
441 |
! |
|
442 |
||
701 | 443 |
asInteger |
444 |
"return an integer with same value - might truncate" |
|
445 |
||
446 |
^ self truncated |
|
447 |
! |
|
448 |
||
5986 | 449 |
asLongFloat |
450 |
"return a longFloat with same value" |
|
451 |
||
452 |
^ self subclassResponsibility |
|
453 |
||
454 |
"Modified: / 17.4.1996 / 12:21:35 / cg" |
|
455 |
"Created: / 7.9.2001 / 13:39:31 / cg" |
|
456 |
! |
|
457 |
||
5987 | 458 |
asScaledDecimal:scale |
459 |
"return a fixedPoint approximating the receivers value" |
|
460 |
||
461 |
^ self asFixedPoint:scale |
|
462 |
||
463 |
" |
|
464 |
1.234 asScaledDecimal:2 |
|
465 |
" |
|
466 |
||
467 |
"Created: / 7.9.2001 / 13:46:26 / cg" |
|
468 |
"Modified: / 7.9.2001 / 13:46:44 / cg" |
|
469 |
! |
|
470 |
||
1200 | 471 |
asShortFloat |
472 |
"return a shortFloat with same value" |
|
473 |
||
474 |
^ self asFloat asShortFloat |
|
475 |
||
476 |
"Modified: 17.4.1996 / 12:21:35 / cg" |
|
477 |
! |
|
478 |
||
701 | 479 |
degreesToRadians |
480 |
"interpreting the receiver as radians, return the degrees" |
|
481 |
||
482 |
^ self asFloat degreesToRadians |
|
483 |
! |
|
484 |
||
485 |
radiansToDegrees |
|
486 |
"interpreting the receiver as degrees, return the radians" |
|
487 |
||
488 |
^ self asFloat radiansToDegrees |
|
1 | 489 |
! ! |
490 |
||
4138
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
491 |
!ArithmeticValue methodsFor:'destructive arithmethic'! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
492 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
493 |
*= aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
494 |
"Return the product of self multiplied by aNumber. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
495 |
Self MAY, but NEED NOT be changed to contain the product. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
496 |
So this method must be used: 'a := a *= 5'. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
497 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
498 |
to do optimisations" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
499 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
500 |
^ self * aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
501 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
502 |
"Created: / 28.4.1999 / 11:46:11 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
503 |
"Modified: / 28.4.1999 / 11:53:28 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
504 |
! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
505 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
506 |
+= aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
507 |
"Return the sum of self and aNumber. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
508 |
Self MAY, but NEED NOT be changed to contain the product. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
509 |
So this method must be used: 'a := a += 5'. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
510 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
511 |
to do optimisations" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
512 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
513 |
^ self + aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
514 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
515 |
"Created: / 28.4.1999 / 10:13:41 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
516 |
"Modified: / 28.4.1999 / 11:54:11 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
517 |
! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
518 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
519 |
-= aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
520 |
"Return the difference of self and aNumber. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
521 |
Self MAY, but NEED NOT be changed to contain the product. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
522 |
So this method must be used: 'a := a -= 5'. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
523 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
524 |
to do optimisations" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
525 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
526 |
^ self - aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
527 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
528 |
"Created: / 28.4.1999 / 10:13:58 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
529 |
"Modified: / 28.4.1999 / 11:54:37 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
530 |
! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
531 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
532 |
/= aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
533 |
"Return the quotient of self and aNumber. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
534 |
Self MAY, but NEED NOT be changed to contain the product. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
535 |
So this method must be used: 'a := a /= 5'. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
536 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
537 |
to do optimisations" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
538 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
539 |
^ self / aNumber |
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 |
"Created: / 28.4.1999 / 11:46:22 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
542 |
"Modified: / 28.4.1999 / 11:55:06 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
543 |
! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
544 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
545 |
div2 |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
546 |
"Return the quotient of self divided by 2. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
547 |
Self MAY, but NEED NOT be changed to contain the product. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
548 |
So this method must be used: 'a := a div2. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
549 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
550 |
to do optimisations" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
551 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
552 |
^ self // 2 |
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 |
"Created: / 28.4.1999 / 10:12:44 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
555 |
"Modified: / 28.4.1999 / 11:56:09 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
556 |
! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
557 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
558 |
mul2 |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
559 |
"Return the product of self multiplied by 2. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
560 |
Self MAY, but NEED NOT be changed to contain the product. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
561 |
So this method must be used: 'a := a mul2. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
562 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
563 |
to do optimisations" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
564 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
565 |
^ self * 2 |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
566 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
567 |
"Created: / 28.4.1999 / 10:12:55 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
568 |
"Modified: / 28.4.1999 / 11:56:38 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
569 |
! ! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
570 |
|
701 | 571 |
!ArithmeticValue methodsFor:'double dispatching'! |
1 | 572 |
|
1888 | 573 |
differenceFromFixedPoint:aFixedPoint |
574 |
"the receiver does not know how to subtract from a fixedPoint number - |
|
575 |
retry the operation by coercing to higher generality" |
|
576 |
||
577 |
^ aFixedPoint retry:#- coercing:self |
|
578 |
||
579 |
"Created: 5.11.1996 / 14:55:51 / cg" |
|
580 |
! |
|
581 |
||
701 | 582 |
differenceFromFloat:aFloat |
583 |
"the receiver does not know how to subtract from a float - |
|
584 |
retry the operation by coercing to higher generality" |
|
1 | 585 |
|
701 | 586 |
^ aFloat retry:#- coercing:self |
1 | 587 |
! |
588 |
||
701 | 589 |
differenceFromFraction:aFraction |
590 |
"the receiver does not know how to subtract from a fraction - |
|
591 |
retry the operation by coercing to higher generality" |
|
1 | 592 |
|
701 | 593 |
^ aFraction retry:#- coercing:self |
1 | 594 |
! |
595 |
||
701 | 596 |
differenceFromInteger:anInteger |
597 |
"the receiver does not know how to subtract from an integer - |
|
598 |
retry the operation by coercing to higher generality" |
|
1 | 599 |
|
701 | 600 |
^ anInteger retry:#- coercing:self |
1 | 601 |
! |
602 |
||
1200 | 603 |
differenceFromShortFloat:aShortFloat |
604 |
"the receiver does not know how to subtract from a shortFloat - |
|
605 |
retry the operation by coercing to higher generality" |
|
606 |
||
607 |
^ aShortFloat retry:#- coercing:self |
|
608 |
||
609 |
"Created: 17.4.1996 / 12:33:16 / cg" |
|
610 |
! |
|
611 |
||
1888 | 612 |
lessFromFixedPoint:aFixedPoint |
613 |
"the receiver does not know how to compare to a fixedPoint number - |
|
614 |
retry the operation by coercing to higher generality" |
|
615 |
||
616 |
^ aFixedPoint retry:#< coercing:self |
|
617 |
||
618 |
"Created: 5.11.1996 / 14:56:12 / cg" |
|
619 |
! |
|
620 |
||
701 | 621 |
lessFromFloat:aFloat |
622 |
"the receiver does not know how to compare to a float - |
|
623 |
retry the operation by coercing to higher generality" |
|
1 | 624 |
|
701 | 625 |
^ aFloat retry:#< coercing:self |
1 | 626 |
! |
627 |
||
701 | 628 |
lessFromFraction:aFraction |
629 |
"the receiver does not know how to compare to a fraction - |
|
630 |
retry the operation by coercing to higher generality" |
|
1 | 631 |
|
701 | 632 |
^ aFraction retry:#< coercing:self |
1 | 633 |
! |
634 |
||
701 | 635 |
lessFromInteger:anInteger |
636 |
"the receiver does not know how to compare to an integer - |
|
637 |
retry the operation by coercing to higher generality" |
|
1 | 638 |
|
701 | 639 |
^ anInteger retry:#< coercing:self |
1 | 640 |
! |
641 |
||
1200 | 642 |
lessFromShortFloat:aShortFloat |
643 |
"the receiver does not know how to compare to a shortFloat - |
|
644 |
retry the operation by coercing to higher generality" |
|
645 |
||
646 |
^ aShortFloat retry:#< coercing:self |
|
647 |
||
648 |
"Modified: 17.4.1996 / 12:33:33 / cg" |
|
649 |
! |
|
650 |
||
1888 | 651 |
productFromFixedPoint:aFixedPoint |
652 |
"the receiver does not know how to multiply a fixed point number - |
|
653 |
retry the operation by coercing to higher generality" |
|
654 |
||
655 |
^ aFixedPoint retry:#* coercing:self |
|
656 |
||
657 |
"Created: 5.11.1996 / 14:56:28 / cg" |
|
658 |
! |
|
659 |
||
701 | 660 |
productFromFloat:aFloat |
661 |
"the receiver does not know how to multiply a float - |
|
662 |
retry the operation by coercing to higher generality" |
|
1 | 663 |
|
701 | 664 |
^ aFloat retry:#* coercing:self |
1 | 665 |
! |
666 |
||
701 | 667 |
productFromFraction:aFraction |
668 |
"the receiver does not know how to multiply a fraction - |
|
669 |
retry the operation by coercing to higher generality" |
|
1 | 670 |
|
701 | 671 |
^ aFraction retry:#* coercing:self |
1 | 672 |
! |
673 |
||
701 | 674 |
productFromInteger:anInteger |
675 |
"the receiver does not know how to multiply an integer - |
|
676 |
retry the operation by coercing to higher generality" |
|
1 | 677 |
|
701 | 678 |
^ anInteger retry:#* coercing:self |
1 | 679 |
! |
680 |
||
1200 | 681 |
productFromShortFloat:aShortFloat |
682 |
"the receiver does not know how to multiply a shortFloat - |
|
683 |
retry the operation by coercing to higher generality" |
|
684 |
||
685 |
^ aShortFloat retry:#* coercing:self |
|
686 |
||
687 |
"Created: 17.4.1996 / 12:33:48 / cg" |
|
688 |
! |
|
689 |
||
1888 | 690 |
quotientFromFixedPoint:aFixedPoint |
691 |
"the receiver does not know how to divide a fixed point number - |
|
692 |
retry the operation by coercing to higher generality" |
|
693 |
||
694 |
^ aFixedPoint retry:#/ coercing:self |
|
695 |
||
696 |
"Created: 5.11.1996 / 14:56:41 / cg" |
|
697 |
! |
|
698 |
||
701 | 699 |
quotientFromFloat:aFloat |
700 |
"the receiver does not know how to divide a float - |
|
701 |
retry the operation by coercing to higher generality" |
|
1 | 702 |
|
701 | 703 |
^ aFloat retry:#/ coercing:self |
1 | 704 |
! |
705 |
||
701 | 706 |
quotientFromFraction:aFraction |
707 |
"the receiver does not know how to divide a fraction - |
|
708 |
retry the operation by coercing to higher generality" |
|
1 | 709 |
|
701 | 710 |
^ aFraction retry:#/ coercing:self |
1 | 711 |
! |
712 |
||
701 | 713 |
quotientFromInteger:anInteger |
714 |
"the receiver does not know how to divide an integer - |
|
1 | 715 |
retry the operation by coercing to higher generality" |
716 |
||
701 | 717 |
^ anInteger retry:#/ coercing:self |
1 | 718 |
! |
719 |
||
1200 | 720 |
quotientFromShortFloat:aShortFloat |
721 |
"the receiver does not know how to divide a shortFloat - |
|
722 |
retry the operation by coercing to higher generality" |
|
723 |
||
724 |
^ aShortFloat retry:#/ coercing:self |
|
725 |
||
726 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
727 |
! |
|
728 |
||
1888 | 729 |
sumFromFixedPoint:aFixedPoint |
730 |
"the receiver does not know how to add a fixed point number - |
|
731 |
retry the operation by coercing to higher generality" |
|
732 |
||
733 |
^ aFixedPoint retry:#+ coercing:self |
|
734 |
||
735 |
"Created: 5.11.1996 / 14:56:56 / cg" |
|
736 |
! |
|
737 |
||
1 | 738 |
sumFromFloat:aFloat |
739 |
"the receiver does not know how to add a float - |
|
740 |
retry the operation by coercing to higher generality" |
|
741 |
||
742 |
^ aFloat retry:#+ coercing:self |
|
743 |
! |
|
744 |
||
745 |
sumFromFraction:aFraction |
|
746 |
"the receiver does not know how to add a fraction - |
|
747 |
retry the operation by coercing to higher generality" |
|
748 |
||
749 |
^ aFraction retry:#+ coercing:self |
|
750 |
! |
|
751 |
||
701 | 752 |
sumFromInteger:anInteger |
753 |
"the receiver does not know how to add an integer - |
|
1 | 754 |
retry the operation by coercing to higher generality" |
755 |
||
701 | 756 |
^ anInteger retry:#+ coercing:self |
1200 | 757 |
! |
758 |
||
759 |
sumFromShortFloat:aShortFloat |
|
760 |
"the receiver does not know how to add a shortFloat - |
|
761 |
retry the operation by coercing to higher generality" |
|
762 |
||
763 |
^ aShortFloat retry:#+ coercing:self |
|
764 |
||
765 |
"Created: 17.4.1996 / 12:34:10 / cg" |
|
1 | 766 |
! ! |
767 |
||
768 |
!ArithmeticValue methodsFor:'misc math'! |
|
769 |
||
770 |
exp |
|
771 |
"return e ^ receiver" |
|
772 |
||
773 |
^ self asFloat exp |
|
774 |
! |
|
775 |
||
701 | 776 |
floorLog:radix |
777 |
"return the logarithm truncated as an integer" |
|
778 |
||
779 |
^ (self log:radix) floor |
|
780 |
! |
|
781 |
||
1 | 782 |
ln |
783 |
"return the natural logarithm of the receiver" |
|
784 |
||
785 |
^ self asFloat ln |
|
786 |
! |
|
787 |
||
788 |
log |
|
789 |
"return log base 10 of the receiver" |
|
790 |
||
791 |
^ self log:10 |
|
792 |
! |
|
793 |
||
794 |
log:aNumber |
|
795 |
"return log base aNumber of the receiver" |
|
796 |
||
797 |
^ self ln / aNumber ln |
|
798 |
! |
|
799 |
||
800 |
raisedTo:aNumber |
|
801 |
"return the receiver raised to aNumber" |
|
802 |
||
803 |
aNumber = 0 ifTrue:[^ 1]. |
|
804 |
aNumber = 1 ifTrue:[^ self]. |
|
805 |
aNumber isInteger ifTrue:[ |
|
213 | 806 |
^ self raisedToInteger:aNumber |
1 | 807 |
]. |
808 |
^ self asFloat raisedTo:aNumber |
|
809 |
! |
|
810 |
||
811 |
raisedToInteger:anInteger |
|
812 |
"return the receiver raised to anInteger" |
|
813 |
||
814 |
|count result| |
|
815 |
||
816 |
result := self coerce:1. |
|
817 |
count := anInteger abs. |
|
818 |
count timesRepeat:[result := result * self]. |
|
819 |
(anInteger < 0) ifTrue:[ |
|
213 | 820 |
^ 1 / result |
1 | 821 |
]. |
822 |
^ result |
|
701 | 823 |
! |
824 |
||
825 |
sqrt |
|
826 |
"return the square root of the receiver" |
|
827 |
||
828 |
^ self asFloat sqrt |
|
829 |
! |
|
830 |
||
831 |
squared |
|
832 |
"return receiver * receiver" |
|
833 |
||
834 |
^ self * self |
|
835 |
! ! |
|
836 |
||
837 |
!ArithmeticValue methodsFor:'queries'! |
|
838 |
||
839 |
respondsToArithmetic |
|
840 |
"return true, if the receiver responds to arithmetic messages" |
|
841 |
||
842 |
^ true |
|
843 |
! ! |
|
844 |
||
845 |
!ArithmeticValue methodsFor:'testing'! |
|
846 |
||
847 |
denominator |
|
848 |
"return the denominator of the receiver" |
|
849 |
||
850 |
^ 1 |
|
851 |
! |
|
852 |
||
853 |
even |
|
854 |
"return true if the receiver is divisible by 2" |
|
855 |
||
856 |
^ self truncated asInteger even |
|
857 |
! |
|
858 |
||
3635 | 859 |
isComplex |
860 |
"Answer whether the receiver has an imaginary part" |
|
861 |
||
862 |
^ false |
|
863 |
||
864 |
"Modified: / 9.7.1998 / 10:19:27 / cg" |
|
865 |
! |
|
866 |
||
6156 | 867 |
isReal |
868 |
"return true, if the receiver is some kind of real number (as opposed to a complex); |
|
869 |
false is returned here - the method is only redefined in Number (and Complex)." |
|
870 |
||
871 |
^ false |
|
872 |
||
873 |
"Modified: / 6.11.2001 / 13:20:20 / cg" |
|
874 |
"Created: / 6.11.2001 / 13:26:29 / cg" |
|
875 |
! |
|
876 |
||
701 | 877 |
negative |
878 |
"return true, if the receiver is < 0" |
|
879 |
||
880 |
" this would lead to infinite recursion ... |
|
881 |
^ (self < 0) |
|
882 |
" |
|
883 |
^ self subclassResponsibility |
|
884 |
! |
|
885 |
||
886 |
numerator |
|
887 |
"return the numerator of the receiver." |
|
888 |
||
889 |
^ self |
|
890 |
! |
|
891 |
||
892 |
odd |
|
893 |
"return true if the receiver is not divisible by 2" |
|
894 |
||
895 |
^ self even not |
|
896 |
! |
|
897 |
||
898 |
positive |
|
899 |
"return true, if the receiver is >= 0" |
|
900 |
||
901 |
^ self negative not |
|
902 |
! |
|
903 |
||
904 |
sign |
|
905 |
"return the sign of the receiver" |
|
906 |
||
907 |
(self < 0) ifTrue:[^ -1]. |
|
908 |
(self > 0) ifTrue:[^ 1]. |
|
909 |
^ 0 |
|
910 |
! |
|
911 |
||
912 |
strictlyPositive |
|
913 |
"return true, if the receiver is > 0" |
|
914 |
||
915 |
^ (self > 0) |
|
1 | 916 |
! ! |
917 |
||
918 |
!ArithmeticValue methodsFor:'trigonometric'! |
|
919 |
||
920 |
arcCos |
|
921 |
"return the arccosine of the receiver (in radians)" |
|
922 |
||
923 |
^ self asFloat arcCos |
|
924 |
! |
|
925 |
||
926 |
arcSin |
|
927 |
"return the arcsine of the receiver (in radians)" |
|
928 |
||
929 |
^ self asFloat arcSin |
|
930 |
! |
|
931 |
||
932 |
arcTan |
|
933 |
"return the arctangens of the receiver (in radians)" |
|
934 |
||
935 |
^ self asFloat arcTan |
|
701 | 936 |
! |
937 |
||
938 |
cos |
|
939 |
"return the cosine of the receiver (interpreted as radians)" |
|
940 |
||
941 |
^ self asFloat cos |
|
942 |
! |
|
943 |
||
944 |
sin |
|
945 |
"return the sine of the receiver (interpreted as radians)" |
|
946 |
||
947 |
^ self asFloat sin |
|
948 |
! |
|
949 |
||
950 |
tan |
|
951 |
"return the tangens of the receiver (interpreted as radians)" |
|
952 |
||
953 |
^ self asFloat tan |
|
1 | 954 |
! ! |
955 |
||
701 | 956 |
!ArithmeticValue methodsFor:'truncation & rounding'! |
1 | 957 |
|
701 | 958 |
ceiling |
959 |
"return the integer nearest the receiver towards positive infinity." |
|
1 | 960 |
|
701 | 961 |
|anInteger| |
1 | 962 |
|
701 | 963 |
anInteger := self // 1. "truncates towards negative infinity" |
964 |
anInteger = self ifTrue:[^ anInteger]. |
|
965 |
^ anInteger + 1 |
|
1 | 966 |
! |
967 |
||
701 | 968 |
floor |
969 |
"return the receiver truncated towards negative infinity" |
|
1 | 970 |
|
701 | 971 |
^ self // 1 |
1 | 972 |
! |
973 |
||
701 | 974 |
roundTo:aNumber |
975 |
"return the receiver rounded to multiples of aNumber" |
|
1 | 976 |
|
701 | 977 |
^ (self / aNumber) rounded * aNumber |
978 |
! |
|
979 |
||
980 |
rounded |
|
981 |
"return the integer nearest the receiver" |
|
982 |
||
1881 | 983 |
self negative ifTrue:[ |
984 |
^ (self - 0.5) ceiling |
|
985 |
]. |
|
701 | 986 |
^ (self + 0.5) floor |
1881 | 987 |
|
988 |
"Modified: 5.11.1996 / 11:31:59 / cg" |
|
1 | 989 |
! |
990 |
||
701 | 991 |
truncateTo:aNumber |
992 |
"return the receiver truncated to multiples of aNumber" |
|
1 | 993 |
|
701 | 994 |
^ ((self / aNumber) floor * aNumber) asInteger |
1 | 995 |
! |
996 |
||
701 | 997 |
truncated |
998 |
"return the receiver truncated towards zero" |
|
1 | 999 |
|
701 | 1000 |
self negative ifTrue:[ |
1001 |
^ self ceiling |
|
1002 |
]. |
|
1003 |
^ self floor |
|
1004 |
! ! |
|
1 | 1005 |
|
701 | 1006 |
!ArithmeticValue class methodsFor:'documentation'! |
1007 |
||
1008 |
version |
|
6342
fbbe62d5b17c
Remove obsolete (since years) AnyArithmeticSignal definitions.
Stefan Vogel <sv@exept.de>
parents:
6156
diff
changeset
|
1009 |
^ '$Header: /cvs/stx/stx/libbasic/ArithmeticValue.st,v 1.42 2001-12-19 13:56:24 stefan Exp $' |
1 | 1010 |
! ! |
701 | 1011 |
ArithmeticValue initialize! |