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