author | Claus Gittinger <cg@exept.de> |
Thu, 31 Aug 2000 12:03:29 +0200 | |
changeset 5570 | e6e14f50d721 |
parent 3731 | 8c2ef8f76670 |
child 6237 | 4dc908af4f5c |
permissions | -rw-r--r-- |
1886 | 1 |
" |
3060 | 2 |
This is public domain code, not covered by the ST/X copyright. |
3 |
Code is provided 'as is', as a goody, without any warranty. |
|
4 |
||
1886 | 5 |
this comes from: |
6 |
||
7 |
Jan Steinman, Bytesmiths |
|
8 |
2002 Parkside Court, West Linn, OR 97068-2767 USA, +1 503 657 7703 |
|
9 |
Friedlistrasse 19, CH-3006, Bern, Switzerland, +41 31 999 3946 |
|
10 |
||
11 |
this code was published in comp.lang.smalltalk; |
|
12 |
added here as an example ... |
|
13 |
" |
|
14 |
||
5570 | 15 |
"{ Package: 'stx:libbasic' }" |
16 |
||
1886 | 17 |
Fraction subclass:#FixedPoint |
18 |
instanceVariableNames:'scale' |
|
19 |
classVariableNames:'' |
|
20 |
poolDictionaries:'' |
|
21 |
category:'Magnitude-Numbers' |
|
22 |
! |
|
23 |
||
24 |
FixedPoint comment:' |
|
25 |
Description: This class implements infinite precision fixed-point numbers. |
|
26 |
It doesn''t really do anything too interesting except creating instances, converting, and printing, |
|
27 |
since its superclass Fraction does all the work. |
|
28 |
||
29 |
Test: "''123456789012345678901234567890.123456789'' asFixed * 1000000000 = 123456789012345678901234567890123456789" |
|
30 |
||
31 |
Notes: 1) The current implementation does not convert arbitrarily-based String representations, |
|
32 |
which shouldn''t be too much a problem for financial types.'! |
|
33 |
||
34 |
!FixedPoint class methodsFor:'documentation'! |
|
35 |
||
36 |
copyright |
|
37 |
" |
|
3060 | 38 |
This is public domain code, not covered by the ST/X copyright. |
39 |
Code is provided 'as is', as a goody, without any warranty. |
|
40 |
||
1886 | 41 |
this comes from: |
42 |
||
43 |
Jan Steinman, Bytesmiths |
|
44 |
2002 Parkside Court, West Linn, OR 97068-2767 USA, +1 503 657 7703 |
|
45 |
Friedlistrasse 19, CH-3006, Bern, Switzerland, +41 31 999 3946 |
|
46 |
||
47 |
this code was published in comp.lang.smalltalk; |
|
48 |
added here as an example ... |
|
49 |
" |
|
50 |
! |
|
51 |
||
52 |
documentation |
|
53 |
" |
|
54 |
Description: This class implements infinite precision fixed-point numbers, |
|
55 |
which internally hold exact (fractional) results, but print themself with |
|
56 |
a limited number of digits after the decimal point. |
|
57 |
These can be used in computation, where rounding errors should not accumulate, |
|
58 |
but only a limited precision is required for the final result. |
|
1898 | 59 |
(i.e. business applications) |
1886 | 60 |
|
61 |
It doesn't really do anything too interesting except creating instances, |
|
62 |
converting, and printing, since its superclass Fraction does all the work. |
|
63 |
||
64 |
Test: |
|
3060 | 65 |
'123456789012345678901234567890.123456789' asFixedPoint * 1000000000 |
1886 | 66 |
-> 123456789012345678901234567890123456789' |
67 |
||
68 |
Notes: 1) The current implementation does not convert arbitrarily-based |
|
69 |
String representations, which shouldn't be too much a problem |
|
70 |
for financial types. |
|
71 |
||
72 |
2) the implementation is a hack - it has not been optimized for speed |
|
73 |
in particular. |
|
74 |
||
1892 | 75 |
Mixed mode arithmetic: |
76 |
fix op fix -> fix, scale is max. of operands |
|
77 |
fix op fraction -> fix; scale is fix's scale |
|
78 |
fix op integer -> fix; scale is fix's scale |
|
79 |
fix op float -> float |
|
80 |
||
1886 | 81 |
[author:] |
82 |
Jan Steinman, Bytesmiths |
|
3060 | 83 |
adapted, modified & enhanced by Claus Gittinger |
1886 | 84 |
|
85 |
[see also:] |
|
1893 | 86 |
Integer Float Number Fraction |
1886 | 87 |
" |
88 |
! |
|
89 |
||
90 |
examples |
|
91 |
" |
|
92 |
[exBegin] |
|
93 |
|a b r| |
|
94 |
||
95 |
a := (FixedPoint fromString:'123.456'). |
|
3060 | 96 |
b := '1.10' asFixedPoint. |
1886 | 97 |
r := a + b. |
98 |
Transcript showCR:r. |
|
99 |
Transcript showCR:(r withScale:2). |
|
100 |
Transcript showCR:(r withScale:1). |
|
101 |
Transcript showCR:(r rounded). |
|
102 |
[exEnd] |
|
103 |
||
104 |
[exBegin] |
|
105 |
|a b r| |
|
106 |
||
107 |
a := (FixedPoint fromString:'0.9999999'). |
|
3060 | 108 |
b := 0.0000001 asFixedPoint. |
1886 | 109 |
r := a + b. |
110 |
Transcript showCR:r. |
|
111 |
Transcript showCR:(r withScale:2). |
|
112 |
Transcript showCR:(r withScale:1). |
|
113 |
Transcript showCR:(r rounded). |
|
114 |
[exEnd] |
|
115 |
||
116 |
[exBegin] |
|
117 |
|a b r| |
|
118 |
||
119 |
a := (FixedPoint fromString:'0.9999998'). |
|
120 |
b := (FixedPoint fromString:'0.0000001'). |
|
121 |
r := a + b. |
|
122 |
Transcript showCR:r. |
|
123 |
Transcript showCR:(r withScale:2). |
|
124 |
Transcript showCR:(r withScale:1). |
|
125 |
Transcript showCR:(r rounded). |
|
126 |
[exEnd] |
|
127 |
||
128 |
[exBegin] |
|
129 |
|a b r| |
|
130 |
||
131 |
a := (FixedPoint fromString:'1.0'). |
|
132 |
b := (FixedPoint fromString:'0.0000001'). |
|
133 |
r := a + b. |
|
134 |
Transcript showCR:r. |
|
135 |
Transcript showCR:(r withScale:2). |
|
136 |
Transcript showCR:(r withScale:1). |
|
137 |
Transcript showCR:(r rounded). |
|
138 |
[exEnd] |
|
139 |
||
140 |
[exBegin] |
|
141 |
|a b r| |
|
142 |
||
143 |
a := (FixedPoint fromString:'0.99'). |
|
144 |
b := (FixedPoint fromString:'0.0000001'). |
|
145 |
r := a + b. |
|
146 |
Transcript showCR:r. |
|
147 |
Transcript showCR:(r withScale:2). |
|
148 |
Transcript showCR:(r withScale:1). |
|
149 |
Transcript showCR:(r rounded). |
|
150 |
[exEnd] |
|
151 |
||
152 |
" |
|
153 |
! ! |
|
154 |
||
155 |
!FixedPoint class methodsFor:'instance creation'! |
|
156 |
||
157 |
numerator:n denominator:d scale:s |
|
158 |
^ self basicNew |
|
159 |
setNumerator:n |
|
160 |
denominator:d |
|
161 |
scale:s |
|
162 |
! |
|
163 |
||
3060 | 164 |
readFrom:aStringOrStream |
165 |
"return the next FixedPoint from the (character-)stream aStream. |
|
166 |
||
167 |
NOTICE: |
|
168 |
This behaves different from the default readFrom:, in returning |
|
169 |
0 (instead of raising an error) in case no number can be read. |
|
170 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
171 |
- depending on the upcoming ANSI standard, this may change." |
|
172 |
||
173 |
^ self readFrom:aStringOrStream onError:0 |
|
1886 | 174 |
|
3060 | 175 |
" |
176 |
FixedPoint readFrom:(ReadStream on:'foobar') |
|
177 |
FixedPoint readFrom:(ReadStream on:'foobar') onError:nil |
|
178 |
" |
|
179 |
||
180 |
"Modified: / 25.10.1997 / 15:30:29 / cg" |
|
181 |
! |
|
182 |
||
183 |
readFrom:aStringOrStream onError:exceptionBlock |
|
184 |
"return an instance of me as described on the string or stream, aStringOrStream. |
|
185 |
If an error occurs during conversion, return the result |
|
186 |
from evaluating exceptionBlock" |
|
187 |
||
188 |
| aStream sign integerPart fractionStream char fractionPart scale | |
|
189 |
||
190 |
aStream := aStringOrStream readStream. |
|
1886 | 191 |
|
192 |
aStream peek == $- ifTrue:[ |
|
193 |
sign := -1. |
|
194 |
aStream next. |
|
195 |
] ifFalse:[ |
|
196 |
sign := 1 |
|
197 |
]. |
|
198 |
||
3060 | 199 |
(aStream atEnd or:[aStream peek isLetter]) ifTrue: [^ exceptionBlock value]. |
1886 | 200 |
|
201 |
integerPart := (aStream upTo:$.) asNumber. |
|
3060 | 202 |
(aStream atEnd or: [aStream peek isLetter]) ifTrue: [ |
203 |
fractionPart := 0. |
|
204 |
scale := 1. |
|
205 |
] ifFalse:[ |
|
206 |
fractionStream := WriteStream on:(String new: 10). |
|
207 |
[ |
|
208 |
char := aStream next. |
|
209 |
char ~~ nil and:[char isDigit] |
|
210 |
] whileTrue:[ |
|
211 |
fractionStream nextPut:char |
|
212 |
]. |
|
1886 | 213 |
|
3060 | 214 |
scale := fractionStream contents size. |
215 |
fractionPart := Number readFromString:(fractionStream contents) onError:exceptionBlock. |
|
1886 | 216 |
]. |
217 |
||
218 |
^ self basicNew |
|
219 |
setNumerator:(integerPart * (10 raisedTo:scale) + fractionPart) * sign |
|
220 |
scale:scale |
|
221 |
||
222 |
" |
|
3060 | 223 |
FixedPoint readFrom:'123.456' |
224 |
FixedPoint readFrom:'-123.456' |
|
225 |
FixedPoint readFrom:'123' |
|
226 |
FixedPoint readFrom:'-123' |
|
1886 | 227 |
" |
3060 | 228 |
|
229 |
"Created: / 25.10.1997 / 15:28:59 / cg" |
|
230 |
"Modified: / 25.10.1997 / 15:31:47 / cg" |
|
1886 | 231 |
! ! |
232 |
||
233 |
!FixedPoint methodsFor:'accessing'! |
|
234 |
||
235 |
scale |
|
2538 | 236 |
"return the number of places of significance that is carried by the receiver." |
1886 | 237 |
|
238 |
^ scale |
|
2538 | 239 |
|
240 |
"Modified: 12.4.1997 / 11:21:05 / cg" |
|
1886 | 241 |
! ! |
242 |
||
243 |
!FixedPoint methodsFor:'arithmetic'! |
|
244 |
||
245 |
* aNumber |
|
246 |
"return the product of the receiver and the argument, aNumber. |
|
247 |
Redefined to care for the scale if the argument is another fixPoint number. |
|
248 |
The results scale is the maximum of the receivers scale and the arguments |
|
249 |
scale." |
|
250 |
||
251 |
|n d sMax| |
|
252 |
||
253 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
|
254 |
^ self class |
|
255 |
numerator:(numerator * aNumber) |
|
256 |
denominator:denominator |
|
257 |
scale:scale |
|
258 |
]. |
|
259 |
(aNumber isMemberOf:self class) ifTrue:[ |
|
260 |
n := numerator * aNumber numerator. |
|
261 |
d := denominator * aNumber denominator. |
|
262 |
sMax := scale max:aNumber scale. |
|
263 |
||
264 |
^ self class |
|
265 |
numerator:n |
|
266 |
denominator:d |
|
267 |
scale:sMax |
|
268 |
]. |
|
269 |
^ aNumber productFromFixedPoint:self |
|
270 |
||
271 |
" |
|
272 |
|a r| |
|
273 |
||
274 |
a := (FixedPoint fromString:'123.456'). |
|
275 |
r := a * 5. |
|
276 |
Transcript showCR:r. |
|
277 |
Transcript showCR:(r withScale:2). |
|
278 |
" |
|
279 |
||
280 |
" |
|
281 |
|a b r| |
|
282 |
||
283 |
a := (FixedPoint fromString:'123.456'). |
|
284 |
b := (FixedPoint fromString:'1.10'). |
|
285 |
r := a * b. |
|
286 |
Transcript showCR:r. |
|
287 |
Transcript showCR:(r withScale:2). |
|
288 |
" |
|
289 |
||
290 |
" |
|
291 |
|a b r| |
|
292 |
||
293 |
a := (FixedPoint fromString:'123.456'). |
|
294 |
b := (FixedPoint fromString:'-1.10'). |
|
295 |
r := a * b. |
|
296 |
Transcript showCR:r. |
|
297 |
Transcript showCR:(r withScale:2). |
|
298 |
" |
|
299 |
||
300 |
" |
|
301 |
|a b r| |
|
302 |
||
303 |
a := (FixedPoint fromString:'0.9999999'). |
|
304 |
b := (FixedPoint fromString:'0.9999999'). |
|
305 |
r := a * b. |
|
306 |
Transcript show:'fixed (exact) : '; showCR:r. |
|
307 |
Transcript show:'fixed (scale2) : '; showCR:(r withScale:2). |
|
308 |
||
309 |
Transcript show:'float (inexact): '; showCR:(0.9999999 * 0.9999999). |
|
310 |
" |
|
311 |
||
312 |
" |
|
313 |
|a b r| |
|
314 |
||
315 |
a := 1. |
|
316 |
b := (FixedPoint fromString:'0.9999999'). |
|
317 |
r := a * b. |
|
318 |
Transcript show:'fixed (exact) : '; showCR:r. |
|
319 |
Transcript show:'fixed (scale2) : '; showCR:(r withScale:2). |
|
320 |
||
321 |
Transcript show:'float (inexact): '; showCR:(0.9999999 * 0.9999999). |
|
322 |
" |
|
323 |
||
324 |
||
325 |
! |
|
326 |
||
327 |
+ aNumber |
|
328 |
"return the sum of the receiver and the argument, aNumber. |
|
329 |
Redefined to care for the scale if the argument is another fixPoint number. |
|
330 |
The resulting scale will be the maximum of the receivers and the |
|
331 |
arguments scale." |
|
332 |
||
333 |
|n d sMax| |
|
334 |
||
335 |
(aNumber isMemberOf:self class) ifTrue:[ |
|
336 |
n := aNumber numerator. |
|
337 |
d := aNumber denominator. |
|
338 |
sMax := scale max:aNumber scale. |
|
339 |
||
340 |
"save a multiplication if possible" |
|
341 |
denominator == d ifTrue:[ |
|
342 |
^ self class |
|
343 |
numerator:(numerator + n) |
|
344 |
denominator:d |
|
345 |
scale:sMax |
|
346 |
]. |
|
347 |
||
348 |
^ self class |
|
349 |
numerator:((numerator * d) + (n * denominator)) |
|
350 |
denominator:(denominator * d) |
|
351 |
scale:sMax |
|
352 |
]. |
|
353 |
^ aNumber sumFromFixedPoint:self |
|
354 |
||
355 |
" |
|
356 |
|a b| |
|
357 |
||
358 |
a := (FixedPoint fromString:'123.456'). |
|
359 |
b := (FixedPoint fromString:'1.10'). |
|
360 |
a + b |
|
361 |
" |
|
362 |
||
363 |
" |
|
364 |
|a b| |
|
365 |
||
366 |
a := (FixedPoint fromString:'0.9999999'). |
|
367 |
b := (FixedPoint fromString:'0.0000001'). |
|
368 |
a + b |
|
369 |
" |
|
370 |
||
371 |
" |
|
372 |
|a b| |
|
373 |
||
374 |
a := (FixedPoint fromString:'0.99'). |
|
375 |
b := (FixedPoint fromString:'0.0000001'). |
|
376 |
a + b |
|
377 |
" |
|
378 |
||
379 |
" |
|
380 |
|a b| |
|
381 |
||
382 |
a := (FixedPoint fromString:'0.99'). |
|
383 |
b := (FixedPoint fromString:'0.0000001'). |
|
384 |
(a + b) withScale:2 |
|
385 |
" |
|
386 |
||
387 |
" |
|
388 |
|a b| |
|
389 |
||
390 |
a := (FixedPoint fromString:'0.99'). |
|
391 |
b := (FixedPoint fromString:'0.0000001'). |
|
392 |
(a + b) withScale:1 |
|
393 |
" |
|
394 |
||
395 |
" |
|
396 |
|a b| |
|
397 |
||
398 |
a := 1. |
|
399 |
b := (FixedPoint fromString:'0.0000001'). |
|
400 |
Transcript showCR:((a + b) withScale:1). |
|
401 |
Transcript showCR:(a + b) |
|
402 |
" |
|
403 |
||
404 |
||
405 |
! |
|
406 |
||
407 |
- aNumber |
|
408 |
"return the difference of the receiver and the argument, aNumber. |
|
409 |
Redefined to care for the scale if the argument is another fixPoint number. |
|
410 |
The results scale is the maximum of the receivers scale and the arguments |
|
411 |
scale." |
|
412 |
||
413 |
|n d sMax| |
|
414 |
||
415 |
(aNumber isMemberOf:self class) ifTrue:[ |
|
416 |
n := aNumber numerator. |
|
417 |
d := aNumber denominator. |
|
418 |
sMax := scale max:aNumber scale. |
|
419 |
||
420 |
"save a multiplication if possible" |
|
421 |
denominator == d ifTrue:[ |
|
422 |
^ self class |
|
423 |
numerator:(numerator - n) |
|
424 |
denominator:d |
|
425 |
scale:sMax |
|
426 |
]. |
|
427 |
||
428 |
^ self class |
|
429 |
numerator:((numerator * d) - (n * denominator)) |
|
430 |
denominator:(denominator * d) |
|
431 |
scale:sMax |
|
432 |
]. |
|
433 |
^ aNumber differenceFromFixedPoint:self |
|
434 |
||
435 |
" |
|
436 |
|a b| |
|
437 |
||
438 |
a := (FixedPoint fromString:'123.456'). |
|
439 |
b := (FixedPoint fromString:'1.10'). |
|
440 |
a - b |
|
441 |
" |
|
442 |
||
443 |
" |
|
444 |
|a b| |
|
445 |
||
446 |
a := (FixedPoint fromString:'0.9999999'). |
|
447 |
b := (FixedPoint fromString:'0.0000009'). |
|
448 |
a - b |
|
449 |
" |
|
450 |
||
451 |
" |
|
452 |
|a b| |
|
453 |
||
454 |
a := (FixedPoint fromString:'0.99'). |
|
455 |
b := (FixedPoint fromString:'0.0000001'). |
|
456 |
a - b |
|
457 |
" |
|
458 |
||
459 |
" |
|
460 |
|a b| |
|
461 |
||
462 |
a := (FixedPoint fromString:'0.99'). |
|
463 |
b := (FixedPoint fromString:'0.0000001'). |
|
464 |
(a - b) withScale:2 |
|
465 |
" |
|
466 |
||
467 |
" |
|
468 |
|a b| |
|
469 |
||
470 |
a := (FixedPoint fromString:'0.99'). |
|
471 |
b := (FixedPoint fromString:'0.0000001'). |
|
472 |
(a - b) withScale:1 |
|
473 |
" |
|
474 |
||
475 |
" |
|
476 |
|a b| |
|
477 |
||
478 |
a := (FixedPoint fromString:'0.0000001'). |
|
479 |
b := (FixedPoint fromString:'0.99'). |
|
480 |
(a - b) withScale:2 |
|
481 |
" |
|
482 |
||
483 |
" |
|
484 |
|a b| |
|
485 |
||
486 |
a := 1. |
|
487 |
b := (FixedPoint fromString:'0.0000001'). |
|
488 |
Transcript showCR:((a - b) withScale:1). |
|
489 |
Transcript showCR:(a - b) |
|
490 |
" |
|
491 |
||
492 |
! |
|
493 |
||
494 |
/ aNumber |
|
1898 | 495 |
"return the quotient of the receiver and the argument, aNumber. |
1886 | 496 |
Redefined to care for the scale if the argument is another fixPoint number. |
497 |
The results scale is the maximum of the receivers scale and the arguments |
|
498 |
scale." |
|
499 |
||
500 |
|n d sMax| |
|
501 |
||
502 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
|
503 |
^ (self class |
|
504 |
numerator:numerator |
|
505 |
denominator:(denominator * aNumber) |
|
506 |
scale:scale) reduced |
|
507 |
]. |
|
508 |
||
509 |
(aNumber isMemberOf:self class) ifTrue:[ |
|
510 |
n := numerator * aNumber denominator. |
|
511 |
d := denominator * aNumber numerator. |
|
512 |
sMax := scale max:aNumber scale. |
|
513 |
||
514 |
^ (self class |
|
515 |
numerator:n |
|
516 |
denominator:d |
|
517 |
scale:sMax) reduced |
|
518 |
]. |
|
519 |
^ aNumber quotientFromFixedPoint:self |
|
520 |
||
521 |
" |
|
522 |
|a r| |
|
523 |
||
524 |
a := (FixedPoint fromString:'123.456'). |
|
525 |
r := a / 5. |
|
526 |
Transcript showCR:r. |
|
527 |
Transcript showCR:(r withScale:2). |
|
528 |
Transcript showCR:(r withScale:9). |
|
529 |
" |
|
530 |
||
531 |
" |
|
532 |
|a b r| |
|
533 |
||
534 |
a := (FixedPoint fromString:'123.456'). |
|
535 |
b := (FixedPoint fromString:'1.10'). |
|
536 |
r := a / b. |
|
537 |
Transcript showCR:r. |
|
538 |
Transcript showCR:(r withScale:2). |
|
539 |
" |
|
540 |
||
541 |
" |
|
542 |
|a b r| |
|
543 |
||
544 |
a := (FixedPoint fromString:'-123.456'). |
|
545 |
b := (FixedPoint fromString:'-1.10'). |
|
546 |
r := a / b. |
|
547 |
Transcript showCR:r. |
|
548 |
Transcript showCR:(r withScale:2). |
|
549 |
" |
|
550 |
||
551 |
" |
|
552 |
|a b r| |
|
553 |
||
554 |
a := (FixedPoint fromString:'123.456'). |
|
555 |
b := (FixedPoint fromString:'-1.10'). |
|
556 |
r := a / b. |
|
557 |
Transcript showCR:r. |
|
558 |
Transcript showCR:(r withScale:2). |
|
559 |
" |
|
560 |
||
561 |
" |
|
562 |
|a b r| |
|
563 |
||
564 |
a := 1. |
|
565 |
b := (FixedPoint fromString:'0.9999999'). |
|
566 |
r := a / b. |
|
567 |
Transcript show:'fixed (exact) : '; showCR:r. |
|
568 |
Transcript show:'fixed (scale2) : '; showCR:(r withScale:2). |
|
569 |
||
570 |
Transcript show:'float (inexact): '; showCR:(1 / 0.9999999). |
|
571 |
" |
|
572 |
! ! |
|
573 |
||
574 |
!FixedPoint methodsFor:'coercing & converting'! |
|
575 |
||
576 |
asFixedPoint |
|
2140 | 577 |
"return the receiver as a fixedPoint number - thats the receiver itself" |
1886 | 578 |
|
579 |
^ self |
|
580 |
||
2140 | 581 |
"Modified: 10.1.1997 / 19:53:14 / cg" |
582 |
! |
|
583 |
||
584 |
asFixedPoint:newScale |
|
2538 | 585 |
"return a copy of the receiver, with newScale number of post-decimal |
2140 | 586 |
digits" |
587 |
||
588 |
^ self class |
|
589 |
numerator:numerator |
|
590 |
denominator:denominator |
|
591 |
scale:newScale |
|
592 |
||
593 |
" |
|
594 |
(FixedPoint fromString:'12345.12345') asFixedPoint:2 |
|
595 |
||
596 |
((FixedPoint fromString:'0.33333333') |
|
597 |
+ |
|
598 |
(FixedPoint fromString:'0.33333333') |
|
599 |
) asFixedPoint:2 |
|
600 |
" |
|
601 |
||
2538 | 602 |
"Modified: 12.4.1997 / 11:20:37 / cg" |
1886 | 603 |
! |
604 |
||
605 |
asFraction |
|
606 |
"return the receiver as a fraction" |
|
607 |
||
608 |
^ (Fraction |
|
609 |
numerator:numerator |
|
610 |
denominator:denominator) reduced |
|
611 |
||
612 |
" |
|
613 |
(FixedPoint fromString:'0.2') |
|
614 |
(FixedPoint fromString:'0.2') asFraction |
|
615 |
(FixedPoint fromString:'0.2') asFloat |
|
616 |
(FixedPoint fromString:'0.2') asShortFloat |
|
617 |
(FixedPoint fromString:'0.2') asInteger |
|
618 |
" |
|
619 |
! |
|
620 |
||
621 |
coerce:aNumber |
|
622 |
"return aNumber converted into receivers type" |
|
623 |
||
624 |
^ aNumber asFixedPoint |
|
625 |
||
626 |
! |
|
627 |
||
628 |
generality |
|
629 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
630 |
||
631 |
^ 65 |
|
632 |
||
633 |
" |
|
634 |
(FixedPoint fromString:'1.001') + 1 |
|
635 |
(FixedPoint fromString:'1.001') + 1.0 |
|
636 |
(FixedPoint fromString:'1.001') + (1/2) |
|
637 |
(FixedPoint fromString:'1.001') + 1.0 asShortFloat |
|
638 |
(FixedPoint fromString:'1.001') + 1 asLargeInteger |
|
639 |
||
640 |
1 + (FixedPoint fromString:'1.001') |
|
1892 | 641 |
1.0 + (FixedPoint fromString:'1.001') |
1891 | 642 |
(1/2) + (FixedPoint fromString:'1.001') |
1886 | 643 |
1.0 asShortFloat + (FixedPoint fromString:'1.001') |
644 |
1 asLargeInteger + (FixedPoint fromString:'1.001') |
|
645 |
" |
|
2140 | 646 |
! |
647 |
||
648 |
withScale:newScale |
|
2538 | 649 |
"return a copy of the receiver, with newScale number of post-decimal |
2140 | 650 |
digits" |
651 |
||
652 |
^ self asFixedPoint:newScale |
|
653 |
||
654 |
" |
|
655 |
(FixedPoint fromString:'12345.12345') withScale:2 |
|
656 |
||
657 |
((FixedPoint fromString:'0.33333333') |
|
658 |
+ |
|
659 |
(FixedPoint fromString:'0.33333333') |
|
660 |
) withScale:2 |
|
661 |
" |
|
662 |
||
2538 | 663 |
"Modified: 12.4.1997 / 11:22:09 / cg" |
1886 | 664 |
! ! |
665 |
||
666 |
!FixedPoint methodsFor:'double dispatching'! |
|
667 |
||
668 |
differenceFromInteger:anInteger |
|
669 |
"sent when an integer does not know how to subtract the receiver. |
|
670 |
Redefined here to preserve the scale." |
|
671 |
||
672 |
^ (self class |
|
673 |
numerator:((anInteger * denominator) - numerator) |
|
674 |
denominator:denominator |
|
675 |
scale:scale) reduced |
|
676 |
||
677 |
! |
|
678 |
||
679 |
productFromInteger:anInteger |
|
680 |
"sent when an integer does not know how to multiply the receiver. |
|
681 |
Redefined here to preserve the scale." |
|
682 |
||
683 |
^ (self class |
|
684 |
numerator:(anInteger * numerator) |
|
685 |
denominator:denominator |
|
686 |
scale:scale) reduced |
|
687 |
||
688 |
"Modified: 5.11.1996 / 10:32:28 / cg" |
|
689 |
||
690 |
||
691 |
! |
|
692 |
||
693 |
quotientFromInteger:anInteger |
|
694 |
"sent when an integer does not know how to divide by the receiver. |
|
695 |
Redefined here to preserve the scale." |
|
696 |
||
697 |
^ (self class |
|
698 |
numerator:(anInteger * denominator) |
|
699 |
denominator:numerator |
|
700 |
scale:scale) reduced |
|
701 |
||
702 |
"Modified: 5.11.1996 / 10:32:35 / cg" |
|
703 |
||
704 |
||
705 |
! |
|
706 |
||
707 |
sumFromInteger:anInteger |
|
708 |
"sent when an integer does not know how to add the receiver. |
|
709 |
Redefined here to preserve the scale." |
|
710 |
||
711 |
^ (self class |
|
712 |
numerator:(numerator + (anInteger * denominator)) |
|
713 |
denominator:denominator |
|
714 |
scale:scale) reduced |
|
715 |
||
716 |
"Modified: 5.11.1996 / 10:32:43 / cg" |
|
717 |
||
718 |
||
719 |
! ! |
|
720 |
||
5570 | 721 |
!FixedPoint methodsFor:'printing & storing'! |
1886 | 722 |
|
723 |
printOn: aStream |
|
2538 | 724 |
"append to the argument, aStream, a printed representation of the receiver. |
1886 | 725 |
For printout, only scale post-decimal digits are printed |
726 |
(and the printout is rounded to that many digits)" |
|
727 |
||
2563 | 728 |
|e integerPart fractionPart negative num| |
1886 | 729 |
|
730 |
numerator < 0 ifTrue:[ |
|
731 |
negative := true. |
|
732 |
num := numerator negated. |
|
733 |
] ifFalse:[ |
|
734 |
negative := false. |
|
735 |
num := numerator. |
|
736 |
]. |
|
737 |
integerPart := (num // denominator). |
|
738 |
e := 10 raisedTo:scale. |
|
739 |
fractionPart := (num \\ denominator). |
|
740 |
||
741 |
"/ the most common case is a denominator fitting the scale |
|
742 |
"/ (fixedPoint numbers are created this way) |
|
743 |
||
744 |
e == denominator ifFalse:[ |
|
745 |
fractionPart := fractionPart * (e * 10) // denominator. |
|
746 |
fractionPart := (fractionPart roundTo:10) // 10. |
|
747 |
||
748 |
fractionPart >= e ifTrue:[ |
|
749 |
integerPart := integerPart + 1. |
|
750 |
fractionPart := 0. |
|
751 |
] |
|
752 |
]. |
|
753 |
||
754 |
"/ |
|
755 |
"/ add a 1000..., so we can (mis-)use integer-printString ... |
|
756 |
"/ the highest-1 will be cutoff after padding. |
|
757 |
"/ |
|
758 |
fractionPart := e + fractionPart. |
|
759 |
||
760 |
negative ifTrue:[ |
|
761 |
aStream nextPut:$- |
|
762 |
]. |
|
763 |
integerPart printOn:aStream. |
|
764 |
aStream nextPut: $.. |
|
765 |
((fractionPart printStringPaddedTo:scale with:$0) copyFrom:2) printOn:aStream |
|
766 |
||
767 |
" |
|
768 |
(FixedPoint fromString:'0.66666666') |
|
769 |
(FixedPoint fromString:'0.66666666') withScale:2 |
|
770 |
(FixedPoint fromString:'0.99999999') |
|
771 |
(FixedPoint fromString:'0.99999999') withScale:2 |
|
772 |
(FixedPoint fromString:'1.00000001') |
|
773 |
(FixedPoint fromString:'1.00000001') withScale:2 |
|
774 |
(FixedPoint fromString:'1.005') |
|
775 |
(FixedPoint fromString:'1.005') withScale:2 |
|
776 |
(FixedPoint fromString:'1.005') withScale:1 |
|
777 |
||
778 |
(FixedPoint fromString:'-0.66666666') |
|
779 |
(FixedPoint fromString:'-0.66666666') withScale:2 |
|
780 |
(FixedPoint fromString:'-0.99999999') |
|
781 |
(FixedPoint fromString:'-0.99999999') withScale:2 |
|
782 |
(FixedPoint fromString:'-1.00000001') |
|
783 |
(FixedPoint fromString:'-1.00000001') withScale:2 |
|
784 |
(FixedPoint fromString:'-1.005') |
|
785 |
(FixedPoint fromString:'-1.005') withScale:2 |
|
786 |
(FixedPoint fromString:'-1.005') withScale:1 |
|
787 |
(FixedPoint fromString:'-1.05') |
|
788 |
(FixedPoint fromString:'-1.05') withScale:2 |
|
789 |
(FixedPoint fromString:'-1.05') withScale:1 |
|
790 |
(FixedPoint fromString:'-1.04') |
|
791 |
(FixedPoint fromString:'-1.04') withScale:2 |
|
792 |
(FixedPoint fromString:'-1.04') withScale:1 |
|
793 |
" |
|
794 |
||
795 |
" |
|
796 |
|a b r| |
|
797 |
||
798 |
a := (FixedPoint fromString:'0.66666666') withScale:2. |
|
799 |
b := (FixedPoint fromString:'0.33333333'). |
|
800 |
r := (a + b) withScale:4. |
|
801 |
Transcript show:'printout with scale of 4 :'; showCR:r. |
|
802 |
Transcript show:'more precise value :'; showCR:(r withScale:8) |
|
803 |
" |
|
2538 | 804 |
|
805 |
"Modified: 12.4.1997 / 11:20:51 / cg" |
|
1886 | 806 |
! ! |
807 |
||
808 |
!FixedPoint methodsFor:'private'! |
|
809 |
||
810 |
reduced |
|
811 |
|gc| |
|
812 |
||
813 |
scale isNil ifTrue:[ |
|
814 |
"/ to catch inherited Fraction reduce calls |
|
815 |
self error:'should not happen'. |
|
816 |
scale := 3 |
|
817 |
]. |
|
818 |
||
819 |
denominator == 1 ifTrue:[^ numerator]. |
|
820 |
numerator == 1 ifTrue:[^ self]. |
|
821 |
numerator == 0 ifTrue:[^ 0]. |
|
822 |
||
823 |
gc := numerator gcd:denominator. |
|
824 |
gc := gc gcd:(10 raisedTo:scale). |
|
825 |
||
826 |
(gc == 1) ifFalse:[ |
|
827 |
numerator := numerator // gc. |
|
828 |
denominator := denominator // gc |
|
829 |
]. |
|
830 |
||
831 |
(numerator < 0) ifTrue:[ |
|
832 |
(denominator < 0) ifTrue:[ |
|
833 |
numerator := numerator negated. |
|
834 |
denominator := denominator negated |
|
835 |
] |
|
836 |
]. |
|
837 |
(denominator == 1) ifTrue:[^ numerator]. |
|
838 |
^ self |
|
839 |
||
840 |
! |
|
841 |
||
3731
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
842 |
scale:newScale |
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
843 |
"set the scale." |
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
844 |
|
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
845 |
scale := newScale. |
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
846 |
|
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
847 |
"Modified: / 12.4.1997 / 11:22:02 / cg" |
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
848 |
"Created: / 5.8.1998 / 13:28:49 / cg" |
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
849 |
! |
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
850 |
|
1886 | 851 |
setNumerator:nInteger denominator:d scale:s |
2538 | 852 |
"initialize the instance variables. |
853 |
Assumes that the fraction as specified by numerator and denominator |
|
854 |
is already reduced." |
|
1886 | 855 |
|
856 |
scale := s. |
|
857 |
super |
|
858 |
setNumerator:nInteger |
|
859 |
denominator:d |
|
2538 | 860 |
|
861 |
"Modified: 12.4.1997 / 11:21:47 / cg" |
|
1886 | 862 |
! |
863 |
||
864 |
setNumerator:nInteger scale:s |
|
2538 | 865 |
"initialize the instance variables. |
866 |
Assumes that the fraction as specified by numerator and denominator |
|
867 |
is already reduced." |
|
1886 | 868 |
|
869 |
scale := s. |
|
870 |
super |
|
871 |
setNumerator:nInteger |
|
872 |
denominator:(10 raisedTo:s) |
|
2538 | 873 |
|
874 |
"Modified: 12.4.1997 / 11:21:55 / cg" |
|
1886 | 875 |
! |
876 |
||
877 |
setScale:newScale |
|
2538 | 878 |
"initialize the scale instance variables." |
1886 | 879 |
|
880 |
scale := newScale. |
|
2538 | 881 |
|
882 |
"Modified: 12.4.1997 / 11:22:02 / cg" |
|
1886 | 883 |
! ! |
884 |
||
1891 | 885 |
!FixedPoint methodsFor:'queries'! |
886 |
||
887 |
isFixedPoint |
|
888 |
"return true, if the receiver is some kind of fixedPoint number; |
|
889 |
true is returned here - the method is redefined from Object." |
|
890 |
||
891 |
^ true |
|
892 |
||
893 |
||
894 |
! ! |
|
895 |
||
1886 | 896 |
!FixedPoint class methodsFor:'documentation'! |
897 |
||
898 |
version |
|
5570 | 899 |
^ '$Header: /cvs/stx/stx/libbasic/FixedPoint.st,v 1.11 2000-08-31 10:02:43 cg Exp $' |
1886 | 900 |
! ! |