author | Stefan Vogel <sv@exept.de> |
Wed, 03 Jan 2018 21:20:49 +0100 | |
changeset 22406 | b94edbfd6aa7 |
parent 22358 | 101b2548022b |
child 22723 | 5e9e074b1c0f |
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 |
||
17336 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1 | 16 |
Magnitude subclass:#ArithmeticValue |
1172 | 17 |
instanceVariableNames:'' |
21797 | 18 |
classVariableNames:'ArithmeticSignal ConversionErrorSignal DivisionByZeroSignal |
19 |
DomainErrorSignal OverflowSignal UnderflowSignal UnorderedSignal' |
|
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:] |
21967 | 50 |
ArithmeticSignal <Error> parent of all arithmetic signals |
51 |
(never raised itself) |
|
52 |
New: now a reference to ArithmeticError |
|
82 | 53 |
|
21967 | 54 |
DomainErrorSignal <Error> raised upon float errors |
55 |
(for example range in trigonometric) |
|
56 |
New: now a reference to DomainError; no longer a classVar |
|
1283 | 57 |
|
21967 | 58 |
DivisionByZeroSignal <Error> raised when division by 0 is attempted |
59 |
New: now a reference to ZeroDivide |
|
155 | 60 |
|
21967 | 61 |
OverflowSignal <Error> raised on overflow/underflow conditions |
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:] |
21967 | 69 |
Claus Gittinger |
1295 | 70 |
|
1283 | 71 |
[See also:] |
21967 | 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 |
||
84 |
divisionByZeroSignal |
|
21865 | 85 |
"return the signal which is raised on division by zero. |
86 |
No longer used - we now have the class based ZeroDivide exception. |
|
87 |
This method is kept for backward compatibility." |
|
701 | 88 |
|
7403 | 89 |
^ ZeroDivide |
21865 | 90 |
|
91 |
"Modified (comment): / 20-06-2017 / 17:53:33 / cg" |
|
701 | 92 |
! |
1 | 93 |
|
94 |
domainErrorSignal |
|
22289 | 95 |
"return the signal which is raised on some math errors, |
96 |
when the argument is outside the legal domain. |
|
97 |
(such as arcSin of 2 etc.)" |
|
1 | 98 |
|
7403 | 99 |
^ DomainError |
22289 | 100 |
|
101 |
" |
|
102 |
2 arcSin |
|
103 |
0 log - raises infiniteResult, which is a child of DomainError |
|
104 |
" |
|
105 |
||
106 |
"Modified (comment): / 22-09-2017 / 09:50:41 / cg" |
|
7403 | 107 |
! |
108 |
||
109 |
imaginaryResultSignal |
|
110 |
"return the signal which is raised when an imaginary result would be created |
|
22289 | 111 |
(such as when taking the sqrt of a negative number). |
112 |
This error can be handled by wrapping the computation inside a trapImaginary |
|
113 |
block; then, a complex result is generated." |
|
7403 | 114 |
|
115 |
^ ImaginaryResultError |
|
22289 | 116 |
|
117 |
" |
|
118 |
-2 sqrt |
|
119 |
||
120 |
Complex trapImaginary:[ -2 sqrt ] |
|
121 |
" |
|
122 |
||
123 |
"Modified (comment): / 22-09-2017 / 09:53:58 / cg" |
|
1 | 124 |
! |
125 |
||
21967 | 126 |
infiniteResultSignal |
22289 | 127 |
"return the signal which is raised when an infinite result would be created. |
128 |
This is a subclass of DomainError. |
|
21967 | 129 |
(such as when taking the logarithm of zero)" |
130 |
||
131 |
^ InfiniteResultError |
|
132 |
||
133 |
"Created: / 03-07-2017 / 15:46:02 / cg" |
|
22289 | 134 |
"Modified (comment): / 22-09-2017 / 09:49:15 / cg" |
21967 | 135 |
! |
136 |
||
7440 | 137 |
operationNotPossibleSignal |
138 |
^ Error |
|
139 |
! |
|
140 |
||
1 | 141 |
overflowSignal |
22289 | 142 |
"return the signal which is raised on overflow conditions (in floats). |
143 |
Attention: currently not raised on all architectures; some return NaN" |
|
1 | 144 |
|
7403 | 145 |
^ OverflowError |
22289 | 146 |
|
147 |
" |
|
148 |
10e300 * 10e30 |
|
149 |
||
150 |
10q300 * 10e30 |
|
151 |
" |
|
152 |
||
153 |
"Modified (comment): / 22-09-2017 / 09:58:49 / cg" |
|
1 | 154 |
! |
155 |
||
5955 | 156 |
rangeErrorSignal |
157 |
"return the parent of the overflow/underflow signals" |
|
158 |
||
159 |
^ RangeError |
|
160 |
! |
|
161 |
||
7440 | 162 |
undefinedResultSignal |
163 |
^ Error |
|
164 |
! |
|
165 |
||
1 | 166 |
underflowSignal |
22289 | 167 |
"return the signal which is raised on underflow conditions (in floats) |
168 |
Attention: currently not raised on all architectures; some return zero" |
|
1 | 169 |
|
7403 | 170 |
^ UnderflowError |
22289 | 171 |
|
172 |
" |
|
173 |
10e-300 / 10e30 |
|
174 |
" |
|
175 |
||
176 |
"Modified (comment): / 22-09-2017 / 09:58:57 / cg" |
|
1 | 177 |
! |
178 |
||
345 | 179 |
unorderedSignal |
10326 | 180 |
"return the signal which is raised when numbers are compared, |
345 | 181 |
for which no ordering is defined (for example: complex numbers)" |
182 |
||
7403 | 183 |
^ UnorderedNumbersError |
22289 | 184 |
|
185 |
" |
|
186 |
(0 + 3i) > (0 + 4i) |
|
187 |
3i > 4i |
|
188 |
" |
|
189 |
||
190 |
"Modified (comment): / 22-09-2017 / 09:57:28 / cg" |
|
1 | 191 |
! ! |
192 |
||
7440 | 193 |
!ArithmeticValue class methodsFor:'class initialization'! |
194 |
||
195 |
initialize |
|
196 |
"setup the signals" |
|
197 |
||
20094 | 198 |
"/ Notice that we now use class based exceptions; |
7440 | 199 |
"/ however, for backward compatibility, the class variables |
200 |
"/ referring to those are kept for a while, for subclass compatibility. |
|
201 |
"/ Please convert your code to access those via the Signal-constants class |
|
20090 | 202 |
"/ messages (accessors) instead of referring to the class variables. |
7440 | 203 |
|
204 |
ArithmeticSignal := ArithmeticError. |
|
21967 | 205 |
"/ DomainErrorSignal := DomainError. |
7440 | 206 |
DivisionByZeroSignal := ZeroDivide. |
207 |
UnorderedSignal := UnorderedNumbersError. |
|
208 |
ConversionErrorSignal := ConversionError. |
|
209 |
OverflowSignal := OverflowError. |
|
210 |
UnderflowSignal := UnderflowError. |
|
21967 | 211 |
|
212 |
"Modified: / 03-07-2017 / 14:05:10 / cg" |
|
7440 | 213 |
! ! |
214 |
||
18835 | 215 |
!ArithmeticValue class methodsFor:'coercing & converting'! |
216 |
||
217 |
coerce:aNumber |
|
218 |
"convert the argument aNumber into an instance of the receiver (class) and return it." |
|
219 |
||
220 |
^ self subclassResponsibility |
|
221 |
! ! |
|
222 |
||
7440 | 223 |
!ArithmeticValue class methodsFor:'constants'! |
224 |
||
225 |
NaN |
|
8850 | 226 |
"return the constant NaN (not a Number)." |
227 |
||
7440 | 228 |
^ NotANumber new |
229 |
! |
|
230 |
||
231 |
infinity |
|
21862 | 232 |
"return something which represents positive infinity (for my instances)" |
8850 | 233 |
|
7440 | 234 |
^ Infinity positive |
21862 | 235 |
|
236 |
"Modified (comment): / 20-06-2017 / 13:45:23 / cg" |
|
7440 | 237 |
! |
238 |
||
7538
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
239 |
nan |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
240 |
"VW compatibility" |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
241 |
|
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
242 |
^ self NaN |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
243 |
! |
dd0131e05cf0
Define #nan for VW-compatibility
Stefan Vogel <sv@exept.de>
parents:
7471
diff
changeset
|
244 |
|
7440 | 245 |
negativeInfinity |
8850 | 246 |
"return something which represents negative infinity (for my instances)" |
247 |
||
7440 | 248 |
^ Infinity negative |
249 |
! |
|
250 |
||
7726 | 251 |
unity |
11467 | 252 |
"return something which represents the unity element (for my instances). |
253 |
That is the neutral element for multiplication." |
|
8850 | 254 |
|
21827 | 255 |
^ self subclassResponsibility |
256 |
||
257 |
"Modified: / 16-06-2017 / 11:04:36 / cg" |
|
7726 | 258 |
! |
259 |
||
7440 | 260 |
zero |
11467 | 261 |
"return something which represents the zero element (for my instances). |
262 |
That is the neutral element for addition." |
|
8850 | 263 |
|
21827 | 264 |
^ self subclassResponsibility |
265 |
||
266 |
"Modified: / 16-06-2017 / 11:04:33 / cg" |
|
7440 | 267 |
! ! |
268 |
||
17223 | 269 |
!ArithmeticValue class methodsFor:'error reporting'! |
270 |
||
271 |
raise:aSignalSymbolOrErrorClass receiver:someNumber selector:sel arguments:argArray errorString:text |
|
272 |
"ST-80 compatible signal raising. Provided for PD numeric classes. |
|
273 |
aSignalSymbolOrErrorClass is either an Error-subclass, or |
|
274 |
the selector which is sent to myself, to retrieve the Exception class / Signal." |
|
275 |
||
276 |
<context: #return> |
|
277 |
||
278 |
|msg signalOrException| |
|
279 |
||
280 |
msg := MessageSend |
|
281 |
receiver:someNumber |
|
282 |
selector:sel |
|
283 |
arguments:argArray. |
|
284 |
||
285 |
aSignalSymbolOrErrorClass isSymbol ifTrue:[ |
|
286 |
signalOrException := self perform:aSignalSymbolOrErrorClass. |
|
287 |
] ifFalse:[ |
|
288 |
signalOrException := aSignalSymbolOrErrorClass. "/ assume its an Error-Subclass |
|
289 |
]. |
|
290 |
||
291 |
^ signalOrException |
|
292 |
raiseRequestWith:msg |
|
293 |
errorString:text |
|
294 |
in:thisContext sender |
|
295 |
||
296 |
" |
|
297 |
Number |
|
298 |
raise:#domainErrorSignal |
|
299 |
receiver:1.0 |
|
300 |
selector:#foo |
|
301 |
errorString:'foo bar test' |
|
302 |
" |
|
303 |
||
304 |
"Modified: / 16.11.2001 / 14:12:09 / cg" |
|
305 |
! ! |
|
306 |
||
21967 | 307 |
!ArithmeticValue class methodsFor:'exception handling'! |
308 |
||
309 |
trapImaginary:aBlock |
|
310 |
"evaluate aBlock; |
|
22289 | 311 |
if any ImaginaryResult occurs inside, which would return an imaginary result, |
21967 | 312 |
(eg. square root of negative number), |
313 |
convert the result to a complex number and proceed. |
|
314 |
||
315 |
This allows for regular (failing) code to transparently convert to complex." |
|
316 |
||
317 |
^ ImaginaryResultError |
|
318 |
handle: [:ex | |
|
319 |
|msgSend selector rcvr| |
|
320 |
||
321 |
msgSend := ex parameter. |
|
322 |
selector := msgSend selector. |
|
323 |
rcvr := msgSend receiver. |
|
22289 | 324 |
|
21967 | 325 |
(selector == #sqrt or: [selector == #sqrtTruncated]) ifTrue: [ |
326 |
(rcvr isNumber and:[rcvr isReal]) ifTrue:[ |
|
327 |
ex proceedWith:(rcvr abs perform:selector) i |
|
328 |
] ifFalse:[ |
|
329 |
msgSend receiver: rcvr asComplex. |
|
330 |
ex proceedWith: msgSend value |
|
331 |
]. |
|
332 |
] ifFalse: [ |
|
333 |
(selector == #integerSqrt) ifTrue: [ |
|
334 |
(rcvr isInteger) ifTrue:[ |
|
335 |
ex proceedWith:(rcvr abs integerSqrt) i |
|
336 |
] ifFalse:[ |
|
337 |
ex proceedWith:(rcvr abs asComplex sqrt floor) i |
|
338 |
]. |
|
22289 | 339 |
] ifFalse: [ |
340 |
(selector == #nthRoot:) ifTrue: [ |
|
341 |
ex proceedWith:(rcvr abs nthRoot:msgSend argument) i |
|
342 |
] |
|
21967 | 343 |
] |
344 |
]. |
|
22289 | 345 |
|
21967 | 346 |
ex reject |
347 |
] do: |
|
348 |
aBlock |
|
349 |
||
350 |
" |
|
351 |
this raises an error: |
|
352 |
-2 sqrt |
|
353 |
||
354 |
this returns an imaginary result: |
|
355 |
Complex trapImaginary: [-2 sqrt] |
|
356 |
||
357 |
of course, this one as well: |
|
358 |
-2 asComplex sqrt |
|
359 |
" |
|
360 |
||
361 |
"failing code: |
|
362 |
|a| |
|
363 |
||
364 |
a := -2. |
|
365 |
(a sqrt + 5) * 17. |
|
366 |
" |
|
367 |
"complex code: |
|
368 |
|a| |
|
369 |
||
370 |
Complex trapImaginary:[ |
|
371 |
a := -2. |
|
372 |
(a sqrt + 5) * 2. |
|
373 |
] |
|
374 |
" |
|
375 |
||
22289 | 376 |
"Modified: / 22-09-2017 / 10:18:14 / cg" |
21967 | 377 |
! |
378 |
||
379 |
trapInfinity:aBlock |
|
380 |
"evaluate aBlock; |
|
381 |
if any DomainError occurs inside, which would return an infinite result, |
|
382 |
(eg. ln of zero), |
|
383 |
convert the result to infinity and proceed. |
|
384 |
||
385 |
This allows for regular (failing) code to transparently convert to infinity and behave |
|
386 |
similar to other systems which do that." |
|
387 |
||
388 |
^ InfiniteResultError |
|
389 |
handle: [:ex | |
|
390 |
|msgSend selector rcvr| |
|
391 |
||
392 |
msgSend := ex parameter. |
|
393 |
selector := msgSend selector. |
|
394 |
rcvr := msgSend receiver. |
|
395 |
((selector == #ln) |
|
396 |
or:[selector == #log10 |
|
397 |
or:[selector == #integerLog10]]) ifTrue: [ |
|
398 |
(rcvr isNumber) ifTrue:[ |
|
399 |
ex proceedWith:(rcvr class negativeInfinity ) |
|
400 |
]. |
|
401 |
]. |
|
402 |
ex reject |
|
403 |
] do: |
|
404 |
aBlock |
|
405 |
||
406 |
" |
|
407 |
this raises an error: |
|
408 |
0 ln |
|
409 |
||
410 |
this returns an imaginary result: |
|
411 |
Number trapInfinity: [0 ln] |
|
412 |
" |
|
413 |
||
414 |
"failing code: |
|
415 |
|a| |
|
416 |
||
417 |
a := 0. |
|
418 |
a ln isFinite. |
|
419 |
" |
|
420 |
"fixed code: |
|
421 |
|a| |
|
422 |
||
423 |
Number trapInfinity:[ |
|
424 |
a := 0. |
|
425 |
a ln isFinite. |
|
426 |
] |
|
427 |
" |
|
428 |
||
429 |
"Created: / 03-07-2017 / 14:25:57 / cg" |
|
430 |
"Modified: / 03-07-2017 / 15:53:40 / cg" |
|
21991 | 431 |
! |
432 |
||
433 |
trapOverflow:aBlock |
|
434 |
"evaluate aBlock; |
|
435 |
if an Overflow occurs inside, return INF and proceed." |
|
436 |
||
437 |
^ OverflowError |
|
438 |
handle: [:ex | |
|
439 |
|msgSend rcvr| |
|
440 |
||
441 |
msgSend := ex parameter. |
|
442 |
rcvr := msgSend receiver. |
|
443 |
ex proceedWith:(rcvr class infinity) |
|
444 |
] do: |
|
445 |
aBlock |
|
446 |
||
447 |
" |
|
448 |
this raises an error: |
|
449 |
1000 factorial asShortFloat |
|
450 |
||
451 |
this returns an imaginary result: |
|
452 |
Number trapOverflow: [ 1000 factorial asShortFloat] |
|
453 |
" |
|
454 |
||
455 |
"Created: / 07-07-2017 / 13:08:06 / cg" |
|
456 |
! |
|
457 |
||
458 |
trapUnderflow:aBlock |
|
459 |
"evaluate aBlock; |
|
460 |
if an Underflow occurs inside, return zero and proceed." |
|
461 |
||
462 |
^ UnderflowError |
|
463 |
handle: [:ex | |
|
464 |
|msgSend rcvr| |
|
465 |
||
466 |
msgSend := ex parameter. |
|
467 |
rcvr := msgSend receiver. |
|
468 |
ex proceedWith:(rcvr class zero) |
|
469 |
] do: |
|
470 |
aBlock |
|
471 |
||
472 |
" |
|
473 |
this raises an error: |
|
474 |
1.0 / (1000 factorial) |
|
475 |
||
476 |
this returns an imaginary result: |
|
477 |
Number trapUnderflow: 1.0 / (1000 factorial)] |
|
478 |
" |
|
479 |
||
480 |
"Created: / 05-07-2017 / 13:16:43 / cg" |
|
21967 | 481 |
! ! |
482 |
||
8892 | 483 |
!ArithmeticValue class methodsFor:'queries'! |
484 |
||
485 |
isAbstract |
|
11227 | 486 |
"Return if this class is an abstract class. |
487 |
True is returned for ArithmeticValue here; false for subclasses. |
|
19454 | 488 |
Abstract subclasses must redefine this again." |
11227 | 489 |
|
8892 | 490 |
^ self == ArithmeticValue |
491 |
! ! |
|
492 |
||
22358 | 493 |
|
701 | 494 |
!ArithmeticValue methodsFor:'arithmetic'! |
495 |
||
496 |
* something |
|
11735 | 497 |
"return the product of the receiver and the argument." |
701 | 498 |
|
499 |
^ self subclassResponsibility |
|
500 |
! |
|
501 |
||
502 |
+ something |
|
503 |
"return the sum of the receiver and the argument" |
|
504 |
||
505 |
^ self subclassResponsibility |
|
506 |
! |
|
507 |
||
508 |
- something |
|
509 |
"return the difference of the receiver and the argument" |
|
510 |
||
511 |
^ self subclassResponsibility |
|
512 |
! |
|
513 |
||
514 |
/ something |
|
515 |
"return the quotient of the receiver and the argument" |
|
1 | 516 |
|
701 | 517 |
^ self subclassResponsibility |
518 |
! |
|
519 |
||
22358 | 520 |
// aNumber |
1881 | 521 |
"return the integer quotient of dividing the receiver by aNumber with |
20337 | 522 |
truncation towards negative infinity. |
22358 | 523 |
|
20338 | 524 |
Please be aware of the effect of truncation on negative receivers, |
20337 | 525 |
and understand the difference between '//' vs. 'quo:' |
526 |
and the corresponding '\\' vs. 'rem:'" |
|
701 | 527 |
|
22358 | 528 |
"/ Note: '^ (self / aNumber) floor' may lead to infinite recursion |
1172 | 529 |
|
530 |
^ self subclassResponsibility |
|
531 |
||
22358 | 532 |
"Modified: / 05-11-1996 / 11:46:27 / cg" |
533 |
"Modified: / 12-02-1998 / 16:31:36 / stefan" |
|
534 |
"Modified (comment): / 16-11-2017 / 11:26:13 / mawalch" |
|
701 | 535 |
! |
536 |
||
537 |
\\ something |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
538 |
"return the receiver modulo something. |
17038 | 539 |
The remainder has the same sign as the argument, something. |
6067 | 540 |
The following is always true: |
17038 | 541 |
(receiver // something) * something + (receiver \\ something) = receiver |
20338 | 542 |
|
543 |
Please be aware of the effect of truncation on negative receivers, |
|
544 |
and understand the difference between '//' vs. 'quo:' |
|
545 |
and the corresponding '\\' vs. 'rem:'." |
|
1 | 546 |
|
701 | 547 |
^ self - ((self // something) * something) |
3134 | 548 |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
549 |
" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
550 |
0.9 \\ 0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
551 |
0.9 \\ -0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
552 |
-0.9 \\ 0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
553 |
-0.9 \\ -0.4 |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
554 |
" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
555 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
556 |
"Modified: / 12.2.1998 / 19:14:37 / stefan" |
701 | 557 |
! |
558 |
||
559 |
abs |
|
560 |
"return the absolute value of the receiver" |
|
561 |
||
562 |
(self negative) ifTrue:[^ self negated]. |
|
563 |
^ self |
|
564 |
! |
|
565 |
||
10326 | 566 |
dist:arg |
20696 | 567 |
"return the distance between the arg and the receiver." |
8757 | 568 |
|
569 |
^ (arg - self) abs |
|
570 |
||
571 |
" |
|
10326 | 572 |
(1%1) dist:(0%0) |
573 |
(1@1) dist:(0@0) |
|
574 |
(1) dist:(0) |
|
8757 | 575 |
" |
576 |
! |
|
577 |
||
16121 | 578 |
modulusOf:aNumber |
20353 | 579 |
"return aNumber modulo the receiver. |
16121 | 580 |
The remainder has the same sign as something. |
581 |
Defined for protocol compatibility with ModuloNumber." |
|
582 |
||
583 |
^ aNumber \\ self |
|
584 |
! |
|
585 |
||
701 | 586 |
negated |
587 |
"return the receiver negated" |
|
588 |
||
589 |
^ self class zero - self |
|
1 | 590 |
! |
591 |
||
701 | 592 |
quo:something |
593 |
"Return the integer quotient of dividing the receiver by the argument |
|
6067 | 594 |
with truncation towards zero. |
20338 | 595 |
|
596 |
Please be aware of the effect of truncation on negative receivers, |
|
597 |
and understand the difference between '//' vs. 'quo:' |
|
598 |
and the corresponding '\\' vs. 'rem:'. |
|
599 |
||
6067 | 600 |
The following is always true: |
20337 | 601 |
(receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver |
602 |
For positive results, this is the same as #//, |
|
603 |
for negative results, the remainder is ignored. |
|
604 |
I.e.: '9 // 4 = 2' and '-9 // 4 = -3' |
|
605 |
in contrast: '9 quo: 4 = 2' and '-9 quo: 4 = -2'" |
|
701 | 606 |
|
607 |
^ (self / something) truncated |
|
608 |
! |
|
609 |
||
610 |
reciprocal |
|
18835 | 611 |
"return the receiver's reciprocal" |
701 | 612 |
|
613 |
^ self class unity / self |
|
614 |
! |
|
1 | 615 |
|
701 | 616 |
rem:something |
617 |
"Return the integer remainder of dividing the receiver by the argument |
|
618 |
with truncation towards zero. |
|
6067 | 619 |
The remainder has the same sign as the receiver. |
620 |
The following is always true: |
|
20338 | 621 |
(receiver quo: something) * something + (receiver rem: something) = receiver |
622 |
||
623 |
Please be aware of the effect of truncation on negative receivers, |
|
624 |
and understand the difference between '//' vs. 'quo:' |
|
625 |
and the corresponding '\\' vs. 'rem:'." |
|
701 | 626 |
|
627 |
^ self - ((self quo:something) * something) |
|
7440 | 628 |
! |
629 |
||
630 |
uncheckedDivide:aNumber |
|
631 |
"return the quotient of the receiver and the argument, aNumber. |
|
7471 | 632 |
Do not check for divide by zero (return NaN or Infinity). |
7440 | 633 |
This operation is provided for emulators of other languages/semantics, |
634 |
where no exception is raised for these results (i.e. Java). |
|
18835 | 635 |
It is only defined if the argument's type is the same as the receiver's." |
7440 | 636 |
|
637 |
aNumber isZero ifTrue:[ |
|
21831 | 638 |
"/ protect against NaN and negativeInfinity trying to use me... |
639 |
thisContext isRecursive ifTrue:[ self subclassResponsibility]. |
|
640 |
||
18835 | 641 |
self isZero ifTrue:[^ self class NaN]. |
642 |
self negative ifTrue:[^ self class negativeInfinity]. |
|
643 |
^ self class infinity. |
|
7440 | 644 |
]. |
645 |
^ self / aNumber |
|
21831 | 646 |
|
647 |
"Modified (format): / 18-06-2017 / 23:43:13 / cg" |
|
701 | 648 |
! ! |
649 |
||
12578 | 650 |
!ArithmeticValue methodsFor:'arithmetic destructive'! |
651 |
||
652 |
*= aNumber |
|
653 |
"Return the product of self multiplied by aNumber. |
|
654 |
The receiver MAY, but NEED NOT be changed to contain the product. |
|
655 |
So this method must be used as: 'a := a *= 5'. |
|
656 |
This method can be redefined for constructed datatypes to do optimisations" |
|
657 |
||
658 |
^ self * aNumber |
|
659 |
||
660 |
"Created: / 28.4.1999 / 11:46:11 / stefan" |
|
661 |
"Modified: / 28.4.1999 / 11:53:28 / stefan" |
|
662 |
! |
|
663 |
||
664 |
+= aNumber |
|
665 |
"Return the sum of self and aNumber. |
|
666 |
The receiver MAY, but NEED NOT be changed to contain the sum. |
|
667 |
So this method must be used as: 'a := a += 5'. |
|
668 |
This method can be redefined for constructed datatypes to do optimisations" |
|
669 |
||
670 |
^ self + aNumber |
|
671 |
||
672 |
"Created: / 28.4.1999 / 10:13:41 / stefan" |
|
673 |
"Modified: / 28.4.1999 / 11:54:11 / stefan" |
|
674 |
! |
|
675 |
||
676 |
-= aNumber |
|
677 |
"Return the difference of self and aNumber. |
|
678 |
The receiver MAY, but NEED NOT be changed to contain the difference. |
|
679 |
So this method must be used as: 'a := a -= 5'. |
|
680 |
This method can be redefined for constructed datatypes to do optimisations" |
|
681 |
||
682 |
^ self - aNumber |
|
683 |
||
684 |
"Created: / 28.4.1999 / 10:13:58 / stefan" |
|
685 |
"Modified: / 28.4.1999 / 11:54:37 / stefan" |
|
686 |
! |
|
687 |
||
688 |
/= aNumber |
|
689 |
"Return the quotient of self and aNumber. |
|
690 |
The receiver MAY, but NEED NOT be changed to contain the quotient. |
|
691 |
So this method must be used as: 'a := a /= 5'. |
|
692 |
This method can be redefined for constructed datatypes to do optimisations" |
|
693 |
||
694 |
^ self / aNumber |
|
695 |
||
696 |
"Created: / 28.4.1999 / 11:46:22 / stefan" |
|
697 |
"Modified: / 28.4.1999 / 11:55:06 / stefan" |
|
698 |
! |
|
699 |
||
700 |
div2 |
|
701 |
"Return the quotient of self divided by 2. |
|
702 |
The receiver MAY, but NEED NOT be changed to contain the result. |
|
703 |
So this method must be used as: 'a := a div2. |
|
704 |
This method can be redefined for constructed datatypes to do optimisations" |
|
705 |
||
706 |
^ self // 2 |
|
707 |
||
708 |
"Created: / 28.4.1999 / 10:12:44 / stefan" |
|
709 |
"Modified: / 28.4.1999 / 11:56:09 / stefan" |
|
710 |
! |
|
711 |
||
712 |
mul2 |
|
713 |
"Return the product of self multiplied by 2. |
|
714 |
The receiver MAY, but NEED NOT be changed to contain the result. |
|
15279 | 715 |
So this method must be used as: a := a mul2. |
12578 | 716 |
This method can be redefined for constructed datatypes to do optimisations" |
717 |
||
718 |
^ self * 2 |
|
719 |
||
720 |
"Created: / 28.4.1999 / 10:12:55 / stefan" |
|
721 |
"Modified: / 28.4.1999 / 11:56:38 / stefan" |
|
722 |
! ! |
|
723 |
||
5238 | 724 |
!ArithmeticValue methodsFor:'coercing & converting'! |
1888 | 725 |
|
726 |
coerce:aNumber |
|
18835 | 727 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
1888 | 728 |
|
21816 | 729 |
^ self class coerce:aNumber |
730 |
||
731 |
"Modified: / 15-06-2017 / 10:27:03 / cg" |
|
1888 | 732 |
! |
733 |
||
734 |
generality |
|
18835 | 735 |
"return a number giving the receiver's generality. |
736 |
That number is used to convert one of the arguments in a mixed expression. |
|
1888 | 737 |
The generality has to be defined in subclasses, |
10326 | 738 |
such that gen(a) > gen(b) iff, conversion of b into a's class |
18835 | 739 |
does not cut precision. |
740 |
For example, Integer has 40, Float has 80, meaning that if we convert a Float to an Integer, |
|
741 |
some precision may be lost. |
|
742 |
The generality is used by ArithmeticValue>>retry:coercing:, |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
743 |
which converts the lower-precision number to the higher precision |
18835 | 744 |
number's class, when mixed-type arithmetic is performed." |
10326 | 745 |
|
1888 | 746 |
^ self subclassResponsibility |
747 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
748 |
"Modified: / 5.11.1996 / 15:05:30 / cg" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
749 |
"Modified: / 13.2.1998 / 15:36:01 / stefan" |
1888 | 750 |
! |
751 |
||
752 |
retry:aSymbol coercing:aNumber |
|
753 |
"arithmetic represented by the binary operator, aSymbol, |
|
10326 | 754 |
could not be performed with the receiver and the argument, aNumber, |
755 |
because of the differences in representation. |
|
756 |
Coerce either the receiver or the argument, depending on which has higher |
|
757 |
generality, and try again. |
|
1888 | 758 |
If the operation is compare for same value (=), return false if |
10326 | 759 |
the argument is not a Number. |
1888 | 760 |
If the generalities are the same, create an error message, since this |
761 |
means that a subclass has not been fully implemented." |
|
762 |
||
763 |
|hasGenerality myGenerality otherGenerality| |
|
764 |
||
765 |
hasGenerality := aNumber respondsTo:#generality. |
|
766 |
hasGenerality ifFalse:[ |
|
20060 | 767 |
(aSymbol == #=) ifTrue:[ |
768 |
^ false |
|
769 |
]. |
|
770 |
(aSymbol == #~=) ifTrue:[ |
|
771 |
^ true |
|
772 |
]. |
|
773 |
^ self error:'retry:coercing: argument is not a number'. |
|
1888 | 774 |
]. |
775 |
||
776 |
myGenerality := self generality. |
|
777 |
otherGenerality := aNumber generality. |
|
778 |
(myGenerality > otherGenerality) ifTrue:[ |
|
20060 | 779 |
^ self perform:aSymbol with:(self coerce:aNumber) |
1888 | 780 |
]. |
781 |
(myGenerality < otherGenerality) ifTrue:[ |
|
20060 | 782 |
^ (aNumber coerce:self) perform:aSymbol with:aNumber |
1888 | 783 |
]. |
20060 | 784 |
"/ self error:'retry:coercing: oops - same generality; retry should not happen' |
785 |
"same generality may happen with Lazy and LazyValue" |
|
786 |
^ self perform:aSymbol with:aNumber |
|
787 |
||
788 |
" |
|
789 |
([3] lazyValue) * ([2] lazyValue) |
|
790 |
" |
|
1888 | 791 |
|
792 |
"Modified: 5.11.1996 / 15:03:38 / cg" |
|
16657 | 793 |
! |
794 |
||
795 |
retry:aSymbol coercing:aNumber with:anArgument |
|
796 |
"arithmetic represented by the binary operator, aSymbol, |
|
797 |
could not be performed with the receiver and the argument, aNumber, |
|
798 |
because of the differences in representation. |
|
799 |
Coerce either the receiver or the argument, depending on which has higher |
|
800 |
generality, and try again. |
|
801 |
If the operation is compare for same value (=), return false if |
|
802 |
the argument is not a Number. |
|
803 |
If the generalities are the same, create an error message, since this |
|
804 |
means that a subclass has not been fully implemented." |
|
805 |
||
806 |
|hasGenerality myGenerality otherGenerality| |
|
807 |
||
808 |
hasGenerality := aNumber respondsTo:#generality. |
|
809 |
hasGenerality ifFalse:[ |
|
810 |
^ self error:'retry:coercing: argument is not a number'. |
|
811 |
]. |
|
812 |
||
813 |
myGenerality := self generality. |
|
814 |
otherGenerality := aNumber generality. |
|
815 |
(myGenerality > otherGenerality) ifTrue:[ |
|
816 |
^ self perform:aSymbol with:(self coerce:aNumber) with:anArgument |
|
817 |
]. |
|
818 |
(myGenerality < otherGenerality) ifTrue:[ |
|
819 |
^ (aNumber coerce:self) perform:aSymbol with:aNumber with:anArgument |
|
820 |
]. |
|
20060 | 821 |
"/ self error:'retry:coercing: oops - same generality; retry should not happen' |
822 |
"same generality may happen with Lazy and LazyValue" |
|
823 |
^ self perform:aSymbol with:aNumber with:anArgument |
|
824 |
||
16657 | 825 |
|
826 |
"Modified: 5.11.1996 / 15:03:38 / cg" |
|
1888 | 827 |
! ! |
828 |
||
701 | 829 |
!ArithmeticValue methodsFor:'converting'! |
830 |
||
17222 | 831 |
as32BitIEEEFloatBytesMSB:msb |
832 |
^ self asShortFloat digitBytesMSB:msb |
|
833 |
||
834 |
" |
|
835 |
2 as32BitIEEEFloatBytesMSB:true |
|
836 |
2.0 as32BitIEEEFloatBytesMSB:true |
|
837 |
" |
|
838 |
! |
|
839 |
||
840 |
as64BitIEEEFloatBytesMSB:msb |
|
841 |
^ self asFloat digitBytesMSB:msb |
|
842 |
||
843 |
" |
|
844 |
2 as64BitIEEEFloatBytesMSB:true |
|
845 |
2.0 as64BitIEEEFloatBytesMSB:true |
|
846 |
" |
|
847 |
! |
|
848 |
||
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
849 |
asDouble |
18835 | 850 |
"ST80 compatibility: return a double with receiver's value. |
851 |
Attention: our floats are the identical to ST80's doubles" |
|
3982
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
852 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
853 |
^ self asFloat |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
854 |
|
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
855 |
"Created: / 13.2.1998 / 15:40:14 / stefan" |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
856 |
! |
22fdd79b2d9d
Define asDouble for ST80 compatibility.
Stefan Vogel <sv@exept.de>
parents:
3654
diff
changeset
|
857 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
858 |
asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
859 |
"return the receiver as fixedPoint number. |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
860 |
Q: what should the scale be here ?" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
861 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
862 |
^ self asFraction asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
863 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
864 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
865 |
0.3 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
866 |
0.5 asFixedPoint |
10326 | 867 |
(1/5) asFloat asFixedPoint |
868 |
(1/3) asFloat asFixedPoint |
|
869 |
(2/3) asFloat asFixedPoint |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
870 |
(1/8) asFloat asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
871 |
3.14159 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
872 |
0.0000001 asFraction |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
873 |
0.0000001 asFixedPoint |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
874 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
875 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
876 |
"Modified: / 25.10.1997 / 15:36:54 / cg" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
877 |
! |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
878 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
879 |
asFixedPoint:scale |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
880 |
"return the receiver as fixedPoint number with the given |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
881 |
number of post-decimal-digits." |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
882 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
883 |
^ self asFraction asFixedPoint:scale |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
884 |
|
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
885 |
" |
10326 | 886 |
0.3 asFixedPoint:4 |
887 |
0.3 asFixedPoint:3 |
|
888 |
0.3 asFixedPoint:2 |
|
889 |
0.3 asFixedPoint:1 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
890 |
0.3 asFixedPoint:0 |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
891 |
|
10326 | 892 |
0.5 asFixedPoint:3 |
893 |
(1/5) asFloat asFixedPoint:1 |
|
894 |
(1/8) asFloat asFixedPoint:1 |
|
895 |
1.0 asFixedPoint:2 |
|
896 |
3.14159 asFixedPoint:2 |
|
897 |
3.14159 asFixedPoint:3 |
|
898 |
(3.14159 asFixedPoint:2) asFixedPoint:5 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
899 |
" |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
900 |
! |
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
901 |
|
12980 | 902 |
asFixedPointRoundedToScale |
903 |
"return the receiver as fixedPoint number, rounded to its scale." |
|
904 |
||
905 |
^ self asFixedPoint roundedToScale |
|
906 |
||
907 |
" |
|
908 |
0.3 asFixedPoint |
|
909 |
0.5 asFixedPoint |
|
910 |
(2/3) asFloat asFixedPoint |
|
911 |
(1/8) asFloat asFixedPoint |
|
912 |
3.14159 asFixedPoint |
|
913 |
||
914 |
0.3 asFixedPointRoundedToScale |
|
915 |
0.5 asFixedPointRoundedToScale |
|
916 |
(2/3) asFloat asFixedPointRoundedToScale |
|
917 |
(1/8) asFloat asFixedPointRoundedToScale |
|
918 |
3.14159 asFixedPointRoundedToScale |
|
919 |
" |
|
920 |
||
921 |
"Created: / 02-08-2010 / 13:32:16 / cg" |
|
922 |
! |
|
923 |
||
924 |
asFixedPointRoundedToScale:scale |
|
925 |
"return the receiver as fixedPoint number with the given |
|
926 |
number of post-decimal-digits, rounded to its scale" |
|
927 |
||
928 |
^ (self asFixedPoint:scale) roundedToScale |
|
929 |
||
17336 | 930 |
" |
931 |
3.14159 asFixedPointRoundedToScale:1 |
|
932 |
3.14159 asFixedPointRoundedToScale:2 |
|
933 |
3.14159 asFixedPointRoundedToScale:3 |
|
934 |
3.14159 asFixedPointRoundedToScale:4 |
|
935 |
" |
|
936 |
||
12980 | 937 |
"Created: / 02-08-2010 / 13:33:11 / cg" |
938 |
! |
|
939 |
||
1 | 940 |
asFloat |
941 |
"return a float with same value" |
|
942 |
||
943 |
^ self subclassResponsibility |
|
944 |
! |
|
945 |
||
5986 | 946 |
asFloatD |
947 |
"return a double precision float with same value. |
|
948 |
Added for ANSI compatibility" |
|
949 |
||
950 |
^ self asFloat |
|
951 |
||
952 |
"Created: / 7.9.2001 / 13:36:48 / cg" |
|
953 |
"Modified: / 7.9.2001 / 13:38:10 / cg" |
|
954 |
! |
|
955 |
||
956 |
asFloatE |
|
957 |
"return a single precision float with same value. |
|
958 |
Added for ANSI compatibility" |
|
959 |
||
960 |
^ self asShortFloat |
|
961 |
||
962 |
"Created: / 7.9.2001 / 13:37:06 / cg" |
|
963 |
"Modified: / 7.9.2001 / 13:38:15 / cg" |
|
964 |
! |
|
965 |
||
966 |
asFloatQ |
|
967 |
"return a quad precision float with same value. |
|
21879 | 968 |
Notice that longFloats as returned here may or may not provide more |
21940 | 969 |
precision than a double - depending on the machine's CPU |
970 |
(and usually do not provide quad the number of bits of a float) |
|
5986 | 971 |
Added for ANSI compatibility" |
972 |
||
973 |
^ self asLongFloat |
|
974 |
||
21879 | 975 |
"Created: / 07-09-2001 / 13:38:44 / cg" |
21940 | 976 |
"Modified (comment): / 03-07-2017 / 10:19:13 / cg" |
977 |
! |
|
978 |
||
979 |
asFloatQD |
|
980 |
"return a quad double precision float with same value." |
|
981 |
||
982 |
^ self asQDouble |
|
983 |
||
984 |
"Created: / 03-07-2017 / 10:19:41 / cg" |
|
5986 | 985 |
! |
986 |
||
1 | 987 |
asFraction |
988 |
"return a fraction with same value" |
|
989 |
||
990 |
^ self subclassResponsibility |
|
991 |
! |
|
992 |
||
701 | 993 |
asInteger |
994 |
"return an integer with same value - might truncate" |
|
995 |
||
996 |
^ self truncated |
|
997 |
! |
|
998 |
||
7378 | 999 |
asLimitedPrecisionReal |
1000 |
"return a float of any precision with same value" |
|
1001 |
||
1002 |
^ self asFloat |
|
1003 |
! |
|
1004 |
||
5986 | 1005 |
asLongFloat |
1006 |
"return a longFloat with same value" |
|
1007 |
||
10315 | 1008 |
"WARNING: could loose precision here, if not redefined in concrete classes which |
1009 |
have more than float precision (i.e. LargeIntegers and Fractions)" |
|
7355 | 1010 |
|
10315 | 1011 |
^ self asFloat asLongFloat "/ subclassResponsibility |
5986 | 1012 |
|
1013 |
"Modified: / 17.4.1996 / 12:21:35 / cg" |
|
1014 |
"Created: / 7.9.2001 / 13:39:31 / cg" |
|
1015 |
! |
|
1016 |
||
5987 | 1017 |
asScaledDecimal:scale |
18835 | 1018 |
"return a fixedPoint approximating the receiver's value" |
5987 | 1019 |
|
1020 |
^ self asFixedPoint:scale |
|
1021 |
||
1022 |
" |
|
1023 |
1.234 asScaledDecimal:2 |
|
1024 |
" |
|
1025 |
||
1026 |
"Created: / 7.9.2001 / 13:46:26 / cg" |
|
1027 |
"Modified: / 7.9.2001 / 13:46:44 / cg" |
|
1028 |
! |
|
1029 |
||
1200 | 1030 |
asShortFloat |
1031 |
"return a shortFloat with same value" |
|
1032 |
||
1033 |
^ self asFloat asShortFloat |
|
1034 |
||
1035 |
"Modified: 17.4.1996 / 12:21:35 / cg" |
|
1036 |
! |
|
1037 |
||
701 | 1038 |
degreesToRadians |
21106 | 1039 |
"interpreting the receiver as degrees, return the radians" |
701 | 1040 |
|
1041 |
^ self asFloat degreesToRadians |
|
1042 |
! |
|
1043 |
||
1044 |
radiansToDegrees |
|
21106 | 1045 |
"interpreting the receiver as radians, return the degrees" |
701 | 1046 |
|
1047 |
^ self asFloat radiansToDegrees |
|
1 | 1048 |
! ! |
1049 |
||
701 | 1050 |
!ArithmeticValue methodsFor:'double dispatching'! |
1 | 1051 |
|
7355 | 1052 |
differenceFromComplex:aComplex |
21797 | 1053 |
"aComplex does not know how to subtract the receiver - |
7355 | 1054 |
retry the operation by coercing to higher generality" |
1055 |
||
1056 |
^ aComplex retry:#- coercing:self |
|
21797 | 1057 |
|
1058 |
"Modified (comment): / 12-06-2017 / 20:34:41 / cg" |
|
7355 | 1059 |
! |
1060 |
||
1888 | 1061 |
differenceFromFixedPoint:aFixedPoint |
21797 | 1062 |
"aFixedPoint does not know how to subtract the receiver - |
1888 | 1063 |
retry the operation by coercing to higher generality" |
1064 |
||
1065 |
^ aFixedPoint retry:#- coercing:self |
|
1066 |
||
21797 | 1067 |
"Created: / 05-11-1996 / 14:55:51 / cg" |
1068 |
"Modified (comment): / 12-06-2017 / 20:34:33 / cg" |
|
1888 | 1069 |
! |
1070 |
||
701 | 1071 |
differenceFromFloat:aFloat |
21797 | 1072 |
"aFloat does not know how to subtract the receiver - |
701 | 1073 |
retry the operation by coercing to higher generality" |
1 | 1074 |
|
701 | 1075 |
^ aFloat retry:#- coercing:self |
21797 | 1076 |
|
1077 |
"Modified (comment): / 12-06-2017 / 20:34:23 / cg" |
|
1 | 1078 |
! |
1079 |
||
701 | 1080 |
differenceFromFraction:aFraction |
21797 | 1081 |
"aFraction does not know how to subtract the receiver - |
701 | 1082 |
retry the operation by coercing to higher generality" |
1 | 1083 |
|
701 | 1084 |
^ aFraction retry:#- coercing:self |
21797 | 1085 |
|
1086 |
"Modified (comment): / 12-06-2017 / 20:34:16 / cg" |
|
1 | 1087 |
! |
1088 |
||
701 | 1089 |
differenceFromInteger:anInteger |
21797 | 1090 |
"anInteger does not know how to subtract the receiver - |
701 | 1091 |
retry the operation by coercing to higher generality" |
1 | 1092 |
|
701 | 1093 |
^ anInteger retry:#- coercing:self |
21797 | 1094 |
|
1095 |
"Modified (comment): / 12-06-2017 / 20:34:07 / cg" |
|
1 | 1096 |
! |
1097 |
||
7440 | 1098 |
differenceFromLargeFloat:aLargeFloat |
21797 | 1099 |
"aLargeFloat does not know how to subtract the receiver - |
7440 | 1100 |
retry the operation by coercing to higher generality" |
1101 |
||
1102 |
^ aLargeFloat retry:#- coercing:self |
|
21797 | 1103 |
|
1104 |
"Modified (comment): / 12-06-2017 / 20:34:00 / cg" |
|
7440 | 1105 |
! |
1106 |
||
7399 | 1107 |
differenceFromLongFloat:aLongFloat |
21797 | 1108 |
"aLongFloat does not know how to subtract the receiver - |
7399 | 1109 |
retry the operation by coercing to higher generality" |
1110 |
||
1111 |
^ aLongFloat retry:#- coercing:self |
|
1112 |
||
21797 | 1113 |
"Created: / 17-04-1996 / 12:33:16 / cg" |
1114 |
"Modified (comment): / 12-06-2017 / 20:33:50 / cg" |
|
7399 | 1115 |
! |
1116 |
||
1200 | 1117 |
differenceFromShortFloat:aShortFloat |
21797 | 1118 |
"aShortFloat does not know how to subtract the receiver - |
1200 | 1119 |
retry the operation by coercing to higher generality" |
1120 |
||
1121 |
^ aShortFloat retry:#- coercing:self |
|
1122 |
||
21797 | 1123 |
"Created: / 17-04-1996 / 12:33:16 / cg" |
1124 |
"Modified (comment): / 12-06-2017 / 20:33:43 / cg" |
|
1200 | 1125 |
! |
1126 |
||
7355 | 1127 |
equalFromComplex:aComplex |
21797 | 1128 |
"aComplex does not know how to compare to the receiver - |
7355 | 1129 |
retry the operation by coercing to higher generality" |
1130 |
||
1131 |
^ aComplex retry:#= coercing:self |
|
21797 | 1132 |
|
1133 |
"Modified (comment): / 12-06-2017 / 20:33:26 / cg" |
|
7355 | 1134 |
! |
1135 |
||
1136 |
equalFromFixedPoint:aFixedPoint |
|
21797 | 1137 |
"aFixedPoint does not know how to compare to the receiver - |
7355 | 1138 |
retry the operation by coercing to higher generality" |
1139 |
||
1140 |
^ aFixedPoint retry:#= coercing:self |
|
21797 | 1141 |
|
1142 |
"Modified (comment): / 12-06-2017 / 20:33:17 / cg" |
|
7355 | 1143 |
! |
1144 |
||
1145 |
equalFromFloat:aFloat |
|
21797 | 1146 |
"aFloat does not know how to compare to the receiver - |
7355 | 1147 |
retry the operation by coercing to higher generality" |
1148 |
||
1149 |
^ aFloat retry:#= coercing:self |
|
21797 | 1150 |
|
1151 |
"Modified (comment): / 12-06-2017 / 20:33:09 / cg" |
|
7355 | 1152 |
! |
1153 |
||
1154 |
equalFromFraction:aFraction |
|
21797 | 1155 |
"aFraction does not know how to compare to the receiver - |
7355 | 1156 |
retry the operation by coercing to higher generality" |
1157 |
||
1158 |
^ aFraction retry:#= coercing:self |
|
21797 | 1159 |
|
1160 |
"Modified (comment): / 12-06-2017 / 20:33:01 / cg" |
|
7355 | 1161 |
! |
1162 |
||
1163 |
equalFromInteger:anInteger |
|
21797 | 1164 |
"anInteger does not know how to compare to the receiver - |
7355 | 1165 |
retry the operation by coercing to higher generality" |
1166 |
||
1167 |
^ anInteger retry:#= coercing:self |
|
21797 | 1168 |
|
1169 |
"Modified (comment): / 12-06-2017 / 20:32:53 / cg" |
|
7355 | 1170 |
! |
1171 |
||
7440 | 1172 |
equalFromLargeFloat:aLargeFloat |
21797 | 1173 |
"aLargeFloat does not know how to compare to the receiver - |
7440 | 1174 |
retry the operation by coercing to higher generality" |
1175 |
||
1176 |
^ aLargeFloat retry:#= coercing:self |
|
21797 | 1177 |
|
1178 |
"Modified (comment): / 12-06-2017 / 20:32:45 / cg" |
|
7440 | 1179 |
! |
1180 |
||
7355 | 1181 |
equalFromLongFloat:aLongFloat |
21797 | 1182 |
"aLongFloat does not know how to compare to the receiver - |
7355 | 1183 |
retry the operation by coercing to higher generality" |
1184 |
||
1185 |
^ aLongFloat retry:#= coercing:self |
|
21797 | 1186 |
|
1187 |
"Modified (comment): / 12-06-2017 / 20:32:36 / cg" |
|
7355 | 1188 |
! |
1189 |
||
1190 |
equalFromShortFloat:aShortFloat |
|
21797 | 1191 |
"aShortFloat does not know how to compare to the receiver - |
7355 | 1192 |
retry the operation by coercing to higher generality" |
1193 |
||
1194 |
^ aShortFloat retry:#= coercing:self |
|
21797 | 1195 |
|
1196 |
"Modified (comment): / 12-06-2017 / 20:32:28 / cg" |
|
7355 | 1197 |
! |
1198 |
||
13007 | 1199 |
integerQuotientFromInteger:anInteger |
1200 |
"anInteger does not know how to divide by the receiver - |
|
1201 |
retry the operation by coercing to higher generality" |
|
1202 |
||
1203 |
^ anInteger retry:#// coercing:self |
|
1204 |
||
1205 |
"Created: / 09-08-2010 / 19:49:26 / cg" |
|
1206 |
! |
|
1207 |
||
16657 | 1208 |
isAlmostEqualToFromFloat:aFloat nEpsilon:nE |
1209 |
"the receiver does not know how to compare to a short float - |
|
1210 |
retry the operation by coercing to higher generality" |
|
1211 |
||
1212 |
^ aFloat retry:#isAlmostEqualTo:nEpsilon: coercing:self with:nE |
|
1213 |
! |
|
1214 |
||
1215 |
isAlmostEqualToFromShortFloat:aShortFloat nEpsilon:nE |
|
1216 |
"the receiver does not know how to compare to a short float - |
|
1217 |
retry the operation by coercing to higher generality" |
|
1218 |
||
1219 |
^ aShortFloat retry:#isAlmostEqualTo:nEpsilon: coercing:self with:nE |
|
1220 |
! |
|
1221 |
||
1888 | 1222 |
lessFromFixedPoint:aFixedPoint |
21797 | 1223 |
"aFixedPoint does not know how to compare to the receiver - |
18860 | 1224 |
Return true if aFixedPoint < self. |
1888 | 1225 |
retry the operation by coercing to higher generality" |
1226 |
||
1227 |
^ aFixedPoint retry:#< coercing:self |
|
1228 |
||
21797 | 1229 |
"Created: / 05-11-1996 / 14:56:12 / cg" |
1230 |
"Modified (comment): / 12-06-2017 / 20:32:12 / cg" |
|
1888 | 1231 |
! |
1232 |
||
701 | 1233 |
lessFromFloat:aFloat |
21797 | 1234 |
"aFloat does not know how to compare to the receiver - |
18860 | 1235 |
Return true if aFloat < self. |
701 | 1236 |
retry the operation by coercing to higher generality" |
1 | 1237 |
|
701 | 1238 |
^ aFloat retry:#< coercing:self |
21797 | 1239 |
|
1240 |
"Modified (comment): / 12-06-2017 / 20:32:02 / cg" |
|
1 | 1241 |
! |
1242 |
||
701 | 1243 |
lessFromFraction:aFraction |
21797 | 1244 |
"aFraction does not know how to compare to the receiver - |
18860 | 1245 |
Return true if aFraction < self. |
701 | 1246 |
retry the operation by coercing to higher generality" |
1 | 1247 |
|
701 | 1248 |
^ aFraction retry:#< coercing:self |
21797 | 1249 |
|
1250 |
"Modified (comment): / 12-06-2017 / 20:31:53 / cg" |
|
1 | 1251 |
! |
1252 |
||
701 | 1253 |
lessFromInteger:anInteger |
21797 | 1254 |
"anInteger does not know how to compare to the receiver - |
18860 | 1255 |
Return true if anInteger < self. |
701 | 1256 |
retry the operation by coercing to higher generality" |
1 | 1257 |
|
701 | 1258 |
^ anInteger retry:#< coercing:self |
21797 | 1259 |
|
1260 |
"Modified (comment): / 12-06-2017 / 20:31:44 / cg" |
|
1 | 1261 |
! |
1262 |
||
7440 | 1263 |
lessFromLargeFloat:aLargeFloat |
21797 | 1264 |
"aLargeFloat does not know how to compare to the receiver - |
18860 | 1265 |
Return true if aLargeFloat < self. |
7440 | 1266 |
retry the operation by coercing to higher generality" |
1267 |
||
1268 |
^ aLargeFloat retry:#< coercing:self |
|
21797 | 1269 |
|
1270 |
"Modified (comment): / 12-06-2017 / 20:31:36 / cg" |
|
7440 | 1271 |
! |
1272 |
||
7399 | 1273 |
lessFromLongFloat:aLongFloat |
21797 | 1274 |
"aLongFloat does not know how to compare to the receiver - |
18860 | 1275 |
Return true if aLongFloat < self. |
7399 | 1276 |
retry the operation by coercing to higher generality" |
1277 |
||
1278 |
^ aLongFloat retry:#< coercing:self |
|
1279 |
||
21797 | 1280 |
"Modified: / 17-04-1996 / 12:33:33 / cg" |
1281 |
"Modified (comment): / 12-06-2017 / 20:31:28 / cg" |
|
7399 | 1282 |
! |
1283 |
||
1200 | 1284 |
lessFromShortFloat:aShortFloat |
21797 | 1285 |
"aShortFloat does not know how to compare to the receiver - |
18860 | 1286 |
Return true if aShortFloat < self. |
1200 | 1287 |
retry the operation by coercing to higher generality" |
1288 |
||
1289 |
^ aShortFloat retry:#< coercing:self |
|
1290 |
||
21797 | 1291 |
"Modified: / 17-04-1996 / 12:33:33 / cg" |
1292 |
"Modified (comment): / 12-06-2017 / 20:31:18 / cg" |
|
1200 | 1293 |
! |
1294 |
||
14002 | 1295 |
moduloFromInteger:anInteger |
1296 |
"anInteger does not know how to compute the modulo from the receiver - |
|
1297 |
retry the operation by coercing to higher generality" |
|
1298 |
||
1299 |
^ anInteger retry:#\\ coercing:self |
|
1300 |
||
1301 |
"Created: / 12-02-2012 / 20:38:13 / cg" |
|
1302 |
! |
|
1303 |
||
7355 | 1304 |
productFromComplex:aComplex |
21797 | 1305 |
"aComplex does not know how to multiply the receiver - |
7355 | 1306 |
retry the operation by coercing to higher generality" |
1307 |
||
1308 |
^ aComplex retry:#* coercing:self |
|
21797 | 1309 |
|
1310 |
"Modified (comment): / 12-06-2017 / 20:31:06 / cg" |
|
7355 | 1311 |
! |
1312 |
||
1888 | 1313 |
productFromFixedPoint:aFixedPoint |
21797 | 1314 |
"aFixedPoint does not know how to multiply the receiver - |
1888 | 1315 |
retry the operation by coercing to higher generality" |
1316 |
||
1317 |
^ aFixedPoint retry:#* coercing:self |
|
1318 |
||
21797 | 1319 |
"Created: / 05-11-1996 / 14:56:28 / cg" |
1320 |
"Modified (comment): / 12-06-2017 / 20:30:56 / cg" |
|
1888 | 1321 |
! |
1322 |
||
701 | 1323 |
productFromFloat:aFloat |
21797 | 1324 |
"aFloat does not know how to multiply the receiver - |
701 | 1325 |
retry the operation by coercing to higher generality" |
1 | 1326 |
|
701 | 1327 |
^ aFloat retry:#* coercing:self |
21797 | 1328 |
|
1329 |
"Modified (comment): / 12-06-2017 / 20:30:46 / cg" |
|
1 | 1330 |
! |
1331 |
||
701 | 1332 |
productFromFraction:aFraction |
21797 | 1333 |
"aFraction does not know how to multiply the receiver - |
701 | 1334 |
retry the operation by coercing to higher generality" |
1 | 1335 |
|
701 | 1336 |
^ aFraction retry:#* coercing:self |
21797 | 1337 |
|
1338 |
"Modified (comment): / 12-06-2017 / 20:30:38 / cg" |
|
1 | 1339 |
! |
1340 |
||
701 | 1341 |
productFromInteger:anInteger |
21797 | 1342 |
"anInteger does not know how to multiply the receiver - |
701 | 1343 |
retry the operation by coercing to higher generality" |
1 | 1344 |
|
701 | 1345 |
^ anInteger retry:#* coercing:self |
21797 | 1346 |
|
1347 |
"Modified (comment): / 12-06-2017 / 20:29:21 / cg" |
|
1 | 1348 |
! |
1349 |
||
7440 | 1350 |
productFromLargeFloat:aLargeFloat |
21797 | 1351 |
"aLargeFloat does not know how to multiply the receiver - |
7440 | 1352 |
retry the operation by coercing to higher generality" |
1353 |
||
1354 |
^ aLargeFloat retry:#* coercing:self |
|
1355 |
||
21797 | 1356 |
"Created: / 17-04-1996 / 12:33:48 / cg" |
1357 |
"Modified (comment): / 12-06-2017 / 20:29:13 / cg" |
|
7440 | 1358 |
! |
1359 |
||
7399 | 1360 |
productFromLongFloat:aLongFloat |
21797 | 1361 |
"aLongFloat does not know how to multiply the receiver - |
7399 | 1362 |
retry the operation by coercing to higher generality" |
1363 |
||
1364 |
^ aLongFloat retry:#* coercing:self |
|
1365 |
||
21797 | 1366 |
"Created: / 17-04-1996 / 12:33:48 / cg" |
1367 |
"Modified (comment): / 12-06-2017 / 20:29:05 / cg" |
|
7399 | 1368 |
! |
1369 |
||
1200 | 1370 |
productFromShortFloat:aShortFloat |
21797 | 1371 |
"aShortFloat does not know how to multiply the receiver - |
1200 | 1372 |
retry the operation by coercing to higher generality" |
1373 |
||
1374 |
^ aShortFloat retry:#* coercing:self |
|
1375 |
||
21797 | 1376 |
"Created: / 17-04-1996 / 12:33:48 / cg" |
1377 |
"Modified (comment): / 12-06-2017 / 20:28:56 / cg" |
|
1200 | 1378 |
! |
1379 |
||
7355 | 1380 |
quotientFromComplex:aComplex |
7440 | 1381 |
"aComplex does not know how to divide by the receiver - |
7355 | 1382 |
retry the operation by coercing to higher generality" |
1383 |
||
1384 |
^ aComplex retry:#/ coercing:self |
|
1385 |
! |
|
1386 |
||
1888 | 1387 |
quotientFromFixedPoint:aFixedPoint |
7440 | 1388 |
"aFixedPoint does not know how to divide by the receiver - |
1888 | 1389 |
retry the operation by coercing to higher generality" |
1390 |
||
1391 |
^ aFixedPoint retry:#/ coercing:self |
|
1392 |
||
1393 |
"Created: 5.11.1996 / 14:56:41 / cg" |
|
1394 |
! |
|
1395 |
||
701 | 1396 |
quotientFromFloat:aFloat |
7440 | 1397 |
"aFloat does not know how to divide by the receiver - |
701 | 1398 |
retry the operation by coercing to higher generality" |
1 | 1399 |
|
701 | 1400 |
^ aFloat retry:#/ coercing:self |
1 | 1401 |
! |
1402 |
||
701 | 1403 |
quotientFromFraction:aFraction |
7440 | 1404 |
"aFraction does not know how to divide by the receiver - |
701 | 1405 |
retry the operation by coercing to higher generality" |
1 | 1406 |
|
701 | 1407 |
^ aFraction retry:#/ coercing:self |
1 | 1408 |
! |
1409 |
||
21797 | 1410 |
quotientFromInteger:aQDouble |
1411 |
"aQDouble does not know how to divide by the receiver - |
|
1 | 1412 |
retry the operation by coercing to higher generality" |
1413 |
||
21797 | 1414 |
^ aQDouble retry:#/ coercing:self |
1415 |
||
1416 |
"Modified (comment): / 12-06-2017 / 20:35:45 / cg" |
|
1 | 1417 |
! |
1418 |
||
7440 | 1419 |
quotientFromLargeFloat:aLargeFloat |
1420 |
"aLargeFloat does not know how to divide by the receiver - |
|
1421 |
retry the operation by coercing to higher generality" |
|
1422 |
||
1423 |
^ aLargeFloat retry:#/ coercing:self |
|
1424 |
||
1425 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
1426 |
! |
|
1427 |
||
7399 | 1428 |
quotientFromLongFloat:aLongFloat |
7440 | 1429 |
"aLongFloat does not know how to divide by the receiver - |
7399 | 1430 |
retry the operation by coercing to higher generality" |
1431 |
||
1432 |
^ aLongFloat retry:#/ coercing:self |
|
1433 |
||
1434 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
1435 |
! |
|
1436 |
||
1200 | 1437 |
quotientFromShortFloat:aShortFloat |
7440 | 1438 |
"aShortFloat does not know how to divide by the receiver - |
1200 | 1439 |
retry the operation by coercing to higher generality" |
1440 |
||
1441 |
^ aShortFloat retry:#/ coercing:self |
|
1442 |
||
1443 |
"Created: 17.4.1996 / 12:34:00 / cg" |
|
1444 |
! |
|
1445 |
||
21935 | 1446 |
raisedFromFloat:aFloat |
1447 |
"aFloat does not know how to be raised to the receiver" |
|
1448 |
||
1449 |
self isNumber ifTrue:[ |
|
1450 |
self isFloat ifFalse:[ |
|
1451 |
^ self asFloat raisedFromFloat:aFloat |
|
1452 |
]. |
|
1453 |
]. |
|
1454 |
^ self raisedFromNumber:aFloat |
|
1455 |
||
1456 |
"Created: / 01-07-2017 / 21:18:54 / cg" |
|
1457 |
"Modified: / 02-07-2017 / 00:44:47 / cg" |
|
1458 |
! |
|
1459 |
||
1460 |
raisedFromNumber:aNumber |
|
21967 | 1461 |
"aNumber does not know how to be raised to the receiver |
1462 |
(i.e. how to compute aNumber^self)" |
|
21935 | 1463 |
|
1464 |
^ self class |
|
1465 |
raise:#domainErrorSignal |
|
1466 |
receiver:aNumber |
|
1467 |
selector:#raisedTo: |
|
1468 |
arguments:(Array with:self) |
|
1469 |
errorString:'bad receiver/arg in raisedTo:' |
|
1470 |
||
1471 |
"Created: / 01-07-2017 / 21:17:06 / cg" |
|
21967 | 1472 |
"Modified (comment): / 03-07-2017 / 14:06:53 / cg" |
21935 | 1473 |
! |
1474 |
||
14747 | 1475 |
remainderFromFloat:aFloat |
1476 |
"aFloat does not know how to compute the remainder with the receiver - |
|
1477 |
retry the operation by coercing to higher generality" |
|
1478 |
||
1479 |
^ aFloat retry:#rem: coercing:self |
|
1480 |
! |
|
1481 |
||
1482 |
remainderFromShortFloat:aShortFloat |
|
1483 |
"aShortFloat does not know how to compute the remainder with the receiver - |
|
1484 |
retry the operation by coercing to higher generality" |
|
1485 |
||
1486 |
^ aShortFloat retry:#rem: coercing:self |
|
1487 |
! |
|
1488 |
||
7355 | 1489 |
sumFromComplex:aComplex |
21797 | 1490 |
"aComplex does not know how to add the receiver - |
7355 | 1491 |
retry the operation by coercing to higher generality" |
1492 |
||
1493 |
^ aComplex retry:#+ coercing:self |
|
21797 | 1494 |
|
1495 |
"Modified (comment): / 12-06-2017 / 20:27:25 / cg" |
|
7355 | 1496 |
! |
1497 |
||
1888 | 1498 |
sumFromFixedPoint:aFixedPoint |
21797 | 1499 |
"aFixedPoint does not know how to add the receiver - |
1888 | 1500 |
retry the operation by coercing to higher generality" |
1501 |
||
1502 |
^ aFixedPoint retry:#+ coercing:self |
|
1503 |
||
21797 | 1504 |
"Created: / 05-11-1996 / 14:56:56 / cg" |
1505 |
"Modified (comment): / 12-06-2017 / 20:27:38 / cg" |
|
1888 | 1506 |
! |
1507 |
||
1 | 1508 |
sumFromFloat:aFloat |
21797 | 1509 |
"aFloat does not know how to add the receiver - |
1 | 1510 |
retry the operation by coercing to higher generality" |
1511 |
||
1512 |
^ aFloat retry:#+ coercing:self |
|
21797 | 1513 |
|
1514 |
"Modified (comment): / 12-06-2017 / 20:27:47 / cg" |
|
1 | 1515 |
! |
1516 |
||
1517 |
sumFromFraction:aFraction |
|
21797 | 1518 |
"aFraction does not know how to add the receiver - |
1 | 1519 |
retry the operation by coercing to higher generality" |
1520 |
||
1521 |
^ aFraction retry:#+ coercing:self |
|
21797 | 1522 |
|
1523 |
"Modified (comment): / 12-06-2017 / 20:27:55 / cg" |
|
1 | 1524 |
! |
1525 |
||
701 | 1526 |
sumFromInteger:anInteger |
21797 | 1527 |
"anInteger does not know how to add the receiver - |
1 | 1528 |
retry the operation by coercing to higher generality" |
1529 |
||
701 | 1530 |
^ anInteger retry:#+ coercing:self |
21797 | 1531 |
|
1532 |
"Modified (comment): / 12-06-2017 / 20:28:03 / cg" |
|
1200 | 1533 |
! |
1534 |
||
7440 | 1535 |
sumFromLargeFloat:aLargeFloat |
21797 | 1536 |
"aLargeFloat does not know how to add the receiver - |
7440 | 1537 |
retry the operation by coercing to higher generality" |
1538 |
||
1539 |
^ aLargeFloat retry:#+ coercing:self |
|
1540 |
||
21797 | 1541 |
"Created: / 17-04-1996 / 12:34:10 / cg" |
1542 |
"Modified (comment): / 12-06-2017 / 20:28:11 / cg" |
|
7440 | 1543 |
! |
1544 |
||
7399 | 1545 |
sumFromLongFloat:aLongFloat |
21797 | 1546 |
"aLongFloat does not know how to add the receiver - |
7399 | 1547 |
retry the operation by coercing to higher generality" |
1548 |
||
1549 |
^ aLongFloat retry:#+ coercing:self |
|
1550 |
||
21797 | 1551 |
"Created: / 17-04-1996 / 12:34:10 / cg" |
1552 |
"Modified (comment): / 12-06-2017 / 20:28:19 / cg" |
|
7399 | 1553 |
! |
1554 |
||
1200 | 1555 |
sumFromShortFloat:aShortFloat |
21797 | 1556 |
"aShortFloat does not know how to add the receiver - |
1200 | 1557 |
retry the operation by coercing to higher generality" |
1558 |
||
1559 |
^ aShortFloat retry:#+ coercing:self |
|
1560 |
||
21797 | 1561 |
"Created: / 17-04-1996 / 12:34:10 / cg" |
1562 |
"Modified (comment): / 12-06-2017 / 20:28:39 / cg" |
|
1 | 1563 |
! ! |
1564 |
||
7384 | 1565 |
!ArithmeticValue methodsFor:'mathematical functions'! |
1 | 1566 |
|
10326 | 1567 |
** aNumber |
9443 | 1568 |
"Answer the receiver raised to the power of the argument, aNumber." |
1569 |
"same as Number>>raisedTo:" |
|
1570 |
||
1571 |
^self raisedTo: aNumber |
|
1572 |
||
1573 |
"Created: / 15-07-2006 / 15:11:05 / cg" |
|
1574 |
"Modified: / 17-07-2006 / 12:51:33 / cg" |
|
1575 |
! |
|
1576 |
||
18860 | 1577 |
basicRaisedToInteger:exp |
21804 | 1578 |
"return the receiver raised to exp. |
1579 |
Warning: if the receiver is a float/double, |
|
1580 |
currently INF may be returned on overflow. |
|
1581 |
This may be changed silently to raise an error in future versions." |
|
18860 | 1582 |
|
1583 |
|result e t| |
|
1584 |
||
1585 |
"use the addition chaining algorithm, |
|
1586 |
which is much faster for big exponents" |
|
6858 | 1587 |
|
18860 | 1588 |
result := 1. |
1589 |
t := self. |
|
1590 |
exp < 0 ifTrue:[ |
|
1591 |
e := exp negated. |
|
1592 |
] ifFalse:[ |
|
1593 |
e := exp. |
|
1594 |
]. |
|
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1595 |
|
18860 | 1596 |
[e ~~ 0] whileTrue:[ |
1597 |
[(e bitAnd:1) == 0] whileTrue:[ |
|
1598 |
e := e bitShift:-1. |
|
1599 |
t := t * t. |
|
1600 |
]. |
|
1601 |
e := e - 1. |
|
1602 |
result := result * t. |
|
1603 |
]. |
|
1604 |
||
1605 |
(exp < 0) ifTrue:[ |
|
1606 |
^ 1 / result |
|
1607 |
]. |
|
1608 |
||
1609 |
^ result |
|
18835 | 1610 |
|
1611 |
" |
|
1612 |
(2.0 raisedToInteger:216) |
|
1613 |
(2 raisedToInteger:216) |
|
1614 |
(2 raisedTo:216) |
|
1615 |
-> 105312291668557186697918027683670432318895095400549111254310977536 |
|
1616 |
||
1617 |
(2 raisedToInteger:216) asFloat |
|
1618 |
(2 raisedTo:216) asFloat |
|
1619 |
-> 1.05312E+65 |
|
1620 |
||
1621 |
(2 raisedToInteger:500) |
|
1622 |
(2 raisedTo:500) |
|
1623 |
-> 3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376 |
|
18860 | 1624 |
(2 raisedTo:-500) |
1625 |
-> (1/3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376) |
|
18835 | 1626 |
2 raisedToInteger:10 |
1627 |
-> 1024 |
|
1628 |
-2 raisedToInteger:10 |
|
1629 |
-> 1024 |
|
1630 |
-2 raisedToInteger:9 |
|
1631 |
-> -512 |
|
1632 |
10 raisedToInteger:-10 |
|
1633 |
-> (1/10000000000) |
|
1634 |
2 raisedToInteger:0 |
|
1635 |
-> 1 |
|
1636 |
2 raisedToInteger:-1 |
|
1637 |
-> (1/2) |
|
1638 |
||
1639 |
Time millisecondsToRun:[ |
|
1640 |
10000 timesRepeat:[ |
|
1641 |
(2 raisedToInteger:500) |
|
1642 |
] |
|
1643 |
] |
|
1644 |
||
1645 |
Time millisecondsToRun:[ |
|
1646 |
|bigNum| |
|
1647 |
bigNum := 2 raisedToInteger:500. |
|
1648 |
10 timesRepeat:[ |
|
1649 |
(bigNum raisedToInteger:500) |
|
1650 |
] |
|
1651 |
] |
|
1652 |
" |
|
1653 |
||
21804 | 1654 |
"Created: / 27-04-1999 / 15:19:22 / stefan" |
1655 |
"Modified (comment): / 13-06-2017 / 17:35:41 / cg" |
|
18835 | 1656 |
! |
1657 |
||
18860 | 1658 |
raisedTo: aNumber |
21935 | 1659 |
"return the receiver raised to aNumber (i.e. self ^ aNumber)" |
1660 |
||
18860 | 1661 |
aNumber isInteger ifTrue:[ |
21935 | 1662 |
^ self raisedToInteger:aNumber |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1663 |
]. |
18860 | 1664 |
^ self subclassResponsibility |
21935 | 1665 |
|
1666 |
" |
|
1667 |
2 raisedTo:16 |
|
1668 |
2.0 raisedTo:16 |
|
1669 |
||
1670 |
3 raisedTo:4 |
|
1671 |
" |
|
1672 |
||
1673 |
"Modified (format): / 01-07-2017 / 20:59:35 / cg" |
|
18860 | 1674 |
! |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1675 |
|
18860 | 1676 |
raisedToInteger:exp |
1677 |
"return the receiver raised to exp. |
|
21804 | 1678 |
Warning: if the receiver is a float/double, |
1679 |
currently INF may be returned on overflow. |
|
1680 |
This may be changed silently to raise an error in future versions." |
|
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1681 |
|
18860 | 1682 |
^ self basicRaisedToInteger:exp |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1683 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1684 |
" |
18860 | 1685 |
(2.0 raisedToInteger:10000) |
1686 |
(2 raisedToInteger:10000) |
|
1687 |
(2 raisedToInteger:-10000) |
|
1688 |
||
17662 | 1689 |
(2.0 raisedToInteger:216) |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1690 |
(2 raisedToInteger:216) |
10326 | 1691 |
(2 raisedTo:216) |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1692 |
-> 105312291668557186697918027683670432318895095400549111254310977536 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1693 |
|
10326 | 1694 |
(2 raisedToInteger:216) asFloat |
1695 |
(2 raisedTo:216) asFloat |
|
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1696 |
-> 1.05312E+65 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1697 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1698 |
(2 raisedToInteger:500) |
10326 | 1699 |
(2 raisedTo:500) |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1700 |
-> 3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376 |
10326 | 1701 |
2 raisedToInteger:10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1702 |
-> 1024 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1703 |
-2 raisedToInteger:10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1704 |
-> 1024 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1705 |
-2 raisedToInteger:9 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1706 |
-> -512 |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1707 |
10 raisedToInteger:-10 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1708 |
-> (1/10000000000) |
10326 | 1709 |
2 raisedToInteger:0 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1710 |
-> 1 |
10326 | 1711 |
2 raisedToInteger:-1 |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1712 |
-> (1/2) |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1713 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1714 |
Time millisecondsToRun:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1715 |
10000 timesRepeat:[ |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1716 |
(2 raisedToInteger:500) |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1717 |
] |
10326 | 1718 |
] |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1719 |
|
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1720 |
Time millisecondsToRun:[ |
12760
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1721 |
|bigNum| |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1722 |
bigNum := 2 raisedToInteger:500. |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1723 |
10 timesRepeat:[ |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1724 |
(bigNum raisedToInteger:500) |
6482afd031af
comment/format in: #raisedToInteger:
Claus Gittinger <cg@exept.de>
parents:
12578
diff
changeset
|
1725 |
] |
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1726 |
] |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1727 |
" |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1728 |
|
21804 | 1729 |
"Created: / 27-04-1999 / 15:19:22 / stefan" |
1730 |
"Modified (comment): / 13-06-2017 / 17:35:20 / cg" |
|
8633
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1731 |
! |
239f2bd03afe
raisedTo: / raisedToInteger: moved up
Claus Gittinger <cg@exept.de>
parents:
8449
diff
changeset
|
1732 |
|
701 | 1733 |
squared |
1734 |
"return receiver * receiver" |
|
1735 |
||
1736 |
^ self * self |
|
1737 |
! ! |
|
1738 |
||
1739 |
!ArithmeticValue methodsFor:'queries'! |
|
1740 |
||
1741 |
respondsToArithmetic |
|
1742 |
"return true, if the receiver responds to arithmetic messages" |
|
1743 |
||
1744 |
^ true |
|
1745 |
! ! |
|
1746 |
||
1747 |
!ArithmeticValue methodsFor:'testing'! |
|
1748 |
||
1749 |
denominator |
|
1750 |
"return the denominator of the receiver" |
|
1751 |
||
1752 |
^ 1 |
|
1753 |
! |
|
1754 |
||
1755 |
even |
|
17223 | 1756 |
"return true if the receiver is divisible by 2. |
21967 | 1757 |
This is only defined for whole-numbers (integers)." |
701 | 1758 |
|
17223 | 1759 |
^ self class |
21967 | 1760 |
raise:#domainErrorSignal |
17223 | 1761 |
receiver:self |
1762 |
selector:#even |
|
1763 |
arguments:#() |
|
1764 |
errorString:'Receiver must be a whole-number' |
|
1765 |
||
21967 | 1766 |
"/ No no no, the following is bad: |
1767 |
"/ ^ self truncated asInteger even |
|
17223 | 1768 |
|
1769 |
" |
|
21967 | 1770 |
2.4 even |
1771 |
2.0 even |
|
17223 | 1772 |
" |
21967 | 1773 |
|
1774 |
"Modified (comment): / 03-07-2017 / 14:10:01 / cg" |
|
701 | 1775 |
! |
1776 |
||
3635 | 1777 |
isComplex |
7355 | 1778 |
"Answer whether the receiver has an imaginary part |
1779 |
(i.e. if it is a complex number). Always false here." |
|
3635 | 1780 |
|
1781 |
^ false |
|
1782 |
||
1783 |
"Modified: / 9.7.1998 / 10:19:27 / cg" |
|
1784 |
! |
|
1785 |
||
8979
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1786 |
isFinite |
10326 | 1787 |
"return true, if the receiver is finite |
9400 | 1788 |
i.e. it can be represented as a rational number." |
1789 |
||
8979
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1790 |
^ true |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1791 |
! |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1792 |
|
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1793 |
isInfinite |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1794 |
^ false |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1795 |
! |
869510c58b9c
Move #isFinite and #isInfinite from Number to ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
1796 |
|
7440 | 1797 |
isNegativeInfinity |
1798 |
^ self negative and:[self isInfinite] |
|
1799 |
! |
|
1800 |
||
1801 |
isNegativeZero |
|
1802 |
"return false - must be redefined by subclasses which can represent a negative zero |
|
1803 |
(i.e. limitedPrecisionReal classes)" |
|
1804 |
||
1805 |
^ false |
|
1806 |
! |
|
1807 |
||
1808 |
isPositiveInfinity |
|
1809 |
^ self positive and:[self isInfinite] |
|
1810 |
! |
|
1811 |
||
6156 | 1812 |
isReal |
1813 |
"return true, if the receiver is some kind of real number (as opposed to a complex); |
|
1814 |
false is returned here - the method is only redefined in Number (and Complex)." |
|
1815 |
||
1816 |
^ false |
|
1817 |
||
1818 |
"Modified: / 6.11.2001 / 13:20:20 / cg" |
|
1819 |
"Created: / 6.11.2001 / 13:26:29 / cg" |
|
1820 |
! |
|
1821 |
||
7440 | 1822 |
isZero |
1823 |
"return false - must be redefined by subclasses which can represent a negative zero |
|
1824 |
(i.e. limitedPrecisionReal classes)" |
|
701 | 1825 |
|
7440 | 1826 |
^ self = self class zero |
1827 |
! |
|
1828 |
||
1829 |
negative |
|
18860 | 1830 |
"return true if the receiver is less than zero." |
7440 | 1831 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
1832 |
^ self < self class zero |
701 | 1833 |
! |
1834 |
||
1835 |
numerator |
|
1836 |
"return the numerator of the receiver." |
|
1837 |
||
1838 |
^ self |
|
1839 |
! |
|
1840 |
||
1841 |
odd |
|
1842 |
"return true if the receiver is not divisible by 2" |
|
1843 |
||
1844 |
^ self even not |
|
1845 |
! |
|
1846 |
||
1847 |
positive |
|
18860 | 1848 |
"return true, if the receiver is greater or equal to zero (not negative)" |
701 | 1849 |
|
1850 |
^ self negative not |
|
1851 |
! |
|
1852 |
||
1853 |
sign |
|
7471 | 1854 |
"return the sign of the receiver (-1, 0 or 1)" |
701 | 1855 |
|
7440 | 1856 |
|zero| |
1857 |
||
1858 |
zero := self class zero. |
|
1859 |
(self < zero) ifTrue:[^ -1]. |
|
1860 |
(zero < self) ifTrue:[^ 1]. |
|
701 | 1861 |
^ 0 |
1862 |
! |
|
1863 |
||
1864 |
strictlyPositive |
|
18860 | 1865 |
"return true, if the receiver is greater than zero" |
701 | 1866 |
|
8202
2a6c8d1e26cb
Define #asFixedPoint in ArithmethicValue
Stefan Vogel <sv@exept.de>
parents:
8184
diff
changeset
|
1867 |
^ self class zero < self |
1 | 1868 |
! ! |
1869 |
||
701 | 1870 |
!ArithmeticValue methodsFor:'truncation & rounding'! |
1 | 1871 |
|
701 | 1872 |
ceiling |
1873 |
"return the integer nearest the receiver towards positive infinity." |
|
1 | 1874 |
|
701 | 1875 |
|anInteger| |
1 | 1876 |
|
701 | 1877 |
anInteger := self // 1. "truncates towards negative infinity" |
1878 |
anInteger = self ifTrue:[^ anInteger]. |
|
1879 |
^ anInteger + 1 |
|
1 | 1880 |
! |
1881 |
||
701 | 1882 |
floor |
1883 |
"return the receiver truncated towards negative infinity" |
|
1 | 1884 |
|
701 | 1885 |
^ self // 1 |
1 | 1886 |
! |
1887 |
||
701 | 1888 |
roundTo:aNumber |
1889 |
"return the receiver rounded to multiples of aNumber" |
|
1 | 1890 |
|
701 | 1891 |
^ (self / aNumber) rounded * aNumber |
8449 | 1892 |
|
1893 |
" |
|
10326 | 1894 |
0 roundTo:4 |
8449 | 1895 |
1 roundTo:4 |
1896 |
2 roundTo:4 |
|
1897 |
3 roundTo:4 |
|
1898 |
4 roundTo:4 |
|
1899 |
5 roundTo:4 |
|
1900 |
6 roundTo:4 |
|
1901 |
7 roundTo:4 |
|
14497 | 1902 |
|
1903 |
7.123 roundTo:0.1 |
|
1904 |
7.523 roundTo:0.1 |
|
1905 |
7.583 roundTo:0.1 |
|
1906 |
7.623 roundTo:0.1 |
|
1907 |
7.623 roundTo:0.01 |
|
1908 |
7.628 roundTo:0.01 |
|
8449 | 1909 |
" |
1910 |
! |
|
1911 |
||
1912 |
roundUpTo:aNumber |
|
1913 |
"return the receiver rounded up to the next multiple of aNumber" |
|
1914 |
||
1915 |
^ (self / aNumber) ceiling * aNumber |
|
1916 |
||
1917 |
" |
|
10326 | 1918 |
0 roundUpTo:4 |
1919 |
1 roundUpTo:4 |
|
1920 |
2 roundUpTo:4 |
|
1921 |
3 roundUpTo:4 |
|
1922 |
4 roundUpTo:4 |
|
1923 |
5 roundUpTo:4 |
|
1924 |
6 roundUpTo:4 |
|
1925 |
7 roundUpTo:4 |
|
1926 |
8 roundUpTo:4 |
|
10911 | 1927 |
(3@4) roundUpTo:8 |
1928 |
(3@4) roundUpTo:(5 @ 4) |
|
1929 |
(3@3) roundUpTo:(5 @ 4) |
|
8449 | 1930 |
" |
701 | 1931 |
! |
1932 |
||
1933 |
rounded |
|
1934 |
"return the integer nearest the receiver" |
|
1935 |
||
1881 | 1936 |
self negative ifTrue:[ |
10326 | 1937 |
^ (self - 0.5) ceiling |
1881 | 1938 |
]. |
701 | 1939 |
^ (self + 0.5) floor |
1881 | 1940 |
|
1941 |
"Modified: 5.11.1996 / 11:31:59 / cg" |
|
1 | 1942 |
! |
1943 |
||
701 | 1944 |
truncateTo:aNumber |
1945 |
"return the receiver truncated to multiples of aNumber" |
|
1 | 1946 |
|
19056 | 1947 |
|t| |
1948 |
||
1949 |
aNumber <= 0 ifTrue:[self error:'argument must be positive']. |
|
1950 |
||
1951 |
t := (self / aNumber) floor * aNumber. |
|
1952 |
(aNumber isInteger) ifTrue:[ |
|
1953 |
^ t asInteger |
|
1954 |
]. |
|
1955 |
^ t |
|
1956 |
||
1957 |
" |
|
1958 |
truncate to multiples of 4 |
|
1959 |
123.456 truncateTo:4 |
|
1960 |
124.456 truncateTo:4 |
|
1961 |
truncate to multiples of 2 |
|
1962 |
122.456 truncateTo:2 |
|
1963 |
123.456 truncateTo:2 |
|
1964 |
124.456 truncateTo:2 |
|
1965 |
normal truncate |
|
1966 |
123.456 truncateTo:1 |
|
1967 |
124.456 truncateTo:1 |
|
1968 |
truncate to decimal digits |
|
1969 |
123.456 truncateTo:0.1 |
|
1970 |
123.987 truncateTo:0.1 |
|
1971 |
123.456 truncateTo:0.01 |
|
1972 |
" |
|
1 | 1973 |
! |
1974 |
||
701 | 1975 |
truncated |
1976 |
"return the receiver truncated towards zero" |
|
1 | 1977 |
|
701 | 1978 |
self negative ifTrue:[ |
1979 |
^ self ceiling |
|
1980 |
]. |
|
1981 |
^ self floor |
|
1982 |
! ! |
|
1 | 1983 |
|
701 | 1984 |
!ArithmeticValue class methodsFor:'documentation'! |
1985 |
||
1986 |
version |
|
18835 | 1987 |
^ '$Header$' |
12576 | 1988 |
! |
1989 |
||
1990 |
version_CVS |
|
18835 | 1991 |
^ '$Header$' |
1 | 1992 |
! ! |
6858 | 1993 |
|
14747 | 1994 |
|
701 | 1995 |
ArithmeticValue initialize! |