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