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