author | Claus Gittinger <cg@exept.de> |
Fri, 02 Aug 2002 19:27:04 +0200 | |
changeset 6707 | 790fc577c403 |
parent 6691 | 48ba39288455 |
child 6858 | 13919a7544b6 |
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 |
||
5986 | 407 |
asLongFloat |
408 |
"return a longFloat with same value" |
|
409 |
||
410 |
^ self subclassResponsibility |
|
411 |
||
412 |
"Modified: / 17.4.1996 / 12:21:35 / cg" |
|
413 |
"Created: / 7.9.2001 / 13:39:31 / cg" |
|
414 |
! |
|
415 |
||
5987 | 416 |
asScaledDecimal:scale |
417 |
"return a fixedPoint approximating the receivers value" |
|
418 |
||
419 |
^ self asFixedPoint:scale |
|
420 |
||
421 |
" |
|
422 |
1.234 asScaledDecimal:2 |
|
423 |
" |
|
424 |
||
425 |
"Created: / 7.9.2001 / 13:46:26 / cg" |
|
426 |
"Modified: / 7.9.2001 / 13:46:44 / cg" |
|
427 |
! |
|
428 |
||
1200 | 429 |
asShortFloat |
430 |
"return a shortFloat with same value" |
|
431 |
||
432 |
^ self asFloat asShortFloat |
|
433 |
||
434 |
"Modified: 17.4.1996 / 12:21:35 / cg" |
|
435 |
! |
|
436 |
||
701 | 437 |
degreesToRadians |
438 |
"interpreting the receiver as radians, return the degrees" |
|
439 |
||
440 |
^ self asFloat degreesToRadians |
|
441 |
! |
|
442 |
||
443 |
radiansToDegrees |
|
444 |
"interpreting the receiver as degrees, return the radians" |
|
445 |
||
446 |
^ self asFloat radiansToDegrees |
|
1 | 447 |
! ! |
448 |
||
4138
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
449 |
!ArithmeticValue methodsFor:'destructive arithmethic'! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
450 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
451 |
*= aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
452 |
"Return the product of self multiplied by aNumber. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
453 |
Self MAY, but NEED NOT be changed to contain the product. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
454 |
So this method must be used: 'a := a *= 5'. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
455 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
456 |
to do optimisations" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
457 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
458 |
^ self * aNumber |
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 |
"Created: / 28.4.1999 / 11:46:11 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
461 |
"Modified: / 28.4.1999 / 11:53:28 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
462 |
! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
463 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
464 |
+= aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
465 |
"Return the sum of self and aNumber. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
466 |
Self MAY, but NEED NOT be changed to contain the product. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
467 |
So this method must be used: 'a := a += 5'. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
468 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
469 |
to do optimisations" |
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 |
^ self + aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
472 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
473 |
"Created: / 28.4.1999 / 10:13:41 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
474 |
"Modified: / 28.4.1999 / 11:54:11 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
475 |
! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
476 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
477 |
-= aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
478 |
"Return the difference of self and aNumber. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
479 |
Self MAY, but NEED NOT be changed to contain the product. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
480 |
So this method must be used: 'a := a -= 5'. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
481 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
482 |
to do optimisations" |
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 |
^ self - aNumber |
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 |
"Created: / 28.4.1999 / 10:13:58 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
487 |
"Modified: / 28.4.1999 / 11:54:37 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
488 |
! |
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 |
/= aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
491 |
"Return the quotient of self and aNumber. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
492 |
Self MAY, but NEED NOT be changed to contain the product. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
493 |
So this method must be used: 'a := a /= 5'. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
494 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
495 |
to do optimisations" |
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 |
^ self / aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
498 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
499 |
"Created: / 28.4.1999 / 11:46:22 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
500 |
"Modified: / 28.4.1999 / 11:55:06 / stefan" |
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 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
503 |
div2 |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
504 |
"Return the quotient of self divided by 2. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
505 |
Self MAY, but NEED NOT be changed to contain the product. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
506 |
So this method must be used: 'a := a div2. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
507 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
508 |
to do optimisations" |
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 |
^ self // 2 |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
511 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
512 |
"Created: / 28.4.1999 / 10:12:44 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
513 |
"Modified: / 28.4.1999 / 11:56:09 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
514 |
! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
515 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
516 |
mul2 |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
517 |
"Return the product of self multiplied by 2. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
518 |
Self MAY, but NEED NOT be changed to contain the product. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
519 |
So this method must be used: 'a := a mul2. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
520 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
521 |
to do optimisations" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
522 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
523 |
^ self * 2 |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
524 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
525 |
"Created: / 28.4.1999 / 10:12:55 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
526 |
"Modified: / 28.4.1999 / 11:56:38 / stefan" |
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 |
|
701 | 529 |
!ArithmeticValue methodsFor:'double dispatching'! |
1 | 530 |
|
1888 | 531 |
differenceFromFixedPoint:aFixedPoint |
532 |
"the receiver does not know how to subtract from a fixedPoint number - |
|
533 |
retry the operation by coercing to higher generality" |
|
534 |
||
535 |
^ aFixedPoint retry:#- coercing:self |
|
536 |
||
537 |
"Created: 5.11.1996 / 14:55:51 / cg" |
|
538 |
! |
|
539 |
||
701 | 540 |
differenceFromFloat:aFloat |
541 |
"the receiver does not know how to subtract from a float - |
|
542 |
retry the operation by coercing to higher generality" |
|
1 | 543 |
|
701 | 544 |
^ aFloat retry:#- coercing:self |
1 | 545 |
! |
546 |
||
701 | 547 |
differenceFromFraction:aFraction |
548 |
"the receiver does not know how to subtract from a fraction - |
|
549 |
retry the operation by coercing to higher generality" |
|
1 | 550 |
|
701 | 551 |
^ aFraction retry:#- coercing:self |
1 | 552 |
! |
553 |
||
701 | 554 |
differenceFromInteger:anInteger |
555 |
"the receiver does not know how to subtract from an integer - |
|
556 |
retry the operation by coercing to higher generality" |
|
1 | 557 |
|
701 | 558 |
^ anInteger retry:#- coercing:self |
1 | 559 |
! |
560 |
||
1200 | 561 |
differenceFromShortFloat:aShortFloat |
562 |
"the receiver does not know how to subtract from a shortFloat - |
|
563 |
retry the operation by coercing to higher generality" |
|
564 |
||
565 |
^ aShortFloat retry:#- coercing:self |
|
566 |
||
567 |
"Created: 17.4.1996 / 12:33:16 / cg" |
|
568 |
! |
|
569 |
||
1888 | 570 |
lessFromFixedPoint:aFixedPoint |
571 |
"the receiver does not know how to compare to a fixedPoint number - |
|
572 |
retry the operation by coercing to higher generality" |
|
573 |
||
574 |
^ aFixedPoint retry:#< coercing:self |
|
575 |
||
576 |
"Created: 5.11.1996 / 14:56:12 / cg" |
|
577 |
! |
|
578 |
||
701 | 579 |
lessFromFloat:aFloat |
580 |
"the receiver does not know how to compare to a float - |
|
581 |
retry the operation by coercing to higher generality" |
|
1 | 582 |
|
701 | 583 |
^ aFloat retry:#< coercing:self |
1 | 584 |
! |
585 |
||
701 | 586 |
lessFromFraction:aFraction |
587 |
"the receiver does not know how to compare to a fraction - |
|
588 |
retry the operation by coercing to higher generality" |
|
1 | 589 |
|
701 | 590 |
^ aFraction retry:#< coercing:self |
1 | 591 |
! |
592 |
||
701 | 593 |
lessFromInteger:anInteger |
594 |
"the receiver does not know how to compare to an integer - |
|
595 |
retry the operation by coercing to higher generality" |
|
1 | 596 |
|
701 | 597 |
^ anInteger retry:#< coercing:self |
1 | 598 |
! |
599 |
||
1200 | 600 |
lessFromShortFloat:aShortFloat |
601 |
"the receiver does not know how to compare to a shortFloat - |
|
602 |
retry the operation by coercing to higher generality" |
|
603 |
||
604 |
^ aShortFloat retry:#< coercing:self |
|
605 |
||
606 |
"Modified: 17.4.1996 / 12:33:33 / cg" |
|
607 |
! |
|
608 |
||
1888 | 609 |
productFromFixedPoint:aFixedPoint |
610 |
"the receiver does not know how to multiply a fixed point number - |
|
611 |
retry the operation by coercing to higher generality" |
|
612 |
||
613 |
^ aFixedPoint retry:#* coercing:self |
|
614 |
||
615 |
"Created: 5.11.1996 / 14:56:28 / cg" |
|
616 |
! |
|
617 |
||
701 | 618 |
productFromFloat:aFloat |
619 |
"the receiver does not know how to multiply a float - |
|
620 |
retry the operation by coercing to higher generality" |
|
1 | 621 |
|
701 | 622 |
^ aFloat retry:#* coercing:self |
1 | 623 |
! |
624 |
||
701 | 625 |
productFromFraction:aFraction |
626 |
"the receiver does not know how to multiply a fraction - |
|
627 |
retry the operation by coercing to higher generality" |
|
1 | 628 |
|
701 | 629 |
^ aFraction retry:#* coercing:self |
1 | 630 |
! |
631 |
||
701 | 632 |
productFromInteger:anInteger |
633 |
"the receiver does not know how to multiply an integer - |
|
634 |
retry the operation by coercing to higher generality" |
|
1 | 635 |
|
701 | 636 |
^ anInteger retry:#* coercing:self |
1 | 637 |
! |
638 |
||
1200 | 639 |
productFromShortFloat:aShortFloat |
640 |
"the receiver does not know how to multiply a shortFloat - |
|
641 |
retry the operation by coercing to higher generality" |
|
642 |
||
643 |
^ aShortFloat retry:#* coercing:self |
|
644 |
||
645 |
"Created: 17.4.1996 / 12:33:48 / cg" |
|
646 |
! |
|
647 |
||
1888 | 648 |
quotientFromFixedPoint:aFixedPoint |
649 |
"the receiver does not know how to divide a fixed point number - |
|
650 |
retry the operation by coercing to higher generality" |
|
651 |
||
652 |
^ aFixedPoint retry:#/ coercing:self |
|
653 |
||
654 |
"Created: 5.11.1996 / 14:56:41 / cg" |
|
655 |
! |
|
656 |
||
701 | 657 |
quotientFromFloat:aFloat |
658 |
"the receiver does not know how to divide a float - |
|
659 |
retry the operation by coercing to higher generality" |
|
1 | 660 |
|
701 | 661 |
^ aFloat retry:#/ coercing:self |
1 | 662 |
! |
663 |
||
701 | 664 |
quotientFromFraction:aFraction |
665 |
"the receiver does not know how to divide a fraction - |
|
666 |
retry the operation by coercing to higher generality" |
|
1 | 667 |
|
701 | 668 |
^ aFraction retry:#/ coercing:self |
1 | 669 |
! |
670 |
||
701 | 671 |
quotientFromInteger:anInteger |
672 |
"the receiver does not know how to divide an integer - |
|
1 | 673 |
retry the operation by coercing to higher generality" |
674 |
||
701 | 675 |
^ anInteger retry:#/ coercing:self |
1 | 676 |
! |
677 |
||
1200 | 678 |
quotientFromShortFloat:aShortFloat |
679 |
"the receiver does not know how to divide a shortFloat - |
|
680 |
retry the operation by coercing to higher generality" |
|
681 |
||
682 |
^ aShortFloat retry:#/ coercing:self |
|
683 |
||
684 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
685 |
! |
|
686 |
||
1888 | 687 |
sumFromFixedPoint:aFixedPoint |
688 |
"the receiver does not know how to add a fixed point number - |
|
689 |
retry the operation by coercing to higher generality" |
|
690 |
||
691 |
^ aFixedPoint retry:#+ coercing:self |
|
692 |
||
693 |
"Created: 5.11.1996 / 14:56:56 / cg" |
|
694 |
! |
|
695 |
||
1 | 696 |
sumFromFloat:aFloat |
697 |
"the receiver does not know how to add a float - |
|
698 |
retry the operation by coercing to higher generality" |
|
699 |
||
700 |
^ aFloat retry:#+ coercing:self |
|
701 |
! |
|
702 |
||
703 |
sumFromFraction:aFraction |
|
704 |
"the receiver does not know how to add a fraction - |
|
705 |
retry the operation by coercing to higher generality" |
|
706 |
||
707 |
^ aFraction retry:#+ coercing:self |
|
708 |
! |
|
709 |
||
701 | 710 |
sumFromInteger:anInteger |
711 |
"the receiver does not know how to add an integer - |
|
1 | 712 |
retry the operation by coercing to higher generality" |
713 |
||
701 | 714 |
^ anInteger retry:#+ coercing:self |
1200 | 715 |
! |
716 |
||
717 |
sumFromShortFloat:aShortFloat |
|
718 |
"the receiver does not know how to add a shortFloat - |
|
719 |
retry the operation by coercing to higher generality" |
|
720 |
||
721 |
^ aShortFloat retry:#+ coercing:self |
|
722 |
||
723 |
"Created: 17.4.1996 / 12:34:10 / cg" |
|
1 | 724 |
! ! |
725 |
||
726 |
!ArithmeticValue methodsFor:'misc math'! |
|
727 |
||
728 |
exp |
|
729 |
"return e ^ receiver" |
|
730 |
||
731 |
^ self asFloat exp |
|
732 |
! |
|
733 |
||
701 | 734 |
floorLog:radix |
735 |
"return the logarithm truncated as an integer" |
|
736 |
||
737 |
^ (self log:radix) floor |
|
738 |
! |
|
739 |
||
1 | 740 |
ln |
741 |
"return the natural logarithm of the receiver" |
|
742 |
||
743 |
^ self asFloat ln |
|
744 |
! |
|
745 |
||
746 |
log |
|
747 |
"return log base 10 of the receiver" |
|
748 |
||
749 |
^ self log:10 |
|
750 |
! |
|
751 |
||
752 |
log:aNumber |
|
753 |
"return log base aNumber of the receiver" |
|
754 |
||
755 |
^ self ln / aNumber ln |
|
756 |
! |
|
757 |
||
758 |
raisedTo:aNumber |
|
759 |
"return the receiver raised to aNumber" |
|
760 |
||
761 |
aNumber = 0 ifTrue:[^ 1]. |
|
762 |
aNumber = 1 ifTrue:[^ self]. |
|
763 |
aNumber isInteger ifTrue:[ |
|
213 | 764 |
^ self raisedToInteger:aNumber |
1 | 765 |
]. |
766 |
^ self asFloat raisedTo:aNumber |
|
767 |
! |
|
768 |
||
769 |
raisedToInteger:anInteger |
|
770 |
"return the receiver raised to anInteger" |
|
771 |
||
772 |
|count result| |
|
773 |
||
774 |
result := self coerce:1. |
|
775 |
count := anInteger abs. |
|
776 |
count timesRepeat:[result := result * self]. |
|
777 |
(anInteger < 0) ifTrue:[ |
|
213 | 778 |
^ 1 / result |
1 | 779 |
]. |
780 |
^ result |
|
701 | 781 |
! |
782 |
||
783 |
sqrt |
|
784 |
"return the square root of the receiver" |
|
785 |
||
786 |
^ self asFloat sqrt |
|
787 |
! |
|
788 |
||
789 |
squared |
|
790 |
"return receiver * receiver" |
|
791 |
||
792 |
^ self * self |
|
793 |
! ! |
|
794 |
||
795 |
!ArithmeticValue methodsFor:'queries'! |
|
796 |
||
797 |
respondsToArithmetic |
|
798 |
"return true, if the receiver responds to arithmetic messages" |
|
799 |
||
800 |
^ true |
|
801 |
! ! |
|
802 |
||
803 |
!ArithmeticValue methodsFor:'testing'! |
|
804 |
||
805 |
denominator |
|
806 |
"return the denominator of the receiver" |
|
807 |
||
808 |
^ 1 |
|
809 |
! |
|
810 |
||
811 |
even |
|
812 |
"return true if the receiver is divisible by 2" |
|
813 |
||
814 |
^ self truncated asInteger even |
|
815 |
! |
|
816 |
||
3635 | 817 |
isComplex |
818 |
"Answer whether the receiver has an imaginary part" |
|
819 |
||
820 |
^ false |
|
821 |
||
822 |
"Modified: / 9.7.1998 / 10:19:27 / cg" |
|
823 |
! |
|
824 |
||
6156 | 825 |
isReal |
826 |
"return true, if the receiver is some kind of real number (as opposed to a complex); |
|
827 |
false is returned here - the method is only redefined in Number (and Complex)." |
|
828 |
||
829 |
^ false |
|
830 |
||
831 |
"Modified: / 6.11.2001 / 13:20:20 / cg" |
|
832 |
"Created: / 6.11.2001 / 13:26:29 / cg" |
|
833 |
! |
|
834 |
||
701 | 835 |
negative |
836 |
"return true, if the receiver is < 0" |
|
837 |
||
838 |
" this would lead to infinite recursion ... |
|
839 |
^ (self < 0) |
|
840 |
" |
|
841 |
^ self subclassResponsibility |
|
842 |
! |
|
843 |
||
844 |
numerator |
|
845 |
"return the numerator of the receiver." |
|
846 |
||
847 |
^ self |
|
848 |
! |
|
849 |
||
850 |
odd |
|
851 |
"return true if the receiver is not divisible by 2" |
|
852 |
||
853 |
^ self even not |
|
854 |
! |
|
855 |
||
856 |
positive |
|
857 |
"return true, if the receiver is >= 0" |
|
858 |
||
859 |
^ self negative not |
|
860 |
! |
|
861 |
||
862 |
sign |
|
863 |
"return the sign of the receiver" |
|
864 |
||
865 |
(self < 0) ifTrue:[^ -1]. |
|
866 |
(self > 0) ifTrue:[^ 1]. |
|
867 |
^ 0 |
|
868 |
! |
|
869 |
||
870 |
strictlyPositive |
|
871 |
"return true, if the receiver is > 0" |
|
872 |
||
873 |
^ (self > 0) |
|
1 | 874 |
! ! |
875 |
||
876 |
!ArithmeticValue methodsFor:'trigonometric'! |
|
877 |
||
878 |
arcCos |
|
879 |
"return the arccosine of the receiver (in radians)" |
|
880 |
||
881 |
^ self asFloat arcCos |
|
882 |
! |
|
883 |
||
884 |
arcSin |
|
885 |
"return the arcsine of the receiver (in radians)" |
|
886 |
||
887 |
^ self asFloat arcSin |
|
888 |
! |
|
889 |
||
890 |
arcTan |
|
891 |
"return the arctangens of the receiver (in radians)" |
|
892 |
||
893 |
^ self asFloat arcTan |
|
701 | 894 |
! |
895 |
||
896 |
cos |
|
897 |
"return the cosine of the receiver (interpreted as radians)" |
|
898 |
||
899 |
^ self asFloat cos |
|
900 |
! |
|
901 |
||
902 |
sin |
|
903 |
"return the sine of the receiver (interpreted as radians)" |
|
904 |
||
905 |
^ self asFloat sin |
|
906 |
! |
|
907 |
||
908 |
tan |
|
909 |
"return the tangens of the receiver (interpreted as radians)" |
|
910 |
||
911 |
^ self asFloat tan |
|
1 | 912 |
! ! |
913 |
||
701 | 914 |
!ArithmeticValue methodsFor:'truncation & rounding'! |
1 | 915 |
|
701 | 916 |
ceiling |
917 |
"return the integer nearest the receiver towards positive infinity." |
|
1 | 918 |
|
701 | 919 |
|anInteger| |
1 | 920 |
|
701 | 921 |
anInteger := self // 1. "truncates towards negative infinity" |
922 |
anInteger = self ifTrue:[^ anInteger]. |
|
923 |
^ anInteger + 1 |
|
1 | 924 |
! |
925 |
||
701 | 926 |
floor |
927 |
"return the receiver truncated towards negative infinity" |
|
1 | 928 |
|
701 | 929 |
^ self // 1 |
1 | 930 |
! |
931 |
||
701 | 932 |
roundTo:aNumber |
933 |
"return the receiver rounded to multiples of aNumber" |
|
1 | 934 |
|
701 | 935 |
^ (self / aNumber) rounded * aNumber |
936 |
! |
|
937 |
||
938 |
rounded |
|
939 |
"return the integer nearest the receiver" |
|
940 |
||
1881 | 941 |
self negative ifTrue:[ |
942 |
^ (self - 0.5) ceiling |
|
943 |
]. |
|
701 | 944 |
^ (self + 0.5) floor |
1881 | 945 |
|
946 |
"Modified: 5.11.1996 / 11:31:59 / cg" |
|
1 | 947 |
! |
948 |
||
701 | 949 |
truncateTo:aNumber |
950 |
"return the receiver truncated to multiples of aNumber" |
|
1 | 951 |
|
701 | 952 |
^ ((self / aNumber) floor * aNumber) asInteger |
1 | 953 |
! |
954 |
||
701 | 955 |
truncated |
956 |
"return the receiver truncated towards zero" |
|
1 | 957 |
|
701 | 958 |
self negative ifTrue:[ |
959 |
^ self ceiling |
|
960 |
]. |
|
961 |
^ self floor |
|
962 |
! ! |
|
1 | 963 |
|
701 | 964 |
!ArithmeticValue class methodsFor:'documentation'! |
965 |
||
966 |
version |
|
6691
48ba39288455
error notifiers are now initialized there
Claus Gittinger <cg@exept.de>
parents:
6342
diff
changeset
|
967 |
^ '$Header: /cvs/stx/stx/libbasic/ArithmeticValue.st,v 1.43 2002-07-31 14:38:58 cg Exp $' |
1 | 968 |
! ! |
701 | 969 |
ArithmeticValue initialize! |