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