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