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