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