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