author | Claus Gittinger <cg@exept.de> |
Thu, 15 Jul 1999 21:45:54 +0200 | |
changeset 4392 | 26fb48f04e1b |
parent 4138 | c5030c520941 |
child 4453 | 721c9dd0e0c6 |
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 |
||
13 |
Magnitude subclass:#ArithmeticValue |
|
1172 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'DivisionByZeroSignal DomainErrorSignal OverflowSignal |
|
16 |
UnderflowSignal ArithmeticSignal AnyArithmeticSignal |
|
17 |
UnorderedSignal' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Magnitude-Numbers' |
|
1 | 20 |
! |
21 |
||
701 | 22 |
!ArithmeticValue class methodsFor:'documentation'! |
82 | 23 |
|
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
213 | 27 |
All Rights Reserved |
1 | 28 |
|
82 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
1 | 37 |
|
82 | 38 |
documentation |
39 |
" |
|
40 |
ArithmeticValue is an abstract superclass for all things responding to |
|
155 | 41 |
arithmetic messages. It was inserted into the hierarchy, to allow objects |
42 |
like matrices, functions etc. to share the arithmetic methods defined here. |
|
43 |
||
1283 | 44 |
[class variables:] |
45 |
ArithmeticSignal <Signal> parent of all arithmetic signals |
|
46 |
(never raised itself) |
|
82 | 47 |
|
1283 | 48 |
DomainErrorSignal <Signal> raised upon float errors |
49 |
(for example range in trigonometric) |
|
50 |
||
51 |
DivisionByZeroSignal <Signal> raised when division by 0 is attempted |
|
155 | 52 |
|
1283 | 53 |
OverflowSignal <Signal> raised on overflow/underflow conditions |
54 |
UnderflowSignal in float arithmetic. |
|
55 |
Notice: some OperatingSystems do not |
|
56 |
provide enough information for ST/X to |
|
57 |
extract the real reason for the floatException |
|
58 |
thus raising DomainErrorSignal in these cases. |
|
155 | 59 |
|
1295 | 60 |
[author:] |
61 |
Claus Gittinger |
|
62 |
||
1283 | 63 |
[See also:] |
64 |
Number |
|
82 | 65 |
" |
66 |
! ! |
|
1 | 67 |
|
701 | 68 |
!ArithmeticValue class methodsFor:'initialization'! |
1 | 69 |
|
70 |
initialize |
|
71 |
"setup the signals" |
|
72 |
||
54 | 73 |
DomainErrorSignal isNil ifTrue:[ |
302 | 74 |
ArithmeticSignal := ErrorSignal newSignalMayProceed:false. |
213 | 75 |
ArithmeticSignal nameClass:self message:#arithmeticSignal. |
76 |
ArithmeticSignal notifierString:'arithmetic error'. |
|
155 | 77 |
|
213 | 78 |
DomainErrorSignal := ArithmeticSignal newSignalMayProceed:false. |
79 |
DomainErrorSignal nameClass:self message:#domainErrorSignal. |
|
80 |
DomainErrorSignal notifierString:'domain error'. |
|
1 | 81 |
|
213 | 82 |
DivisionByZeroSignal := ArithmeticSignal newSignalMayProceed:false. |
83 |
DivisionByZeroSignal nameClass:self message:#divisionByZeroSignal. |
|
84 |
DivisionByZeroSignal notifierString:'division by zero'. |
|
1 | 85 |
|
213 | 86 |
OverflowSignal := ArithmeticSignal newSignalMayProceed:false. |
87 |
OverflowSignal nameClass:self message:#overflowSignal. |
|
88 |
OverflowSignal notifierString:'overflow'. |
|
54 | 89 |
|
213 | 90 |
UnderflowSignal := ArithmeticSignal newSignalMayProceed:false. |
91 |
UnderflowSignal nameClass:self message:#underflowSignal. |
|
92 |
UnderflowSignal notifierString:'underflow'. |
|
1 | 93 |
|
345 | 94 |
UnorderedSignal := ArithmeticSignal newSignalMayProceed:false. |
95 |
UnorderedSignal nameClass:self message:#unorderedSignal. |
|
96 |
UnorderedSignal notifierString:'unordered'. |
|
97 |
||
213 | 98 |
AnyArithmeticSignal := SignalSet with:DomainErrorSignal |
99 |
with:DivisionByZeroSignal |
|
100 |
with:OverflowSignal |
|
345 | 101 |
with:UnderflowSignal |
102 |
with:UnorderedSignal. |
|
54 | 103 |
] |
1 | 104 |
! ! |
105 |
||
701 | 106 |
!ArithmeticValue class methodsFor:'Signal constants'! |
107 |
||
108 |
anyArithmeticSignal |
|
109 |
"return a signalSet with all possible arithmetic signals. |
|
110 |
OBSOLETE: this will vanish, since starting with 2.10.3, signal parents |
|
111 |
have been added." |
|
112 |
||
113 |
^ AnyArithmeticSignal |
|
114 |
! |
|
115 |
||
116 |
arithmeticSignal |
|
117 |
"return the parent of all arithmetic signals" |
|
118 |
||
119 |
^ ArithmeticSignal |
|
120 |
! |
|
121 |
||
122 |
divisionByZeroSignal |
|
123 |
"return the signal which is raised on division by zero" |
|
124 |
||
125 |
^ DivisionByZeroSignal |
|
126 |
! |
|
1 | 127 |
|
128 |
domainErrorSignal |
|
129 |
"return the signal which is raised on math errors |
|
130 |
(such as log of 0 etc.)" |
|
131 |
||
132 |
^ DomainErrorSignal |
|
133 |
! |
|
134 |
||
135 |
overflowSignal |
|
136 |
"return the signal which is raised on overflow conditions (in floats)" |
|
137 |
||
138 |
^ OverflowSignal |
|
139 |
! |
|
140 |
||
141 |
underflowSignal |
|
142 |
"return the signal which is raised on underflow conditions (in floats)" |
|
143 |
||
144 |
^ UnderflowSignal |
|
145 |
! |
|
146 |
||
345 | 147 |
unorderedSignal |
148 |
"return the signal which is raised when numbers are compared, |
|
149 |
for which no ordering is defined (for example: complex numbers)" |
|
150 |
||
3393
f07a758f8c69
oops - unorderedSignal did not
Claus Gittinger <cg@exept.de>
parents:
3134
diff
changeset
|
151 |
^ UnorderedSignal |
1 | 152 |
! ! |
153 |
||
701 | 154 |
!ArithmeticValue methodsFor:'arithmetic'! |
155 |
||
156 |
* something |
|
157 |
"return the product of the receiver and the argument" |
|
158 |
||
159 |
^ self subclassResponsibility |
|
160 |
! |
|
161 |
||
162 |
+ something |
|
163 |
"return the sum of the receiver and the argument" |
|
164 |
||
165 |
^ self subclassResponsibility |
|
166 |
! |
|
167 |
||
168 |
- something |
|
169 |
"return the difference of the receiver and the argument" |
|
170 |
||
171 |
^ self subclassResponsibility |
|
172 |
! |
|
173 |
||
174 |
/ something |
|
175 |
"return the quotient of the receiver and the argument" |
|
1 | 176 |
|
701 | 177 |
^ self subclassResponsibility |
178 |
! |
|
179 |
||
180 |
// something |
|
1881 | 181 |
"return the integer quotient of dividing the receiver by aNumber with |
182 |
truncation towards negative infinity." |
|
701 | 183 |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
184 |
"/ Note: '^ (self / something) floor' may lead to infinite recursion |
1172 | 185 |
|
186 |
^ self subclassResponsibility |
|
187 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
188 |
"Modified: / 5.11.1996 / 11:46:27 / cg" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
189 |
"Modified: / 12.2.1998 / 16:31:36 / stefan" |
701 | 190 |
! |
191 |
||
192 |
\\ something |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
193 |
"return the receiver modulo something. |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
194 |
The remainder has the same sign as something," |
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 |
|
223 |
with truncation towards zero." |
|
224 |
||
225 |
^ (self / something) truncated |
|
226 |
! |
|
227 |
||
228 |
reciprocal |
|
229 |
"return the receivers reciprocal" |
|
230 |
||
231 |
^ self class unity / self |
|
232 |
! |
|
1 | 233 |
|
701 | 234 |
rem:something |
235 |
"Return the integer remainder of dividing the receiver by the argument |
|
236 |
with truncation towards zero. |
|
237 |
The remainder has the same sign as the receiver." |
|
238 |
||
239 |
^ self - ((self quo:something) * something) |
|
240 |
! ! |
|
241 |
||
1888 | 242 |
!ArithmeticValue methodsFor:'coercing'! |
243 |
||
244 |
coerce:aNumber |
|
245 |
"convert aNumber into an instance of the receivers class and return it." |
|
246 |
||
247 |
^ self subclassResponsibility |
|
248 |
! |
|
249 |
||
250 |
generality |
|
251 |
"return a number giving the receivers generality, that number is |
|
252 |
used to convert one of the arguments in a mixed expression. |
|
253 |
The generality has to be defined in subclasses, |
|
254 |
such that gen(a) > gen(b) iff, conversion of b into a's class |
|
255 |
does not cut precision. For example, Integer has 40, Float has 80, |
|
256 |
meaning that if we convert a Float to an Integer, some precision may |
|
257 |
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
|
258 |
which converts the lower-precision number to the higher precision |
1888 | 259 |
numbers class, when mixed-type arithmetic is performed." |
260 |
||
261 |
^ self subclassResponsibility |
|
262 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
263 |
"Modified: / 5.11.1996 / 15:05:30 / cg" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
264 |
"Modified: / 13.2.1998 / 15:36:01 / stefan" |
1888 | 265 |
! |
266 |
||
267 |
retry:aSymbol coercing:aNumber |
|
268 |
"arithmetic represented by the binary operator, aSymbol, |
|
269 |
could not be performed with the receiver and the argument, aNumber, |
|
270 |
because of the differences in representation. |
|
271 |
Coerce either the receiver or the argument, depending on which has higher |
|
272 |
generality, and try again. |
|
273 |
If the operation is compare for same value (=), return false if |
|
274 |
the argument is not a Number. |
|
275 |
If the generalities are the same, create an error message, since this |
|
276 |
means that a subclass has not been fully implemented." |
|
277 |
||
278 |
|hasGenerality myGenerality otherGenerality| |
|
279 |
||
280 |
hasGenerality := aNumber respondsTo:#generality. |
|
281 |
hasGenerality ifFalse:[ |
|
282 |
(aSymbol == #=) ifTrue:[ |
|
283 |
^ false |
|
284 |
]. |
|
285 |
(aSymbol == #~=) ifTrue:[ |
|
286 |
^ true |
|
287 |
]. |
|
288 |
^ self error:'retry:coercing: argument is not a number'. |
|
289 |
]. |
|
290 |
||
291 |
myGenerality := self generality. |
|
292 |
otherGenerality := aNumber generality. |
|
293 |
(myGenerality > otherGenerality) ifTrue:[ |
|
294 |
^ self perform:aSymbol with:(self coerce:aNumber) |
|
295 |
]. |
|
296 |
(myGenerality < otherGenerality) ifTrue:[ |
|
297 |
^ (aNumber coerce:self) perform:aSymbol with:aNumber |
|
298 |
]. |
|
299 |
self error:'retry:coercing: oops - same generality' |
|
300 |
||
301 |
"Modified: 5.11.1996 / 15:03:38 / cg" |
|
302 |
! ! |
|
303 |
||
701 | 304 |
!ArithmeticValue methodsFor:'comparing'! |
305 |
||
306 |
< something |
|
307 |
"return true, if the argument is greater than the receiver" |
|
308 |
||
309 |
^ self subclassResponsibility |
|
1 | 310 |
! |
311 |
||
701 | 312 |
<= something |
313 |
"return true, if the argument is greater or equal than the receiver" |
|
314 |
||
315 |
^ (something < self) not |
|
316 |
! |
|
317 |
||
318 |
> something |
|
319 |
"return true, if the argument is less than the receiver" |
|
320 |
||
321 |
^ something < self |
|
322 |
! |
|
323 |
||
324 |
>= something |
|
325 |
"return true, if the argument is less or equal than the receiver" |
|
1 | 326 |
|
701 | 327 |
^ (self < something) not |
1 | 328 |
! |
329 |
||
701 | 330 |
compare:arg ifLess:lessBlock ifEqual:equalBlock ifGreater:greaterBlock |
331 |
"three-way compare - thanks to Self for this idea. |
|
332 |
Can be redefined in subclasses to do it with a single comparison if |
|
333 |
comparison is expensive." |
|
334 |
||
335 |
self < arg ifTrue:[ |
|
336 |
^ lessBlock value |
|
337 |
]. |
|
338 |
self = arg ifTrue:[ |
|
339 |
^ equalBlock value |
|
340 |
]. |
|
341 |
^ greaterBlock value |
|
342 |
! ! |
|
343 |
||
344 |
!ArithmeticValue methodsFor:'converting'! |
|
345 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
346 |
asDouble |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
347 |
"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
|
348 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
349 |
^ self asFloat |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
350 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
351 |
"Created: / 13.2.1998 / 15:40:14 / stefan" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
352 |
! |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
353 |
|
1 | 354 |
asFloat |
355 |
"return a float with same value" |
|
356 |
||
357 |
^ self subclassResponsibility |
|
358 |
! |
|
359 |
||
360 |
asFraction |
|
361 |
"return a fraction with same value" |
|
362 |
||
363 |
^ self subclassResponsibility |
|
364 |
! |
|
365 |
||
701 | 366 |
asInteger |
367 |
"return an integer with same value - might truncate" |
|
368 |
||
369 |
^ self truncated |
|
370 |
! |
|
371 |
||
1200 | 372 |
asShortFloat |
373 |
"return a shortFloat with same value" |
|
374 |
||
375 |
^ self asFloat asShortFloat |
|
376 |
||
377 |
"Modified: 17.4.1996 / 12:21:35 / cg" |
|
378 |
! |
|
379 |
||
701 | 380 |
degreesToRadians |
381 |
"interpreting the receiver as radians, return the degrees" |
|
382 |
||
383 |
^ self asFloat degreesToRadians |
|
384 |
! |
|
385 |
||
386 |
radiansToDegrees |
|
387 |
"interpreting the receiver as degrees, return the radians" |
|
388 |
||
389 |
^ self asFloat radiansToDegrees |
|
1 | 390 |
! ! |
391 |
||
4138
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
392 |
!ArithmeticValue methodsFor:'destructive arithmethic'! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
393 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
394 |
*= aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
395 |
"Return the product of self multiplied by aNumber. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
396 |
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
|
397 |
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
|
398 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
399 |
to do optimisations" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
400 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
401 |
^ self * aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
402 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
403 |
"Created: / 28.4.1999 / 11:46:11 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
404 |
"Modified: / 28.4.1999 / 11:53:28 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
405 |
! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
406 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
407 |
+= aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
408 |
"Return the sum of self and aNumber. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
409 |
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
|
410 |
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
|
411 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
412 |
to do optimisations" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
413 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
414 |
^ self + aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
415 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
416 |
"Created: / 28.4.1999 / 10:13:41 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
417 |
"Modified: / 28.4.1999 / 11:54:11 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
418 |
! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
419 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
420 |
-= aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
421 |
"Return the difference of self and aNumber. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
422 |
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
|
423 |
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
|
424 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
425 |
to do optimisations" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
426 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
427 |
^ self - aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
428 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
429 |
"Created: / 28.4.1999 / 10:13:58 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
430 |
"Modified: / 28.4.1999 / 11:54:37 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
431 |
! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
432 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
433 |
/= aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
434 |
"Return the quotient of self and aNumber. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
435 |
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
|
436 |
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
|
437 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
438 |
to do optimisations" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
439 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
440 |
^ self / aNumber |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
441 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
442 |
"Created: / 28.4.1999 / 11:46:22 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
443 |
"Modified: / 28.4.1999 / 11:55:06 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
444 |
! |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
445 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
446 |
div2 |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
447 |
"Return the quotient of self divided by 2. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
448 |
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
|
449 |
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
|
450 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
451 |
to do optimisations" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
452 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
453 |
^ self // 2 |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
454 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
455 |
"Created: / 28.4.1999 / 10:12:44 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
456 |
"Modified: / 28.4.1999 / 11:56:09 / stefan" |
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 |
|
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
459 |
mul2 |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
460 |
"Return the product of self multiplied by 2. |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
461 |
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
|
462 |
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
|
463 |
This method can be redefined for constructed datatypes |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
464 |
to do optimisations" |
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 * 2 |
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 / 10:12:55 / stefan" |
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
469 |
"Modified: / 28.4.1999 / 11:56:38 / 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 |
|
701 | 472 |
!ArithmeticValue methodsFor:'double dispatching'! |
1 | 473 |
|
1888 | 474 |
differenceFromFixedPoint:aFixedPoint |
475 |
"the receiver does not know how to subtract from a fixedPoint number - |
|
476 |
retry the operation by coercing to higher generality" |
|
477 |
||
478 |
^ aFixedPoint retry:#- coercing:self |
|
479 |
||
480 |
"Created: 5.11.1996 / 14:55:51 / cg" |
|
481 |
! |
|
482 |
||
701 | 483 |
differenceFromFloat:aFloat |
484 |
"the receiver does not know how to subtract from a float - |
|
485 |
retry the operation by coercing to higher generality" |
|
1 | 486 |
|
701 | 487 |
^ aFloat retry:#- coercing:self |
1 | 488 |
! |
489 |
||
701 | 490 |
differenceFromFraction:aFraction |
491 |
"the receiver does not know how to subtract from a fraction - |
|
492 |
retry the operation by coercing to higher generality" |
|
1 | 493 |
|
701 | 494 |
^ aFraction retry:#- coercing:self |
1 | 495 |
! |
496 |
||
701 | 497 |
differenceFromInteger:anInteger |
498 |
"the receiver does not know how to subtract from an integer - |
|
499 |
retry the operation by coercing to higher generality" |
|
1 | 500 |
|
701 | 501 |
^ anInteger retry:#- coercing:self |
1 | 502 |
! |
503 |
||
1200 | 504 |
differenceFromShortFloat:aShortFloat |
505 |
"the receiver does not know how to subtract from a shortFloat - |
|
506 |
retry the operation by coercing to higher generality" |
|
507 |
||
508 |
^ aShortFloat retry:#- coercing:self |
|
509 |
||
510 |
"Created: 17.4.1996 / 12:33:16 / cg" |
|
511 |
! |
|
512 |
||
1888 | 513 |
lessFromFixedPoint:aFixedPoint |
514 |
"the receiver does not know how to compare to a fixedPoint number - |
|
515 |
retry the operation by coercing to higher generality" |
|
516 |
||
517 |
^ aFixedPoint retry:#< coercing:self |
|
518 |
||
519 |
"Created: 5.11.1996 / 14:56:12 / cg" |
|
520 |
! |
|
521 |
||
701 | 522 |
lessFromFloat:aFloat |
523 |
"the receiver does not know how to compare to a float - |
|
524 |
retry the operation by coercing to higher generality" |
|
1 | 525 |
|
701 | 526 |
^ aFloat retry:#< coercing:self |
1 | 527 |
! |
528 |
||
701 | 529 |
lessFromFraction:aFraction |
530 |
"the receiver does not know how to compare to a fraction - |
|
531 |
retry the operation by coercing to higher generality" |
|
1 | 532 |
|
701 | 533 |
^ aFraction retry:#< coercing:self |
1 | 534 |
! |
535 |
||
701 | 536 |
lessFromInteger:anInteger |
537 |
"the receiver does not know how to compare to an integer - |
|
538 |
retry the operation by coercing to higher generality" |
|
1 | 539 |
|
701 | 540 |
^ anInteger retry:#< coercing:self |
1 | 541 |
! |
542 |
||
1200 | 543 |
lessFromShortFloat:aShortFloat |
544 |
"the receiver does not know how to compare to a shortFloat - |
|
545 |
retry the operation by coercing to higher generality" |
|
546 |
||
547 |
^ aShortFloat retry:#< coercing:self |
|
548 |
||
549 |
"Modified: 17.4.1996 / 12:33:33 / cg" |
|
550 |
! |
|
551 |
||
1888 | 552 |
productFromFixedPoint:aFixedPoint |
553 |
"the receiver does not know how to multiply a fixed point number - |
|
554 |
retry the operation by coercing to higher generality" |
|
555 |
||
556 |
^ aFixedPoint retry:#* coercing:self |
|
557 |
||
558 |
"Created: 5.11.1996 / 14:56:28 / cg" |
|
559 |
! |
|
560 |
||
701 | 561 |
productFromFloat:aFloat |
562 |
"the receiver does not know how to multiply a float - |
|
563 |
retry the operation by coercing to higher generality" |
|
1 | 564 |
|
701 | 565 |
^ aFloat retry:#* coercing:self |
1 | 566 |
! |
567 |
||
701 | 568 |
productFromFraction:aFraction |
569 |
"the receiver does not know how to multiply a fraction - |
|
570 |
retry the operation by coercing to higher generality" |
|
1 | 571 |
|
701 | 572 |
^ aFraction retry:#* coercing:self |
1 | 573 |
! |
574 |
||
701 | 575 |
productFromInteger:anInteger |
576 |
"the receiver does not know how to multiply an integer - |
|
577 |
retry the operation by coercing to higher generality" |
|
1 | 578 |
|
701 | 579 |
^ anInteger retry:#* coercing:self |
1 | 580 |
! |
581 |
||
1200 | 582 |
productFromShortFloat:aShortFloat |
583 |
"the receiver does not know how to multiply a shortFloat - |
|
584 |
retry the operation by coercing to higher generality" |
|
585 |
||
586 |
^ aShortFloat retry:#* coercing:self |
|
587 |
||
588 |
"Created: 17.4.1996 / 12:33:48 / cg" |
|
589 |
! |
|
590 |
||
1888 | 591 |
quotientFromFixedPoint:aFixedPoint |
592 |
"the receiver does not know how to divide a fixed point number - |
|
593 |
retry the operation by coercing to higher generality" |
|
594 |
||
595 |
^ aFixedPoint retry:#/ coercing:self |
|
596 |
||
597 |
"Created: 5.11.1996 / 14:56:41 / cg" |
|
598 |
! |
|
599 |
||
701 | 600 |
quotientFromFloat:aFloat |
601 |
"the receiver does not know how to divide a float - |
|
602 |
retry the operation by coercing to higher generality" |
|
1 | 603 |
|
701 | 604 |
^ aFloat retry:#/ coercing:self |
1 | 605 |
! |
606 |
||
701 | 607 |
quotientFromFraction:aFraction |
608 |
"the receiver does not know how to divide a fraction - |
|
609 |
retry the operation by coercing to higher generality" |
|
1 | 610 |
|
701 | 611 |
^ aFraction retry:#/ coercing:self |
1 | 612 |
! |
613 |
||
701 | 614 |
quotientFromInteger:anInteger |
615 |
"the receiver does not know how to divide an integer - |
|
1 | 616 |
retry the operation by coercing to higher generality" |
617 |
||
701 | 618 |
^ anInteger retry:#/ coercing:self |
1 | 619 |
! |
620 |
||
1200 | 621 |
quotientFromShortFloat:aShortFloat |
622 |
"the receiver does not know how to divide a shortFloat - |
|
623 |
retry the operation by coercing to higher generality" |
|
624 |
||
625 |
^ aShortFloat retry:#/ coercing:self |
|
626 |
||
627 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
628 |
! |
|
629 |
||
1888 | 630 |
sumFromFixedPoint:aFixedPoint |
631 |
"the receiver does not know how to add a fixed point number - |
|
632 |
retry the operation by coercing to higher generality" |
|
633 |
||
634 |
^ aFixedPoint retry:#+ coercing:self |
|
635 |
||
636 |
"Created: 5.11.1996 / 14:56:56 / cg" |
|
637 |
! |
|
638 |
||
1 | 639 |
sumFromFloat:aFloat |
640 |
"the receiver does not know how to add a float - |
|
641 |
retry the operation by coercing to higher generality" |
|
642 |
||
643 |
^ aFloat retry:#+ coercing:self |
|
644 |
! |
|
645 |
||
646 |
sumFromFraction:aFraction |
|
647 |
"the receiver does not know how to add a fraction - |
|
648 |
retry the operation by coercing to higher generality" |
|
649 |
||
650 |
^ aFraction retry:#+ coercing:self |
|
651 |
! |
|
652 |
||
701 | 653 |
sumFromInteger:anInteger |
654 |
"the receiver does not know how to add an integer - |
|
1 | 655 |
retry the operation by coercing to higher generality" |
656 |
||
701 | 657 |
^ anInteger retry:#+ coercing:self |
1200 | 658 |
! |
659 |
||
660 |
sumFromShortFloat:aShortFloat |
|
661 |
"the receiver does not know how to add a shortFloat - |
|
662 |
retry the operation by coercing to higher generality" |
|
663 |
||
664 |
^ aShortFloat retry:#+ coercing:self |
|
665 |
||
666 |
"Created: 17.4.1996 / 12:34:10 / cg" |
|
1 | 667 |
! ! |
668 |
||
669 |
!ArithmeticValue methodsFor:'misc math'! |
|
670 |
||
671 |
exp |
|
672 |
"return e ^ receiver" |
|
673 |
||
674 |
^ self asFloat exp |
|
675 |
! |
|
676 |
||
701 | 677 |
floorLog:radix |
678 |
"return the logarithm truncated as an integer" |
|
679 |
||
680 |
^ (self log:radix) floor |
|
681 |
! |
|
682 |
||
1 | 683 |
ln |
684 |
"return the natural logarithm of the receiver" |
|
685 |
||
686 |
^ self asFloat ln |
|
687 |
! |
|
688 |
||
689 |
log |
|
690 |
"return log base 10 of the receiver" |
|
691 |
||
692 |
^ self log:10 |
|
693 |
! |
|
694 |
||
695 |
log:aNumber |
|
696 |
"return log base aNumber of the receiver" |
|
697 |
||
698 |
^ self ln / aNumber ln |
|
699 |
! |
|
700 |
||
701 |
raisedTo:aNumber |
|
702 |
"return the receiver raised to aNumber" |
|
703 |
||
704 |
aNumber = 0 ifTrue:[^ 1]. |
|
705 |
aNumber = 1 ifTrue:[^ self]. |
|
706 |
aNumber isInteger ifTrue:[ |
|
213 | 707 |
^ self raisedToInteger:aNumber |
1 | 708 |
]. |
709 |
^ self asFloat raisedTo:aNumber |
|
710 |
! |
|
711 |
||
712 |
raisedToInteger:anInteger |
|
713 |
"return the receiver raised to anInteger" |
|
714 |
||
715 |
|count result| |
|
716 |
||
717 |
result := self coerce:1. |
|
718 |
count := anInteger abs. |
|
719 |
count timesRepeat:[result := result * self]. |
|
720 |
(anInteger < 0) ifTrue:[ |
|
213 | 721 |
^ 1 / result |
1 | 722 |
]. |
723 |
^ result |
|
701 | 724 |
! |
725 |
||
726 |
sqrt |
|
727 |
"return the square root of the receiver" |
|
728 |
||
729 |
^ self asFloat sqrt |
|
730 |
! |
|
731 |
||
732 |
squared |
|
733 |
"return receiver * receiver" |
|
734 |
||
735 |
^ self * self |
|
736 |
! ! |
|
737 |
||
738 |
!ArithmeticValue methodsFor:'queries'! |
|
739 |
||
740 |
respondsToArithmetic |
|
741 |
"return true, if the receiver responds to arithmetic messages" |
|
742 |
||
743 |
^ true |
|
744 |
! ! |
|
745 |
||
746 |
!ArithmeticValue methodsFor:'testing'! |
|
747 |
||
748 |
denominator |
|
749 |
"return the denominator of the receiver" |
|
750 |
||
751 |
^ 1 |
|
752 |
! |
|
753 |
||
754 |
even |
|
755 |
"return true if the receiver is divisible by 2" |
|
756 |
||
757 |
^ self truncated asInteger even |
|
758 |
! |
|
759 |
||
3635 | 760 |
isComplex |
761 |
"Answer whether the receiver has an imaginary part" |
|
762 |
||
763 |
^ false |
|
764 |
||
765 |
"Modified: / 9.7.1998 / 10:19:27 / cg" |
|
766 |
! |
|
767 |
||
701 | 768 |
negative |
769 |
"return true, if the receiver is < 0" |
|
770 |
||
771 |
" this would lead to infinite recursion ... |
|
772 |
^ (self < 0) |
|
773 |
" |
|
774 |
^ self subclassResponsibility |
|
775 |
! |
|
776 |
||
777 |
numerator |
|
778 |
"return the numerator of the receiver." |
|
779 |
||
780 |
^ self |
|
781 |
! |
|
782 |
||
783 |
odd |
|
784 |
"return true if the receiver is not divisible by 2" |
|
785 |
||
786 |
^ self even not |
|
787 |
! |
|
788 |
||
789 |
positive |
|
790 |
"return true, if the receiver is >= 0" |
|
791 |
||
792 |
^ self negative not |
|
793 |
! |
|
794 |
||
795 |
sign |
|
796 |
"return the sign of the receiver" |
|
797 |
||
798 |
(self < 0) ifTrue:[^ -1]. |
|
799 |
(self > 0) ifTrue:[^ 1]. |
|
800 |
^ 0 |
|
801 |
! |
|
802 |
||
803 |
strictlyPositive |
|
804 |
"return true, if the receiver is > 0" |
|
805 |
||
806 |
^ (self > 0) |
|
1 | 807 |
! ! |
808 |
||
809 |
!ArithmeticValue methodsFor:'trigonometric'! |
|
810 |
||
811 |
arcCos |
|
812 |
"return the arccosine of the receiver (in radians)" |
|
813 |
||
814 |
^ self asFloat arcCos |
|
815 |
! |
|
816 |
||
817 |
arcSin |
|
818 |
"return the arcsine of the receiver (in radians)" |
|
819 |
||
820 |
^ self asFloat arcSin |
|
821 |
! |
|
822 |
||
823 |
arcTan |
|
824 |
"return the arctangens of the receiver (in radians)" |
|
825 |
||
826 |
^ self asFloat arcTan |
|
701 | 827 |
! |
828 |
||
829 |
cos |
|
830 |
"return the cosine of the receiver (interpreted as radians)" |
|
831 |
||
832 |
^ self asFloat cos |
|
833 |
! |
|
834 |
||
835 |
sin |
|
836 |
"return the sine of the receiver (interpreted as radians)" |
|
837 |
||
838 |
^ self asFloat sin |
|
839 |
! |
|
840 |
||
841 |
tan |
|
842 |
"return the tangens of the receiver (interpreted as radians)" |
|
843 |
||
844 |
^ self asFloat tan |
|
1 | 845 |
! ! |
846 |
||
701 | 847 |
!ArithmeticValue methodsFor:'truncation & rounding'! |
1 | 848 |
|
701 | 849 |
ceiling |
850 |
"return the integer nearest the receiver towards positive infinity." |
|
1 | 851 |
|
701 | 852 |
|anInteger| |
1 | 853 |
|
701 | 854 |
anInteger := self // 1. "truncates towards negative infinity" |
855 |
anInteger = self ifTrue:[^ anInteger]. |
|
856 |
^ anInteger + 1 |
|
1 | 857 |
! |
858 |
||
701 | 859 |
floor |
860 |
"return the receiver truncated towards negative infinity" |
|
1 | 861 |
|
701 | 862 |
^ self // 1 |
1 | 863 |
! |
864 |
||
701 | 865 |
roundTo:aNumber |
866 |
"return the receiver rounded to multiples of aNumber" |
|
1 | 867 |
|
701 | 868 |
^ (self / aNumber) rounded * aNumber |
869 |
! |
|
870 |
||
871 |
rounded |
|
872 |
"return the integer nearest the receiver" |
|
873 |
||
1881 | 874 |
self negative ifTrue:[ |
875 |
^ (self - 0.5) ceiling |
|
876 |
]. |
|
701 | 877 |
^ (self + 0.5) floor |
1881 | 878 |
|
879 |
"Modified: 5.11.1996 / 11:31:59 / cg" |
|
1 | 880 |
! |
881 |
||
701 | 882 |
truncateTo:aNumber |
883 |
"return the receiver truncated to multiples of aNumber" |
|
1 | 884 |
|
701 | 885 |
^ ((self / aNumber) floor * aNumber) asInteger |
1 | 886 |
! |
887 |
||
701 | 888 |
truncated |
889 |
"return the receiver truncated towards zero" |
|
1 | 890 |
|
701 | 891 |
self negative ifTrue:[ |
892 |
^ self ceiling |
|
893 |
]. |
|
894 |
^ self floor |
|
895 |
! ! |
|
1 | 896 |
|
701 | 897 |
!ArithmeticValue class methodsFor:'documentation'! |
898 |
||
899 |
version |
|
4138
c5030c520941
New methods for destructive arithmethics like +=, -= ...
Stefan Vogel <sv@exept.de>
parents:
3982
diff
changeset
|
900 |
^ '$Header: /cvs/stx/stx/libbasic/ArithmeticValue.st,v 1.30 1999-05-04 10:59:13 stefan Exp $' |
1 | 901 |
! ! |
701 | 902 |
ArithmeticValue initialize! |