author | Claus Gittinger <cg@exept.de> |
Tue, 11 Nov 2014 15:26:04 +0100 | |
changeset 17038 | e5e3568f805e |
parent 16657 | a87789caf59d |
child 17189 | 13abc29bad4b |
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 |
" |
|
5948 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
1 | 14 |
Magnitude subclass:#ArithmeticValue |
1172 | 15 |
instanceVariableNames:'' |
16 |
classVariableNames:'DivisionByZeroSignal DomainErrorSignal OverflowSignal |
|
6342
fbbe62d5b17c
Remove obsolete (since years) AnyArithmeticSignal definitions.
Stefan Vogel <sv@exept.de>
parents:
6156
diff
changeset
|
17 |
UnderflowSignal ArithmeticSignal UnorderedSignal |
fbbe62d5b17c
Remove obsolete (since years) AnyArithmeticSignal definitions.
Stefan Vogel <sv@exept.de>
parents:
6156
diff
changeset
|
18 |
ConversionErrorSignal' |
1172 | 19 |
poolDictionaries:'' |
20 |
category:'Magnitude-Numbers' |
|
1 | 21 |
! |
22 |
||
701 | 23 |
!ArithmeticValue class methodsFor:'documentation'! |
82 | 24 |
|
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
213 | 28 |
All Rights Reserved |
1 | 29 |
|
82 | 30 |
This software is furnished under a license and may be used |
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
1 | 38 |
|
82 | 39 |
documentation |
40 |
" |
|
41 |
ArithmeticValue is an abstract superclass for all things responding to |
|
155 | 42 |
arithmetic messages. It was inserted into the hierarchy, to allow objects |
43 |
like matrices, functions etc. to share the arithmetic methods defined here. |
|
44 |
||
5955 | 45 |
Notice, that what used to be signals are now exception classes - the class |
46 |
variables and signal accessors remain here for backward compatibility. |
|
47 |
||
1283 | 48 |
[class variables:] |
10326 | 49 |
ArithmeticSignal <Error> parent of all arithmetic signals |
50 |
(never raised itself) |
|
51 |
New: now a reference to ArithmeticError |
|
82 | 52 |
|
10326 | 53 |
DomainErrorSignal <Error> raised upon float errors |
54 |
(for example range in trigonometric) |
|
55 |
New: now a reference to DomainError |
|
1283 | 56 |
|
10326 | 57 |
DivisionByZeroSignal <Error> raised when division by 0 is attempted |
58 |
New: now a reference to ZeroDivide |
|
155 | 59 |
|
10326 | 60 |
OverflowSignal <Error> raised on overflow/underflow conditions |
61 |
UnderflowSignal in float arithmetic. |
|
62 |
Notice: some OperatingSystems do not |
|
63 |
provide enough information for ST/X to |
|
64 |
extract the real reason for the floatException |
|
65 |
thus raising DomainErrorSignal in these cases. |
|
155 | 66 |
|
1295 | 67 |
[author:] |
10326 | 68 |
Claus Gittinger |
1295 | 69 |
|
1283 | 70 |
[See also:] |
10326 | 71 |
Number |
82 | 72 |
" |
73 |
! ! |
|
1 | 74 |
|
701 | 75 |
!ArithmeticValue class methodsFor:'Signal constants'! |
76 |
||
77 |
arithmeticSignal |
|
78 |
"return the parent of all arithmetic signals" |
|
79 |
||
7403 | 80 |
^ ArithmeticError |
701 | 81 |
! |
82 |
||
83 |
divisionByZeroSignal |
|
84 |
"return the signal which is raised on division by zero" |
|
85 |
||
7403 | 86 |
^ ZeroDivide |
701 | 87 |
! |
1 | 88 |
|
89 |
domainErrorSignal |
|
90 |
"return the signal which is raised on math errors |
|
91 |
(such as log of 0 etc.)" |
|
92 |
||
7403 | 93 |
^ DomainError |
94 |
! |
|
95 |
||
96 |
imaginaryResultSignal |
|
97 |
"return the signal which is raised when an imaginary result would be created |
|
98 |
(such as when taking the sqrt of a negative number)" |
|
99 |
||
100 |
^ ImaginaryResultError |
|
1 | 101 |
! |
102 |
||
7440 | 103 |
operationNotPossibleSignal |
104 |
^ Error |
|
105 |
! |
|
106 |
||
1 | 107 |
overflowSignal |
108 |
"return the signal which is raised on overflow conditions (in floats)" |
|
109 |
||
7403 | 110 |
^ OverflowError |
1 | 111 |
! |
112 |
||
5955 | 113 |
rangeErrorSignal |
114 |
"return the parent of the overflow/underflow signals" |
|
115 |
||
116 |
^ RangeError |
|
117 |
! |
|
118 |
||
7440 | 119 |
undefinedResultSignal |
120 |
^ Error |
|
121 |
! |
|
122 |
||
1 | 123 |
underflowSignal |
124 |
"return the signal which is raised on underflow conditions (in floats)" |
|
125 |
||
7403 | 126 |
^ UnderflowError |
1 | 127 |
! |
128 |
||
345 | 129 |
unorderedSignal |
10326 | 130 |
"return the signal which is raised when numbers are compared, |
345 | 131 |
for which no ordering is defined (for example: complex numbers)" |
132 |
||
7403 | 133 |
^ UnorderedNumbersError |
1 | 134 |
! ! |
135 |
||
7440 | 136 |
!ArithmeticValue class methodsFor:'class initialization'! |
137 |
||
138 |
initialize |
|
139 |
"setup the signals" |
|
140 |
||
141 |
"/ notice that we now use class based exceptions; |
|
142 |
"/ however, for backward compatibility, the class variables |
|
143 |
"/ referring to those are kept for a while, for subclass compatibility. |
|
144 |
"/ Please convert your code to access those via the Signal-constants class |
|
145 |
"/ messages (accesors) instead of referring to the class variables. |
|
146 |
||
147 |
ArithmeticSignal := ArithmeticError. |
|
148 |
DomainErrorSignal := DomainError. |
|
149 |
DivisionByZeroSignal := ZeroDivide. |
|
150 |
UnorderedSignal := UnorderedNumbersError. |
|
151 |
ConversionErrorSignal := ConversionError. |
|
152 |
OverflowSignal := OverflowError. |
|
153 |
UnderflowSignal := UnderflowError. |
|
154 |
! ! |
|
155 |
||
156 |
!ArithmeticValue class methodsFor:'constants'! |
|
157 |
||
158 |
NaN |
|
8850 | 159 |
"return the constant NaN (not a Number)." |
160 |
||
7440 | 161 |
^ NotANumber new |
162 |
! |
|
163 |
||
164 |
infinity |
|
8850 | 165 |
"return something which represents infinity (for my instances)" |
166 |
||
7440 | 167 |
^ Infinity positive |
168 |
! |
|
169 |
||
7538
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
170 |
nan |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
171 |
"VW compatibility" |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
172 |
|
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
173 |
^ self NaN |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
174 |
! |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
175 |
|
7440 | 176 |
negativeInfinity |
8850 | 177 |
"return something which represents negative infinity (for my instances)" |
178 |
||
7440 | 179 |
^ Infinity negative |
180 |
! |
|
181 |
||
7726 | 182 |
unity |
11467 | 183 |
"return something which represents the unity element (for my instances). |
184 |
That is the neutral element for multiplication." |
|
8850 | 185 |
|
7726 | 186 |
self subclassResponsibility |
187 |
! |
|
188 |
||
7440 | 189 |
zero |
11467 | 190 |
"return something which represents the zero element (for my instances). |
191 |
That is the neutral element for addition." |
|
8850 | 192 |
|
7440 | 193 |
self subclassResponsibility |
194 |
! ! |
|
195 |
||
8892 | 196 |
!ArithmeticValue class methodsFor:'queries'! |
197 |
||
198 |
isAbstract |
|
11227 | 199 |
"Return if this class is an abstract class. |
200 |
True is returned for ArithmeticValue here; false for subclasses. |
|
201 |
Abstract subclasses must redefine again." |
|
202 |
||
8892 | 203 |
^ self == ArithmeticValue |
204 |
! ! |
|
205 |
||
15279 | 206 |
|
701 | 207 |
!ArithmeticValue methodsFor:'arithmetic'! |
208 |
||
209 |
* something |
|
11735 | 210 |
"return the product of the receiver and the argument." |
701 | 211 |
|
212 |
^ self subclassResponsibility |
|
213 |
! |
|
214 |
||
215 |
+ something |
|
216 |
"return the sum of the receiver and the argument" |
|
217 |
||
218 |
^ self subclassResponsibility |
|
219 |
! |
|
220 |
||
221 |
- something |
|
222 |
"return the difference of the receiver and the argument" |
|
223 |
||
224 |
^ self subclassResponsibility |
|
225 |
! |
|
226 |
||
227 |
/ something |
|
228 |
"return the quotient of the receiver and the argument" |
|
1 | 229 |
|
701 | 230 |
^ self subclassResponsibility |
231 |
! |
|
232 |
||
233 |
// something |
|
1881 | 234 |
"return the integer quotient of dividing the receiver by aNumber with |
235 |
truncation towards negative infinity." |
|
701 | 236 |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
237 |
"/ Note: '^ (self / something) floor' may lead to infinite recursion |
1172 | 238 |
|
239 |
^ self subclassResponsibility |
|
240 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
241 |
"Modified: / 5.11.1996 / 11:46:27 / cg" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
242 |
"Modified: / 12.2.1998 / 16:31:36 / stefan" |
701 | 243 |
! |
244 |
||
245 |
\\ something |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
246 |
"return the receiver modulo something. |
17038 | 247 |
The remainder has the same sign as the argument, something. |
6067 | 248 |
The following is always true: |
17038 | 249 |
(receiver // something) * something + (receiver \\ something) = receiver |
6067 | 250 |
" |
1 | 251 |
|
701 | 252 |
^ self - ((self // something) * something) |
3134 | 253 |
|
3982
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 |
0.9 \\ 0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
256 |
0.9 \\ -0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
257 |
-0.9 \\ 0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
258 |
-0.9 \\ -0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
259 |
" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
260 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
261 |
"Modified: / 12.2.1998 / 19:14:37 / stefan" |
701 | 262 |
! |
263 |
||
264 |
abs |
|
265 |
"return the absolute value of the receiver" |
|
266 |
||
267 |
(self negative) ifTrue:[^ self negated]. |
|
268 |
^ self |
|
269 |
! |
|
270 |
||
10326 | 271 |
dist:arg |
8757 | 272 |
"return the distance between arg and the receiver." |
273 |
||
274 |
^ (arg - self) abs |
|
275 |
||
276 |
" |
|
10326 | 277 |
(1%1) dist:(0%0) |
278 |
(1@1) dist:(0@0) |
|
279 |
(1) dist:(0) |
|
8757 | 280 |
" |
281 |
! |
|
282 |
||
16121 | 283 |
modulusOf:aNumber |
284 |
"return aNumber modulo the reciever. |
|
285 |
The remainder has the same sign as something. |
|
286 |
Defined for protocol compatibility with ModuloNumber." |
|
287 |
||
288 |
^ aNumber \\ self |
|
289 |
! |
|
290 |
||
701 | 291 |
negated |
292 |
"return the receiver negated" |
|
293 |
||
294 |
^ self class zero - self |
|
1 | 295 |
! |
296 |
||
701 | 297 |
quo:something |
298 |
"Return the integer quotient of dividing the receiver by the argument |
|
6067 | 299 |
with truncation towards zero. |
300 |
The following is always true: |
|
10326 | 301 |
(receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver |
6067 | 302 |
" |
701 | 303 |
|
304 |
^ (self / something) truncated |
|
305 |
! |
|
306 |
||
307 |
reciprocal |
|
308 |
"return the receivers reciprocal" |
|
309 |
||
310 |
^ self class unity / self |
|
311 |
! |
|
1 | 312 |
|
701 | 313 |
rem:something |
314 |
"Return the integer remainder of dividing the receiver by the argument |
|
315 |
with truncation towards zero. |
|
6067 | 316 |
The remainder has the same sign as the receiver. |
317 |
The following is always true: |
|
10326 | 318 |
(receiver quo: something) * something + (receiver rem: something) = receiver |
6067 | 319 |
" |
701 | 320 |
|
321 |
^ self - ((self quo:something) * something) |
|
7440 | 322 |
! |
323 |
||
324 |
uncheckedDivide:aNumber |
|
325 |
"return the quotient of the receiver and the argument, aNumber. |
|
7471 | 326 |
Do not check for divide by zero (return NaN or Infinity). |
7440 | 327 |
This operation is provided for emulators of other languages/semantics, |
328 |
where no exception is raised for these results (i.e. Java). |
|
329 |
Its only defined if the arguments type is the same as the receivers." |
|
330 |
||
331 |
aNumber isZero ifTrue:[ |
|
10326 | 332 |
self isZero ifTrue:[^ self class NaN]. |
333 |
self negative ifTrue:[^ self class negativeInfinity]. |
|
334 |
^ self class infinity. |
|
7440 | 335 |
]. |
336 |
^ self / aNumber |
|
701 | 337 |
! ! |
338 |
||
12578 | 339 |
!ArithmeticValue methodsFor:'arithmetic destructive'! |
340 |
||
341 |
*= aNumber |
|
342 |
"Return the product of self multiplied by aNumber. |
|
343 |
The receiver MAY, but NEED NOT be changed to contain the product. |
|
344 |
So this method must be used as: 'a := a *= 5'. |
|
345 |
This method can be redefined for constructed datatypes to do optimisations" |
|
346 |
||
347 |
^ self * aNumber |
|
348 |
||
349 |
"Created: / 28.4.1999 / 11:46:11 / stefan" |
|
350 |
"Modified: / 28.4.1999 / 11:53:28 / stefan" |
|
351 |
! |
|
352 |
||
353 |
+= aNumber |
|
354 |
"Return the sum of self and aNumber. |
|
355 |
The receiver MAY, but NEED NOT be changed to contain the sum. |
|
356 |
So this method must be used as: 'a := a += 5'. |
|
357 |
This method can be redefined for constructed datatypes to do optimisations" |
|
358 |
||
359 |
^ self + aNumber |
|
360 |
||
361 |
"Created: / 28.4.1999 / 10:13:41 / stefan" |
|
362 |
"Modified: / 28.4.1999 / 11:54:11 / stefan" |
|
363 |
! |
|
364 |
||
365 |
-= aNumber |
|
366 |
"Return the difference of self and aNumber. |
|
367 |
The receiver MAY, but NEED NOT be changed to contain the difference. |
|
368 |
So this method must be used as: 'a := a -= 5'. |
|
369 |
This method can be redefined for constructed datatypes to do optimisations" |
|
370 |
||
371 |
^ self - aNumber |
|
372 |
||
373 |
"Created: / 28.4.1999 / 10:13:58 / stefan" |
|
374 |
"Modified: / 28.4.1999 / 11:54:37 / stefan" |
|
375 |
! |
|
376 |
||
377 |
/= aNumber |
|
378 |
"Return the quotient of self and aNumber. |
|
379 |
The receiver MAY, but NEED NOT be changed to contain the quotient. |
|
380 |
So this method must be used as: 'a := a /= 5'. |
|
381 |
This method can be redefined for constructed datatypes to do optimisations" |
|
382 |
||
383 |
^ self / aNumber |
|
384 |
||
385 |
"Created: / 28.4.1999 / 11:46:22 / stefan" |
|
386 |
"Modified: / 28.4.1999 / 11:55:06 / stefan" |
|
387 |
! |
|
388 |
||
389 |
div2 |
|
390 |
"Return the quotient of self divided by 2. |
|
391 |
The receiver MAY, but NEED NOT be changed to contain the result. |
|
392 |
So this method must be used as: 'a := a div2. |
|
393 |
This method can be redefined for constructed datatypes to do optimisations" |
|
394 |
||
395 |
^ self // 2 |
|
396 |
||
397 |
"Created: / 28.4.1999 / 10:12:44 / stefan" |
|
398 |
"Modified: / 28.4.1999 / 11:56:09 / stefan" |
|
399 |
! |
|
400 |
||
401 |
mul2 |
|
402 |
"Return the product of self multiplied by 2. |
|
403 |
The receiver MAY, but NEED NOT be changed to contain the result. |
|
15279 | 404 |
So this method must be used as: a := a mul2. |
12578 | 405 |
This method can be redefined for constructed datatypes to do optimisations" |
406 |
||
407 |
^ self * 2 |
|
408 |
||
409 |
"Created: / 28.4.1999 / 10:12:55 / stefan" |
|
410 |
"Modified: / 28.4.1999 / 11:56:38 / stefan" |
|
411 |
! ! |
|
412 |
||
5238 | 413 |
!ArithmeticValue methodsFor:'coercing & converting'! |
1888 | 414 |
|
415 |
coerce:aNumber |
|
11725 | 416 |
"convert the argument aNumber into an instance of the receivers class and return it." |
1888 | 417 |
|
418 |
^ self subclassResponsibility |
|
419 |
! |
|
420 |
||
421 |
generality |
|
422 |
"return a number giving the receivers generality, that number is |
|
10326 | 423 |
used to convert one of the arguments in a mixed expression. |
1888 | 424 |
The generality has to be defined in subclasses, |
10326 | 425 |
such that gen(a) > gen(b) iff, conversion of b into a's class |
1888 | 426 |
does not cut precision. For example, Integer has 40, Float has 80, |
427 |
meaning that if we convert a Float to an Integer, some precision may |
|
428 |
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
|
429 |
which converts the lower-precision number to the higher precision |
1888 | 430 |
numbers class, when mixed-type arithmetic is performed." |
10326 | 431 |
|
1888 | 432 |
^ self subclassResponsibility |
433 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
434 |
"Modified: / 5.11.1996 / 15:05:30 / cg" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
435 |
"Modified: / 13.2.1998 / 15:36:01 / stefan" |
1888 | 436 |
! |
437 |
||
438 |
retry:aSymbol coercing:aNumber |
|
439 |
"arithmetic represented by the binary operator, aSymbol, |
|
10326 | 440 |
could not be performed with the receiver and the argument, aNumber, |
441 |
because of the differences in representation. |
|
442 |
Coerce either the receiver or the argument, depending on which has higher |
|
443 |
generality, and try again. |
|
1888 | 444 |
If the operation is compare for same value (=), return false if |
10326 | 445 |
the argument is not a Number. |
1888 | 446 |
If the generalities are the same, create an error message, since this |
447 |
means that a subclass has not been fully implemented." |
|
448 |
||
449 |
|hasGenerality myGenerality otherGenerality| |
|
450 |
||
451 |
hasGenerality := aNumber respondsTo:#generality. |
|
452 |
hasGenerality ifFalse:[ |
|
10326 | 453 |
(aSymbol == #=) ifTrue:[ |
454 |
^ false |
|
455 |
]. |
|
456 |
(aSymbol == #~=) ifTrue:[ |
|
457 |
^ true |
|
458 |
]. |
|
459 |
^ self error:'retry:coercing: argument is not a number'. |
|
1888 | 460 |
]. |
461 |
||
462 |
myGenerality := self generality. |
|
463 |
otherGenerality := aNumber generality. |
|
464 |
(myGenerality > otherGenerality) ifTrue:[ |
|
10326 | 465 |
^ self perform:aSymbol with:(self coerce:aNumber) |
1888 | 466 |
]. |
467 |
(myGenerality < otherGenerality) ifTrue:[ |
|
10326 | 468 |
^ (aNumber coerce:self) perform:aSymbol with:aNumber |
1888 | 469 |
]. |
10324 | 470 |
self error:'retry:coercing: oops - same generality; retry should not happen' |
1888 | 471 |
|
472 |
"Modified: 5.11.1996 / 15:03:38 / cg" |
|
16657 | 473 |
! |
474 |
||
475 |
retry:aSymbol coercing:aNumber with:anArgument |
|
476 |
"arithmetic represented by the binary operator, aSymbol, |
|
477 |
could not be performed with the receiver and the argument, aNumber, |
|
478 |
because of the differences in representation. |
|
479 |
Coerce either the receiver or the argument, depending on which has higher |
|
480 |
generality, and try again. |
|
481 |
If the operation is compare for same value (=), return false if |
|
482 |
the argument is not a Number. |
|
483 |
If the generalities are the same, create an error message, since this |
|
484 |
means that a subclass has not been fully implemented." |
|
485 |
||
486 |
|hasGenerality myGenerality otherGenerality| |
|
487 |
||
488 |
hasGenerality := aNumber respondsTo:#generality. |
|
489 |
hasGenerality ifFalse:[ |
|
490 |
^ self error:'retry:coercing: argument is not a number'. |
|
491 |
]. |
|
492 |
||
493 |
myGenerality := self generality. |
|
494 |
otherGenerality := aNumber generality. |
|
495 |
(myGenerality > otherGenerality) ifTrue:[ |
|
496 |
^ self perform:aSymbol with:(self coerce:aNumber) with:anArgument |
|
497 |
]. |
|
498 |
(myGenerality < otherGenerality) ifTrue:[ |
|
499 |
^ (aNumber coerce:self) perform:aSymbol with:aNumber with:anArgument |
|
500 |
]. |
|
501 |
self error:'retry:coercing: oops - same generality; retry should not happen' |
|
502 |
||
503 |
"Modified: 5.11.1996 / 15:03:38 / cg" |
|
1888 | 504 |
! ! |
505 |
||
701 | 506 |
!ArithmeticValue methodsFor:'converting'! |
507 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
508 |
asDouble |
7429 | 509 |
"ST80 compatibility: return a double with receivers value. |
510 |
our floats are the identical to ST80 doubles" |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
511 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
512 |
^ self asFloat |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
513 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
514 |
"Created: / 13.2.1998 / 15:40:14 / stefan" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
515 |
! |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
516 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
517 |
asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
518 |
"return the receiver as fixedPoint number. |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
519 |
Q: what should the scale be here ?" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
520 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
521 |
^ self asFraction asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
522 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
523 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
524 |
0.3 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
525 |
0.5 asFixedPoint |
10326 | 526 |
(1/5) asFloat asFixedPoint |
527 |
(1/3) asFloat asFixedPoint |
|
528 |
(2/3) asFloat asFixedPoint |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
529 |
(1/8) asFloat asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
530 |
3.14159 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
531 |
0.0000001 asFraction |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
532 |
0.0000001 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
533 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
534 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
535 |
"Modified: / 25.10.1997 / 15:36:54 / cg" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
536 |
! |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
537 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
538 |
asFixedPoint:scale |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
539 |
"return the receiver as fixedPoint number with the given |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
540 |
number of post-decimal-digits." |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
541 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
542 |
^ self asFraction asFixedPoint:scale |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
543 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
544 |
" |
10326 | 545 |
0.3 asFixedPoint:4 |
546 |
0.3 asFixedPoint:3 |
|
547 |
0.3 asFixedPoint:2 |
|
548 |
0.3 asFixedPoint:1 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
549 |
0.3 asFixedPoint:0 |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
550 |
|
10326 | 551 |
0.5 asFixedPoint:3 |
552 |
(1/5) asFloat asFixedPoint:1 |
|
553 |
(1/8) asFloat asFixedPoint:1 |
|
554 |
1.0 asFixedPoint:2 |
|
555 |
3.14159 asFixedPoint:2 |
|
556 |
3.14159 asFixedPoint:3 |
|
557 |
(3.14159 asFixedPoint:2) asFixedPoint:5 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
558 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
559 |
! |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
560 |
|
12980 | 561 |
asFixedPointRoundedToScale |
562 |
"return the receiver as fixedPoint number, rounded to its scale." |
|
563 |
||
564 |
^ self asFixedPoint roundedToScale |
|
565 |
||
566 |
" |
|
567 |
0.3 asFixedPoint |
|
568 |
0.5 asFixedPoint |
|
569 |
(2/3) asFloat asFixedPoint |
|
570 |
(1/8) asFloat asFixedPoint |
|
571 |
3.14159 asFixedPoint |
|
572 |
||
573 |
0.3 asFixedPointRoundedToScale |
|
574 |
0.5 asFixedPointRoundedToScale |
|
575 |
(2/3) asFloat asFixedPointRoundedToScale |
|
576 |
(1/8) asFloat asFixedPointRoundedToScale |
|
577 |
3.14159 asFixedPointRoundedToScale |
|
578 |
" |
|
579 |
||
580 |
"Created: / 02-08-2010 / 13:32:16 / cg" |
|
581 |
! |
|
582 |
||
583 |
asFixedPointRoundedToScale:scale |
|
584 |
"return the receiver as fixedPoint number with the given |
|
585 |
number of post-decimal-digits, rounded to its scale" |
|
586 |
||
587 |
^ (self asFixedPoint:scale) roundedToScale |
|
588 |
||
589 |
"Created: / 02-08-2010 / 13:33:11 / cg" |
|
590 |
! |
|
591 |
||
1 | 592 |
asFloat |
593 |
"return a float with same value" |
|
594 |
||
595 |
^ self subclassResponsibility |
|
596 |
! |
|
597 |
||
5986 | 598 |
asFloatD |
599 |
"return a double precision float with same value. |
|
600 |
Added for ANSI compatibility" |
|
601 |
||
602 |
^ self asFloat |
|
603 |
||
604 |
"Created: / 7.9.2001 / 13:36:48 / cg" |
|
605 |
"Modified: / 7.9.2001 / 13:38:10 / cg" |
|
606 |
! |
|
607 |
||
608 |
asFloatE |
|
609 |
"return a single precision float with same value. |
|
610 |
Added for ANSI compatibility" |
|
611 |
||
612 |
^ self asShortFloat |
|
613 |
||
614 |
"Created: / 7.9.2001 / 13:37:06 / cg" |
|
615 |
"Modified: / 7.9.2001 / 13:38:15 / cg" |
|
616 |
! |
|
617 |
||
618 |
asFloatQ |
|
619 |
"return a quad precision float with same value. |
|
620 |
Added for ANSI compatibility" |
|
621 |
||
622 |
^ self asLongFloat |
|
623 |
||
624 |
"Created: / 7.9.2001 / 13:38:44 / cg" |
|
625 |
! |
|
626 |
||
1 | 627 |
asFraction |
628 |
"return a fraction with same value" |
|
629 |
||
630 |
^ self subclassResponsibility |
|
631 |
! |
|
632 |
||
701 | 633 |
asInteger |
634 |
"return an integer with same value - might truncate" |
|
635 |
||
636 |
^ self truncated |
|
637 |
! |
|
638 |
||
7378 | 639 |
asLimitedPrecisionReal |
640 |
"return a float of any precision with same value" |
|
641 |
||
642 |
^ self asFloat |
|
643 |
! |
|
644 |
||
5986 | 645 |
asLongFloat |
646 |
"return a longFloat with same value" |
|
647 |
||
10315 | 648 |
"WARNING: could loose precision here, if not redefined in concrete classes which |
649 |
have more than float precision (i.e. LargeIntegers and Fractions)" |
|
7355 | 650 |
|
10315 | 651 |
^ self asFloat asLongFloat "/ subclassResponsibility |
5986 | 652 |
|
653 |
"Modified: / 17.4.1996 / 12:21:35 / cg" |
|
654 |
"Created: / 7.9.2001 / 13:39:31 / cg" |
|
655 |
! |
|
656 |
||
5987 | 657 |
asScaledDecimal:scale |
658 |
"return a fixedPoint approximating the receivers value" |
|
659 |
||
660 |
^ self asFixedPoint:scale |
|
661 |
||
662 |
" |
|
663 |
1.234 asScaledDecimal:2 |
|
664 |
" |
|
665 |
||
666 |
"Created: / 7.9.2001 / 13:46:26 / cg" |
|
667 |
"Modified: / 7.9.2001 / 13:46:44 / cg" |
|
668 |
! |
|
669 |
||
1200 | 670 |
asShortFloat |
671 |
"return a shortFloat with same value" |
|
672 |
||
673 |
^ self asFloat asShortFloat |
|
674 |
||
675 |
"Modified: 17.4.1996 / 12:21:35 / cg" |
|
676 |
! |
|
677 |
||
701 | 678 |
degreesToRadians |
679 |
"interpreting the receiver as radians, return the degrees" |
|
680 |
||
681 |
^ self asFloat degreesToRadians |
|
682 |
! |
|
683 |
||
684 |
radiansToDegrees |
|
685 |
"interpreting the receiver as degrees, return the radians" |
|
686 |
||
687 |
^ self asFloat radiansToDegrees |
|
1 | 688 |
! ! |
689 |
||
701 | 690 |
!ArithmeticValue methodsFor:'double dispatching'! |
1 | 691 |
|
7355 | 692 |
differenceFromComplex:aComplex |
693 |
"the receiver does not know how to subtract from a complex - |
|
694 |
retry the operation by coercing to higher generality" |
|
695 |
||
696 |
^ aComplex retry:#- coercing:self |
|
697 |
! |
|
698 |
||
1888 | 699 |
differenceFromFixedPoint:aFixedPoint |
700 |
"the receiver does not know how to subtract from a fixedPoint number - |
|
701 |
retry the operation by coercing to higher generality" |
|
702 |
||
703 |
^ aFixedPoint retry:#- coercing:self |
|
704 |
||
705 |
"Created: 5.11.1996 / 14:55:51 / cg" |
|
706 |
! |
|
707 |
||
701 | 708 |
differenceFromFloat:aFloat |
709 |
"the receiver does not know how to subtract from a float - |
|
710 |
retry the operation by coercing to higher generality" |
|
1 | 711 |
|
701 | 712 |
^ aFloat retry:#- coercing:self |
1 | 713 |
! |
714 |
||
701 | 715 |
differenceFromFraction:aFraction |
716 |
"the receiver does not know how to subtract from a fraction - |
|
717 |
retry the operation by coercing to higher generality" |
|
1 | 718 |
|
701 | 719 |
^ aFraction retry:#- coercing:self |
1 | 720 |
! |
721 |
||
701 | 722 |
differenceFromInteger:anInteger |
723 |
"the receiver does not know how to subtract from an integer - |
|
724 |
retry the operation by coercing to higher generality" |
|
1 | 725 |
|
701 | 726 |
^ anInteger retry:#- coercing:self |
1 | 727 |
! |
728 |
||
7440 | 729 |
differenceFromLargeFloat:aLargeFloat |
730 |
"the receiver does not know how to subtract from a largeFloat - |
|
731 |
retry the operation by coercing to higher generality" |
|
732 |
||
733 |
^ aLargeFloat retry:#- coercing:self |
|
734 |
! |
|
735 |
||
7399 | 736 |
differenceFromLongFloat:aLongFloat |
737 |
"the receiver does not know how to subtract from a longFloat - |
|
738 |
retry the operation by coercing to higher generality" |
|
739 |
||
740 |
^ aLongFloat retry:#- coercing:self |
|
741 |
||
742 |
"Created: 17.4.1996 / 12:33:16 / cg" |
|
743 |
! |
|
744 |
||
1200 | 745 |
differenceFromShortFloat:aShortFloat |
746 |
"the receiver does not know how to subtract from a shortFloat - |
|
747 |
retry the operation by coercing to higher generality" |
|
748 |
||
749 |
^ aShortFloat retry:#- coercing:self |
|
750 |
||
751 |
"Created: 17.4.1996 / 12:33:16 / cg" |
|
752 |
! |
|
753 |
||
7355 | 754 |
equalFromComplex:aComplex |
755 |
"the receiver does not know how to compare to a complex number - |
|
756 |
retry the operation by coercing to higher generality" |
|
757 |
||
758 |
^ aComplex retry:#= coercing:self |
|
759 |
! |
|
760 |
||
761 |
equalFromFixedPoint:aFixedPoint |
|
762 |
"the receiver does not know how to compare to a fixed point - |
|
763 |
retry the operation by coercing to higher generality" |
|
764 |
||
765 |
^ aFixedPoint retry:#= coercing:self |
|
766 |
! |
|
767 |
||
768 |
equalFromFloat:aFloat |
|
769 |
"the receiver does not know how to compare to a float - |
|
770 |
retry the operation by coercing to higher generality" |
|
771 |
||
772 |
^ aFloat retry:#= coercing:self |
|
773 |
! |
|
774 |
||
775 |
equalFromFraction:aFraction |
|
776 |
"the receiver does not know how to compare to a fraction - |
|
777 |
retry the operation by coercing to higher generality" |
|
778 |
||
779 |
^ aFraction retry:#= coercing:self |
|
780 |
! |
|
781 |
||
782 |
equalFromInteger:anInteger |
|
783 |
"the receiver does not know how to compare to an integer - |
|
784 |
retry the operation by coercing to higher generality" |
|
785 |
||
786 |
^ anInteger retry:#= coercing:self |
|
787 |
! |
|
788 |
||
7440 | 789 |
equalFromLargeFloat:aLargeFloat |
790 |
"the receiver does not know how to compare to a large float - |
|
791 |
retry the operation by coercing to higher generality" |
|
792 |
||
793 |
^ aLargeFloat retry:#= coercing:self |
|
794 |
! |
|
795 |
||
7355 | 796 |
equalFromLongFloat:aLongFloat |
797 |
"the receiver does not know how to compare to a long float - |
|
798 |
retry the operation by coercing to higher generality" |
|
799 |
||
800 |
^ aLongFloat retry:#= coercing:self |
|
801 |
! |
|
802 |
||
803 |
equalFromShortFloat:aShortFloat |
|
804 |
"the receiver does not know how to compare to a short float - |
|
805 |
retry the operation by coercing to higher generality" |
|
806 |
||
807 |
^ aShortFloat retry:#= coercing:self |
|
808 |
! |
|
809 |
||
13007 | 810 |
integerQuotientFromInteger:anInteger |
811 |
"anInteger does not know how to divide by the receiver - |
|
812 |
retry the operation by coercing to higher generality" |
|
813 |
||
814 |
^ anInteger retry:#// coercing:self |
|
815 |
||
816 |
"Created: / 09-08-2010 / 19:49:26 / cg" |
|
817 |
! |
|
818 |
||
16657 | 819 |
isAlmostEqualToFromFloat:aFloat nEpsilon:nE |
820 |
"the receiver does not know how to compare to a short float - |
|
821 |
retry the operation by coercing to higher generality" |
|
822 |
||
823 |
^ aFloat retry:#isAlmostEqualTo:nEpsilon: coercing:self with:nE |
|
824 |
! |
|
825 |
||
826 |
isAlmostEqualToFromShortFloat:aShortFloat nEpsilon:nE |
|
827 |
"the receiver does not know how to compare to a short float - |
|
828 |
retry the operation by coercing to higher generality" |
|
829 |
||
830 |
^ aShortFloat retry:#isAlmostEqualTo:nEpsilon: coercing:self with:nE |
|
831 |
! |
|
832 |
||
1888 | 833 |
lessFromFixedPoint:aFixedPoint |
834 |
"the receiver does not know how to compare to a fixedPoint number - |
|
835 |
retry the operation by coercing to higher generality" |
|
836 |
||
837 |
^ aFixedPoint retry:#< coercing:self |
|
838 |
||
839 |
"Created: 5.11.1996 / 14:56:12 / cg" |
|
840 |
! |
|
841 |
||
701 | 842 |
lessFromFloat:aFloat |
843 |
"the receiver does not know how to compare to a float - |
|
844 |
retry the operation by coercing to higher generality" |
|
1 | 845 |
|
701 | 846 |
^ aFloat retry:#< coercing:self |
1 | 847 |
! |
848 |
||
701 | 849 |
lessFromFraction:aFraction |
850 |
"the receiver does not know how to compare to a fraction - |
|
851 |
retry the operation by coercing to higher generality" |
|
1 | 852 |
|
701 | 853 |
^ aFraction retry:#< coercing:self |
1 | 854 |
! |
855 |
||
701 | 856 |
lessFromInteger:anInteger |
857 |
"the receiver does not know how to compare to an integer - |
|
858 |
retry the operation by coercing to higher generality" |
|
1 | 859 |
|
701 | 860 |
^ anInteger retry:#< coercing:self |
1 | 861 |
! |
862 |
||
7440 | 863 |
lessFromLargeFloat:aLargeFloat |
864 |
"the receiver does not know how to compare to a largeFloat - |
|
865 |
retry the operation by coercing to higher generality" |
|
866 |
||
867 |
^ aLargeFloat retry:#< coercing:self |
|
868 |
! |
|
869 |
||
7399 | 870 |
lessFromLongFloat:aLongFloat |
871 |
"the receiver does not know how to compare to a longFloat - |
|
872 |
retry the operation by coercing to higher generality" |
|
873 |
||
874 |
^ aLongFloat retry:#< coercing:self |
|
875 |
||
876 |
"Modified: 17.4.1996 / 12:33:33 / cg" |
|
877 |
! |
|
878 |
||
1200 | 879 |
lessFromShortFloat:aShortFloat |
880 |
"the receiver does not know how to compare to a shortFloat - |
|
881 |
retry the operation by coercing to higher generality" |
|
882 |
||
883 |
^ aShortFloat retry:#< coercing:self |
|
884 |
||
885 |
"Modified: 17.4.1996 / 12:33:33 / cg" |
|
886 |
! |
|
887 |
||
14002 | 888 |
moduloFromInteger:anInteger |
889 |
"anInteger does not know how to compute the modulo from the receiver - |
|
890 |
retry the operation by coercing to higher generality" |
|
891 |
||
892 |
^ anInteger retry:#\\ coercing:self |
|
893 |
||
894 |
"Created: / 12-02-2012 / 20:38:13 / cg" |
|
895 |
! |
|
896 |
||
7355 | 897 |
productFromComplex:aComplex |
898 |
"the receiver does not know how to multiply a complex - |
|
899 |
retry the operation by coercing to higher generality" |
|
900 |
||
901 |
^ aComplex retry:#* coercing:self |
|
902 |
! |
|
903 |
||
1888 | 904 |
productFromFixedPoint:aFixedPoint |
905 |
"the receiver does not know how to multiply 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:28 / cg" |
|
911 |
! |
|
912 |
||
701 | 913 |
productFromFloat:aFloat |
914 |
"the receiver does not know how to multiply a float - |
|
915 |
retry the operation by coercing to higher generality" |
|
1 | 916 |
|
701 | 917 |
^ aFloat retry:#* coercing:self |
1 | 918 |
! |
919 |
||
701 | 920 |
productFromFraction:aFraction |
921 |
"the receiver does not know how to multiply a fraction - |
|
922 |
retry the operation by coercing to higher generality" |
|
1 | 923 |
|
701 | 924 |
^ aFraction retry:#* coercing:self |
1 | 925 |
! |
926 |
||
701 | 927 |
productFromInteger:anInteger |
928 |
"the receiver does not know how to multiply an integer - |
|
929 |
retry the operation by coercing to higher generality" |
|
1 | 930 |
|
701 | 931 |
^ anInteger retry:#* coercing:self |
1 | 932 |
! |
933 |
||
7440 | 934 |
productFromLargeFloat:aLargeFloat |
935 |
"the receiver does not know how to multiply a largeFloat - |
|
936 |
retry the operation by coercing to higher generality" |
|
937 |
||
938 |
^ aLargeFloat retry:#* coercing:self |
|
939 |
||
940 |
"Created: 17.4.1996 / 12:33:48 / cg" |
|
941 |
! |
|
942 |
||
7399 | 943 |
productFromLongFloat:aLongFloat |
944 |
"the receiver does not know how to multiply a longFloat - |
|
945 |
retry the operation by coercing to higher generality" |
|
946 |
||
947 |
^ aLongFloat retry:#* coercing:self |
|
948 |
||
949 |
"Created: 17.4.1996 / 12:33:48 / cg" |
|
950 |
! |
|
951 |
||
1200 | 952 |
productFromShortFloat:aShortFloat |
953 |
"the receiver does not know how to multiply a shortFloat - |
|
954 |
retry the operation by coercing to higher generality" |
|
955 |
||
956 |
^ aShortFloat retry:#* coercing:self |
|
957 |
||
958 |
"Created: 17.4.1996 / 12:33:48 / cg" |
|
959 |
! |
|
960 |
||
7355 | 961 |
quotientFromComplex:aComplex |
7440 | 962 |
"aComplex does not know how to divide by the receiver - |
7355 | 963 |
retry the operation by coercing to higher generality" |
964 |
||
965 |
^ aComplex retry:#/ coercing:self |
|
966 |
! |
|
967 |
||
1888 | 968 |
quotientFromFixedPoint:aFixedPoint |
7440 | 969 |
"aFixedPoint does not know how to divide by the receiver - |
1888 | 970 |
retry the operation by coercing to higher generality" |
971 |
||
972 |
^ aFixedPoint retry:#/ coercing:self |
|
973 |
||
974 |
"Created: 5.11.1996 / 14:56:41 / cg" |
|
975 |
! |
|
976 |
||
701 | 977 |
quotientFromFloat:aFloat |
7440 | 978 |
"aFloat does not know how to divide by the receiver - |
701 | 979 |
retry the operation by coercing to higher generality" |
1 | 980 |
|
701 | 981 |
^ aFloat retry:#/ coercing:self |
1 | 982 |
! |
983 |
||
701 | 984 |
quotientFromFraction:aFraction |
7440 | 985 |
"aFraction does not know how to divide by the receiver - |
701 | 986 |
retry the operation by coercing to higher generality" |
1 | 987 |
|
701 | 988 |
^ aFraction retry:#/ coercing:self |
1 | 989 |
! |
990 |
||
701 | 991 |
quotientFromInteger:anInteger |
7440 | 992 |
"anInteger does not know how to divide by the receiver - |
1 | 993 |
retry the operation by coercing to higher generality" |
994 |
||
701 | 995 |
^ anInteger retry:#/ coercing:self |
1 | 996 |
! |
997 |
||
7440 | 998 |
quotientFromLargeFloat:aLargeFloat |
999 |
"aLargeFloat does not know how to divide by the receiver - |
|
1000 |
retry the operation by coercing to higher generality" |
|
1001 |
||
1002 |
^ aLargeFloat retry:#/ coercing:self |
|
1003 |
||
1004 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
1005 |
! |
|
1006 |
||
7399 | 1007 |
quotientFromLongFloat:aLongFloat |
7440 | 1008 |
"aLongFloat does not know how to divide by the receiver - |
7399 | 1009 |
retry the operation by coercing to higher generality" |
1010 |
||
1011 |
^ aLongFloat retry:#/ coercing:self |
|
1012 |
||
1013 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
1014 |
! |
|
1015 |
||
1200 | 1016 |
quotientFromShortFloat:aShortFloat |
7440 | 1017 |
"aShortFloat does not know how to divide by the receiver - |
1200 | 1018 |
retry the operation by coercing to higher generality" |
1019 |
||
1020 |
^ aShortFloat retry:#/ coercing:self |
|
1021 |
||
1022 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
1023 |
! |
|
1024 |
||
14747 | 1025 |
remainderFromFloat:aFloat |
1026 |
"aFloat does not know how to compute the remainder with the receiver - |
|
1027 |
retry the operation by coercing to higher generality" |
|
1028 |
||
1029 |
^ aFloat retry:#rem: coercing:self |
|
1030 |
! |
|
1031 |
||
1032 |
remainderFromShortFloat:aShortFloat |
|
1033 |
"aShortFloat does not know how to compute the remainder with the receiver - |
|
1034 |
retry the operation by coercing to higher generality" |
|
1035 |
||
1036 |
^ aShortFloat retry:#rem: coercing:self |
|
1037 |
! |
|
1038 |
||
7355 | 1039 |
sumFromComplex:aComplex |
1040 |
"the receiver does not know how to add a complex - |
|
1041 |
retry the operation by coercing to higher generality" |
|
1042 |
||
1043 |
^ aComplex retry:#+ coercing:self |
|
1044 |
! |
|
1045 |
||
1888 | 1046 |
sumFromFixedPoint:aFixedPoint |
1047 |
"the receiver does not know how to add a fixed point number - |
|
1048 |
retry the operation by coercing to higher generality" |
|
1049 |
||
1050 |
^ aFixedPoint retry:#+ coercing:self |
|
1051 |
||
1052 |
"Created: 5.11.1996 / 14:56:56 / cg" |
|
1053 |
! |
|
1054 |
||
1 | 1055 |
sumFromFloat:aFloat |
1056 |
"the receiver does not know how to add a float - |
|
1057 |
retry the operation by coercing to higher generality" |
|
1058 |
||
1059 |
^ aFloat retry:#+ coercing:self |
|
1060 |
! |
|
1061 |
||
1062 |
sumFromFraction:aFraction |
|
1063 |
"the receiver does not know how to add a fraction - |
|
1064 |
retry the operation by coercing to higher generality" |
|
1065 |
||
1066 |
^ aFraction retry:#+ coercing:self |
|
1067 |
! |
|
1068 |
||
701 | 1069 |
sumFromInteger:anInteger |
1070 |
"the receiver does not know how to add an integer - |
|
1 | 1071 |
retry the operation by coercing to higher generality" |
1072 |
||
701 | 1073 |
^ anInteger retry:#+ coercing:self |
1200 | 1074 |
! |
1075 |
||
7440 | 1076 |
sumFromLargeFloat:aLargeFloat |
1077 |
"the receiver does not know how to add a largeFloat - |
|
1078 |
retry the operation by coercing to higher generality" |
|
1079 |
||
1080 |
^ aLargeFloat retry:#+ coercing:self |
|
1081 |
||
1082 |
"Created: 17.4.1996 / 12:34:10 / cg" |
|
1083 |
! |
|
1084 |
||
7399 | 1085 |
sumFromLongFloat:aLongFloat |
1086 |
"the receiver does not know how to add a longFloat - |
|
1087 |
retry the operation by coercing to higher generality" |
|
1088 |
||
1089 |
^ aLongFloat retry:#+ coercing:self |
|
1090 |
||
1091 |
"Created: 17.4.1996 / 12:34:10 / cg" |
|
1092 |
! |
|
1093 |
||
1200 | 1094 |
sumFromShortFloat:aShortFloat |
1095 |
"the receiver does not know how to add a shortFloat - |
|
1096 |
retry the operation by coercing to higher generality" |
|
1097 |
||
1098 |
^ aShortFloat retry:#+ coercing:self |
|
1099 |
||
1100 |
"Created: 17.4.1996 / 12:34:10 / cg" |
|
1 | 1101 |
! ! |
1102 |
||
7384 | 1103 |
!ArithmeticValue methodsFor:'mathematical functions'! |
1 | 1104 |
|
10326 | 1105 |
** aNumber |
9443 | 1106 |
"Answer the receiver raised to the power of the argument, aNumber." |
1107 |
"same as Number>>raisedTo:" |
|
1108 |
||
1109 |
^self raisedTo: aNumber |
|
1110 |
||
1111 |
"Created: / 15-07-2006 / 15:11:05 / cg" |
|
1112 |
"Modified: / 17-07-2006 / 12:51:33 / cg" |
|
1113 |
! |
|
1114 |
||
7635 | 1115 |
raisedTo: aNumber |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1116 |
aNumber isInteger ifTrue:[ |
10326 | 1117 |
^ self raisedToInteger:aNumber |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1118 |
]. |
7635 | 1119 |
^ self subclassResponsibility |
6858 | 1120 |
! |
1121 |
||
10326 | 1122 |
raisedToInteger:exp |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1123 |
"return the receiver raised to exp" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1124 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1125 |
|result e t| |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1126 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1127 |
"use the addition chaining algorithm, |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1128 |
which is much faster for big exp-arguments" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1129 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1130 |
result := 1. |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1131 |
t := self. |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1132 |
exp < 0 ifTrue:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1133 |
e := exp negated. |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1134 |
] ifFalse:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1135 |
e := exp. |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1136 |
]. |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1137 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1138 |
[e ~~ 0] whileTrue:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1139 |
[(e bitAnd:1) == 0] whileTrue:[ |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1140 |
e := e bitShift:-1. |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1141 |
t := t * t. |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1142 |
]. |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1143 |
e := e - 1. |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1144 |
result := result * t. |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1145 |
]. |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1146 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1147 |
(exp < 0) ifTrue:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1148 |
^ 1 / result |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1149 |
]. |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1150 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1151 |
^ result |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1152 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1153 |
" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1154 |
(2 raisedToInteger:216) |
10326 | 1155 |
(2 raisedTo:216) |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1156 |
-> 105312291668557186697918027683670432318895095400549111254310977536 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1157 |
|
10326 | 1158 |
(2 raisedToInteger:216) asFloat |
1159 |
(2 raisedTo:216) asFloat |
|
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1160 |
-> 1.05312E+65 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1161 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1162 |
(2 raisedToInteger:500) |
10326 | 1163 |
(2 raisedTo:500) |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1164 |
-> 3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376 |
10326 | 1165 |
2 raisedToInteger:10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1166 |
-> 1024 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1167 |
-2 raisedToInteger:10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1168 |
-> 1024 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1169 |
-2 raisedToInteger:9 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1170 |
-> -512 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1171 |
10 raisedToInteger:-10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1172 |
-> (1/10000000000) |
10326 | 1173 |
2 raisedToInteger:0 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1174 |
-> 1 |
10326 | 1175 |
2 raisedToInteger:-1 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1176 |
-> (1/2) |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1177 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1178 |
Time millisecondsToRun:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1179 |
10000 timesRepeat:[ |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1180 |
(2 raisedToInteger:500) |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1181 |
] |
10326 | 1182 |
] |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1183 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1184 |
Time millisecondsToRun:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1185 |
|bigNum| |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1186 |
bigNum := 2 raisedToInteger:500. |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1187 |
10 timesRepeat:[ |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1188 |
(bigNum raisedToInteger:500) |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1189 |
] |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1190 |
] |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1191 |
" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1192 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1193 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1194 |
"Modified: / 27.4.1999 / 16:16:11 / stefan" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1195 |
! |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1196 |
|
701 | 1197 |
squared |
1198 |
"return receiver * receiver" |
|
1199 |
||
1200 |
^ self * self |
|
1201 |
! ! |
|
1202 |
||
1203 |
!ArithmeticValue methodsFor:'queries'! |
|
1204 |
||
1205 |
respondsToArithmetic |
|
1206 |
"return true, if the receiver responds to arithmetic messages" |
|
1207 |
||
1208 |
^ true |
|
1209 |
! ! |
|
1210 |
||
1211 |
!ArithmeticValue methodsFor:'testing'! |
|
1212 |
||
1213 |
denominator |
|
1214 |
"return the denominator of the receiver" |
|
1215 |
||
1216 |
^ 1 |
|
1217 |
! |
|
1218 |
||
1219 |
even |
|
1220 |
"return true if the receiver is divisible by 2" |
|
1221 |
||
1222 |
^ self truncated asInteger even |
|
1223 |
! |
|
1224 |
||
3635 | 1225 |
isComplex |
7355 | 1226 |
"Answer whether the receiver has an imaginary part |
1227 |
(i.e. if it is a complex number). Always false here." |
|
3635 | 1228 |
|
1229 |
^ false |
|
1230 |
||
1231 |
"Modified: / 9.7.1998 / 10:19:27 / cg" |
|
1232 |
! |
|
1233 |
||
8979
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1234 |
isFinite |
10326 | 1235 |
"return true, if the receiver is finite |
9400 | 1236 |
i.e. it can be represented as a rational number." |
1237 |
||
8979
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1238 |
^ true |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1239 |
! |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1240 |
|
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1241 |
isInfinite |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1242 |
^ false |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1243 |
! |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1244 |
|
7440 | 1245 |
isNegativeInfinity |
1246 |
^ self negative and:[self isInfinite] |
|
1247 |
! |
|
1248 |
||
1249 |
isNegativeZero |
|
1250 |
"return false - must be redefined by subclasses which can represent a negative zero |
|
1251 |
(i.e. limitedPrecisionReal classes)" |
|
1252 |
||
1253 |
^ false |
|
1254 |
! |
|
1255 |
||
1256 |
isPositiveInfinity |
|
1257 |
^ self positive and:[self isInfinite] |
|
1258 |
! |
|
1259 |
||
6156 | 1260 |
isReal |
1261 |
"return true, if the receiver is some kind of real number (as opposed to a complex); |
|
1262 |
false is returned here - the method is only redefined in Number (and Complex)." |
|
1263 |
||
1264 |
^ false |
|
1265 |
||
1266 |
"Modified: / 6.11.2001 / 13:20:20 / cg" |
|
1267 |
"Created: / 6.11.2001 / 13:26:29 / cg" |
|
1268 |
! |
|
1269 |
||
7440 | 1270 |
isZero |
1271 |
"return false - must be redefined by subclasses which can represent a negative zero |
|
1272 |
(i.e. limitedPrecisionReal classes)" |
|
701 | 1273 |
|
7440 | 1274 |
^ self = self class zero |
1275 |
! |
|
1276 |
||
1277 |
negative |
|
1278 |
"return true if the receiver is less than zero" |
|
1279 |
||
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
1280 |
^ self < self class zero |
701 | 1281 |
! |
1282 |
||
1283 |
numerator |
|
1284 |
"return the numerator of the receiver." |
|
1285 |
||
1286 |
^ self |
|
1287 |
! |
|
1288 |
||
1289 |
odd |
|
1290 |
"return true if the receiver is not divisible by 2" |
|
1291 |
||
1292 |
^ self even not |
|
1293 |
! |
|
1294 |
||
1295 |
positive |
|
1296 |
"return true, if the receiver is >= 0" |
|
1297 |
||
1298 |
^ self negative not |
|
1299 |
! |
|
1300 |
||
1301 |
sign |
|
7471 | 1302 |
"return the sign of the receiver (-1, 0 or 1)" |
701 | 1303 |
|
7440 | 1304 |
|zero| |
1305 |
||
1306 |
zero := self class zero. |
|
1307 |
(self < zero) ifTrue:[^ -1]. |
|
1308 |
(zero < self) ifTrue:[^ 1]. |
|
701 | 1309 |
^ 0 |
1310 |
! |
|
1311 |
||
1312 |
strictlyPositive |
|
1313 |
"return true, if the receiver is > 0" |
|
1314 |
||
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
1315 |
^ self class zero < self |
1 | 1316 |
! ! |
1317 |
||
701 | 1318 |
!ArithmeticValue methodsFor:'truncation & rounding'! |
1 | 1319 |
|
701 | 1320 |
ceiling |
1321 |
"return the integer nearest the receiver towards positive infinity." |
|
1 | 1322 |
|
701 | 1323 |
|anInteger| |
1 | 1324 |
|
701 | 1325 |
anInteger := self // 1. "truncates towards negative infinity" |
1326 |
anInteger = self ifTrue:[^ anInteger]. |
|
1327 |
^ anInteger + 1 |
|
1 | 1328 |
! |
1329 |
||
701 | 1330 |
floor |
1331 |
"return the receiver truncated towards negative infinity" |
|
1 | 1332 |
|
701 | 1333 |
^ self // 1 |
1 | 1334 |
! |
1335 |
||
701 | 1336 |
roundTo:aNumber |
1337 |
"return the receiver rounded to multiples of aNumber" |
|
1 | 1338 |
|
701 | 1339 |
^ (self / aNumber) rounded * aNumber |
8449 | 1340 |
|
1341 |
" |
|
10326 | 1342 |
0 roundTo:4 |
8449 | 1343 |
1 roundTo:4 |
1344 |
2 roundTo:4 |
|
1345 |
3 roundTo:4 |
|
1346 |
4 roundTo:4 |
|
1347 |
5 roundTo:4 |
|
1348 |
6 roundTo:4 |
|
1349 |
7 roundTo:4 |
|
14497 | 1350 |
|
1351 |
7.123 roundTo:0.1 |
|
1352 |
7.523 roundTo:0.1 |
|
1353 |
7.583 roundTo:0.1 |
|
1354 |
7.623 roundTo:0.1 |
|
1355 |
7.623 roundTo:0.01 |
|
1356 |
7.628 roundTo:0.01 |
|
8449 | 1357 |
" |
1358 |
! |
|
1359 |
||
1360 |
roundUpTo:aNumber |
|
1361 |
"return the receiver rounded up to the next multiple of aNumber" |
|
1362 |
||
1363 |
^ (self / aNumber) ceiling * aNumber |
|
1364 |
||
1365 |
" |
|
10326 | 1366 |
0 roundUpTo:4 |
1367 |
1 roundUpTo:4 |
|
1368 |
2 roundUpTo:4 |
|
1369 |
3 roundUpTo:4 |
|
1370 |
4 roundUpTo:4 |
|
1371 |
5 roundUpTo:4 |
|
1372 |
6 roundUpTo:4 |
|
1373 |
7 roundUpTo:4 |
|
1374 |
8 roundUpTo:4 |
|
10911 | 1375 |
(3@4) roundUpTo:8 |
1376 |
(3@4) roundUpTo:(5 @ 4) |
|
1377 |
(3@3) roundUpTo:(5 @ 4) |
|
8449 | 1378 |
" |
701 | 1379 |
! |
1380 |
||
1381 |
rounded |
|
1382 |
"return the integer nearest the receiver" |
|
1383 |
||
1881 | 1384 |
self negative ifTrue:[ |
10326 | 1385 |
^ (self - 0.5) ceiling |
1881 | 1386 |
]. |
701 | 1387 |
^ (self + 0.5) floor |
1881 | 1388 |
|
1389 |
"Modified: 5.11.1996 / 11:31:59 / cg" |
|
1 | 1390 |
! |
1391 |
||
701 | 1392 |
truncateTo:aNumber |
1393 |
"return the receiver truncated to multiples of aNumber" |
|
1 | 1394 |
|
701 | 1395 |
^ ((self / aNumber) floor * aNumber) asInteger |
1 | 1396 |
! |
1397 |
||
701 | 1398 |
truncated |
1399 |
"return the receiver truncated towards zero" |
|
1 | 1400 |
|
701 | 1401 |
self negative ifTrue:[ |
1402 |
^ self ceiling |
|
1403 |
]. |
|
1404 |
^ self floor |
|
1405 |
! ! |
|
1 | 1406 |
|
701 | 1407 |
!ArithmeticValue class methodsFor:'documentation'! |
1408 |
||
1409 |
version |
|
17038 | 1410 |
^ '$Header: /cvs/stx/stx/libbasic/ArithmeticValue.st,v 1.95 2014-11-11 14:26:04 cg Exp $' |
12576 | 1411 |
! |
1412 |
||
1413 |
version_CVS |
|
17038 | 1414 |
^ '$Header: /cvs/stx/stx/libbasic/ArithmeticValue.st,v 1.95 2014-11-11 14:26:04 cg Exp $' |
1 | 1415 |
! ! |
6858 | 1416 |
|
14747 | 1417 |
|
701 | 1418 |
ArithmeticValue initialize! |