author | Claus Gittinger <cg@exept.de> |
Mon, 31 Mar 2003 11:53:22 +0200 | |
changeset 7142 | 7a6297c86ef4 |
parent 7053 | 13e04c48e23c |
child 7355 | 96f466eeddf5 |
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 |
6895 | 18 |
instanceVariableNames:'scale' |
19 |
classVariableNames:'PI_1000' |
|
20 |
poolDictionaries:'' |
|
21 |
category:'Magnitude-Numbers' |
|
1886 | 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, |
|
6704 | 32 |
which shouldn''t be too much a problem for financial types.' |
6237
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 |
|
6894
64f27ed2753f
do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents:
6886
diff
changeset
|
169 |
setNumerator:n denominator:d scale:s |
64f27ed2753f
do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents:
6886
diff
changeset
|
170 |
"/ ; reduced |
1886 | 171 |
! |
172 |
||
3060 | 173 |
readFrom:aStringOrStream |
174 |
"return the next FixedPoint from the (character-)stream aStream. |
|
175 |
||
176 |
NOTICE: |
|
177 |
This behaves different from the default readFrom:, in returning |
|
178 |
0 (instead of raising an error) in case no number can be read. |
|
179 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
180 |
- depending on the upcoming ANSI standard, this may change." |
|
181 |
||
182 |
^ self readFrom:aStringOrStream onError:0 |
|
1886 | 183 |
|
3060 | 184 |
" |
6636
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
185 |
FixedPoint readFrom:'123.456' |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
186 |
FixedPoint readFrom:'3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788' |
3060 | 187 |
FixedPoint readFrom:(ReadStream on:'foobar') |
188 |
FixedPoint readFrom:(ReadStream on:'foobar') onError:nil |
|
189 |
" |
|
190 |
||
191 |
"Modified: / 25.10.1997 / 15:30:29 / cg" |
|
6895 | 192 |
! |
193 |
||
194 |
readFrom:aStringOrStream onError:exceptionBlock |
|
195 |
"return an instance of me as described on the string or stream, aStringOrStream. |
|
196 |
If an error occurs during conversion, return the result |
|
197 |
from evaluating exceptionBlock" |
|
198 |
||
199 |
| aStream sign integerPart fractionStream char fractionPart scale | |
|
200 |
||
201 |
aStream := aStringOrStream readStream. |
|
202 |
||
203 |
aStream peek == $- ifTrue:[ |
|
204 |
sign := -1. |
|
205 |
aStream next. |
|
206 |
] ifFalse:[ |
|
207 |
sign := 1 |
|
208 |
]. |
|
209 |
||
210 |
(aStream atEnd or:[aStream peek isLetter]) ifTrue: [^ exceptionBlock value]. |
|
211 |
||
212 |
integerPart := (aStream upTo:$.) asNumber. |
|
213 |
(aStream atEnd or: [aStream peek isLetter]) ifTrue: [ |
|
214 |
fractionPart := 0. |
|
215 |
scale := 1. |
|
216 |
] ifFalse:[ |
|
217 |
fractionStream := ReadWriteStream on:(String new: 10). |
|
218 |
[ |
|
219 |
char := aStream next. |
|
220 |
char ~~ nil and:[char isDigit] |
|
221 |
] whileTrue:[ |
|
222 |
fractionStream nextPut:char |
|
223 |
]. |
|
224 |
||
7053
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
6898
diff
changeset
|
225 |
scale := fractionStream position0Based. |
6895 | 226 |
fractionStream reset. |
227 |
fractionPart := Number readFrom:fractionStream. |
|
228 |
]. |
|
229 |
||
230 |
^ self basicNew |
|
231 |
setNumerator:(integerPart * (10 raisedTo:scale) + fractionPart) * sign |
|
232 |
scale:scale |
|
233 |
||
234 |
" |
|
235 |
FixedPoint readFrom:'1.00' |
|
7053
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
6898
diff
changeset
|
236 |
FixedPoint readFrom:'123.456' |
6895 | 237 |
FixedPoint readFrom:'-123.456' |
238 |
FixedPoint readFrom:'123' |
|
239 |
FixedPoint readFrom:'-123' |
|
240 |
FixedPoint readFrom:'-123.abcd' onError:[47.5] |
|
241 |
FixedPoint readFrom:'-1a.bcd' onError:[47.5] |
|
242 |
FixedPoint readFrom:'foot' onError:['bad fixedpoint'] |
|
243 |
" |
|
244 |
||
245 |
"Created: / 25.10.1997 / 15:28:59 / cg" |
|
246 |
"Modified: / 25.10.1997 / 15:31:47 / cg" |
|
1886 | 247 |
! ! |
248 |
||
6636
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
249 |
!FixedPoint class methodsFor:'constants'! |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
250 |
|
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
251 |
pi |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
252 |
"pi with 1000 valid digits..." |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
253 |
|
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
254 |
PI_1000 isNil ifTrue:[ |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
255 |
PI_1000 := self |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
256 |
readFrom:'3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788' |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
257 |
]. |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
258 |
^ PI_1000. |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
259 |
|
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
260 |
" |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
261 |
self pi squared |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
262 |
self pi reciprocal |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
263 |
1 / self pi |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
264 |
self pi * 1000000000000000000000000000000000000000000 |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
265 |
" |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
266 |
! ! |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
267 |
|
6898 | 268 |
!FixedPoint class methodsFor:'queries'! |
269 |
||
270 |
exponentCharacter |
|
271 |
^ $s |
|
272 |
! ! |
|
273 |
||
1886 | 274 |
!FixedPoint methodsFor:'accessing'! |
275 |
||
276 |
scale |
|
2538 | 277 |
"return the number of places of significance that is carried by the receiver." |
1886 | 278 |
|
279 |
^ scale |
|
2538 | 280 |
|
281 |
"Modified: 12.4.1997 / 11:21:05 / cg" |
|
1886 | 282 |
! ! |
283 |
||
284 |
!FixedPoint methodsFor:'arithmetic'! |
|
285 |
||
286 |
* aNumber |
|
287 |
"return the product of the receiver and the argument, aNumber. |
|
288 |
Redefined to care for the scale if the argument is another fixPoint number. |
|
289 |
The results scale is the maximum of the receivers scale and the arguments |
|
290 |
scale." |
|
291 |
||
6636
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
292 |
aNumber isInteger ifTrue:[ |
1886 | 293 |
^ self class |
294 |
numerator:(numerator * aNumber) |
|
295 |
denominator:denominator |
|
296 |
scale:scale |
|
297 |
]. |
|
6886 | 298 |
aNumber isFraction ifTrue:[ |
1886 | 299 |
^ self class |
6886 | 300 |
numerator:(numerator * aNumber numerator) |
301 |
denominator:(denominator * aNumber denominator) |
|
6645
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
302 |
scale:(scale max:aNumber scale) |
1886 | 303 |
]. |
304 |
^ aNumber productFromFixedPoint:self |
|
305 |
||
306 |
" |
|
307 |
|a r| |
|
308 |
||
309 |
a := (FixedPoint fromString:'123.456'). |
|
310 |
r := a * 5. |
|
311 |
Transcript showCR:r. |
|
312 |
Transcript showCR:(r withScale:2). |
|
313 |
" |
|
314 |
||
315 |
" |
|
316 |
|a b r| |
|
317 |
||
318 |
a := (FixedPoint fromString:'123.456'). |
|
319 |
b := (FixedPoint fromString:'1.10'). |
|
320 |
r := a * b. |
|
321 |
Transcript showCR:r. |
|
322 |
Transcript showCR:(r withScale:2). |
|
323 |
" |
|
324 |
||
325 |
" |
|
326 |
|a b r| |
|
327 |
||
328 |
a := (FixedPoint fromString:'123.456'). |
|
329 |
b := (FixedPoint fromString:'-1.10'). |
|
330 |
r := a * b. |
|
331 |
Transcript showCR:r. |
|
332 |
Transcript showCR:(r withScale:2). |
|
333 |
" |
|
334 |
||
335 |
" |
|
336 |
|a b r| |
|
337 |
||
338 |
a := (FixedPoint fromString:'0.9999999'). |
|
339 |
b := (FixedPoint fromString:'0.9999999'). |
|
340 |
r := a * b. |
|
341 |
Transcript show:'fixed (exact) : '; showCR:r. |
|
342 |
Transcript show:'fixed (scale2) : '; showCR:(r withScale:2). |
|
343 |
||
344 |
Transcript show:'float (inexact): '; showCR:(0.9999999 * 0.9999999). |
|
345 |
" |
|
346 |
||
347 |
" |
|
348 |
|a b r| |
|
349 |
||
350 |
a := 1. |
|
351 |
b := (FixedPoint fromString:'0.9999999'). |
|
352 |
r := a * b. |
|
353 |
Transcript show:'fixed (exact) : '; showCR:r. |
|
354 |
Transcript show:'fixed (scale2) : '; showCR:(r withScale:2). |
|
355 |
||
356 |
Transcript show:'float (inexact): '; showCR:(0.9999999 * 0.9999999). |
|
357 |
" |
|
6636
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
358 |
|
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
359 |
" |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
360 |
|a r| |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
361 |
|
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
362 |
a := (FixedPoint fromString:'123.456'). |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
363 |
r := a * 5.0. |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
364 |
Transcript showCR:r. |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
365 |
Transcript showCR:(r withScale:2). |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
366 |
" |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
367 |
|
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
368 |
" |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
369 |
|a r| |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
370 |
|
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
371 |
a := (FixedPoint fromString:'123.456'). |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
372 |
r := 5.0 * a. |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
373 |
Transcript showCR:r. |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
374 |
Transcript showCR:(r withScale:2). |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
375 |
" |
1886 | 376 |
! |
377 |
||
378 |
+ aNumber |
|
379 |
"return the sum of the receiver and the argument, aNumber. |
|
380 |
Redefined to care for the scale if the argument is another fixPoint number. |
|
6894
64f27ed2753f
do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents:
6886
diff
changeset
|
381 |
The results scale will be the maximum of the receivers and the |
1886 | 382 |
arguments scale." |
383 |
||
6636
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
384 |
|n d| |
1886 | 385 |
|
6704 | 386 |
(aNumber isInteger or:[aNumber isFraction]) ifTrue:[ |
1886 | 387 |
n := aNumber numerator. |
388 |
d := aNumber denominator. |
|
389 |
||
390 |
"save a multiplication if possible" |
|
391 |
denominator == d ifTrue:[ |
|
392 |
^ self class |
|
393 |
numerator:(numerator + n) |
|
394 |
denominator:d |
|
6636
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
395 |
scale:scale |
1886 | 396 |
]. |
397 |
||
398 |
^ self class |
|
399 |
numerator:((numerator * d) + (n * denominator)) |
|
400 |
denominator:(denominator * d) |
|
6636
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
401 |
scale:scale |
1886 | 402 |
]. |
6645
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
403 |
aNumber isFixedPoint ifTrue:[ |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
404 |
n := aNumber numerator. |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
405 |
d := aNumber denominator. |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
406 |
|
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
407 |
"save a multiplication if possible" |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
408 |
denominator == d ifTrue:[ |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
409 |
n := numerator + n |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
410 |
] ifFalse:[ |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
411 |
n := (numerator * d) + (n * denominator). |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
412 |
d := denominator * d. |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
413 |
]. |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
414 |
^ self class |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
415 |
numerator:n |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
416 |
denominator:d |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
417 |
scale:(scale max:aNumber scale) |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
418 |
]. |
1886 | 419 |
^ aNumber sumFromFixedPoint:self |
420 |
||
421 |
" |
|
422 |
|a b| |
|
423 |
||
424 |
a := (FixedPoint fromString:'123.456'). |
|
425 |
b := (FixedPoint fromString:'1.10'). |
|
426 |
a + b |
|
427 |
" |
|
428 |
||
429 |
" |
|
430 |
|a b| |
|
431 |
||
432 |
a := (FixedPoint fromString:'0.9999999'). |
|
433 |
b := (FixedPoint fromString:'0.0000001'). |
|
434 |
a + b |
|
435 |
" |
|
436 |
||
437 |
" |
|
438 |
|a b| |
|
439 |
||
440 |
a := (FixedPoint fromString:'0.99'). |
|
441 |
b := (FixedPoint fromString:'0.0000001'). |
|
442 |
a + b |
|
443 |
" |
|
444 |
||
445 |
" |
|
446 |
|a b| |
|
447 |
||
448 |
a := (FixedPoint fromString:'0.99'). |
|
449 |
b := (FixedPoint fromString:'0.0000001'). |
|
450 |
(a + b) withScale:2 |
|
451 |
" |
|
452 |
||
453 |
" |
|
454 |
|a b| |
|
455 |
||
456 |
a := (FixedPoint fromString:'0.99'). |
|
457 |
b := (FixedPoint fromString:'0.0000001'). |
|
458 |
(a + b) withScale:1 |
|
459 |
" |
|
460 |
||
461 |
" |
|
462 |
|a b| |
|
463 |
||
464 |
a := 1. |
|
465 |
b := (FixedPoint fromString:'0.0000001'). |
|
466 |
Transcript showCR:((a + b) withScale:1). |
|
467 |
Transcript showCR:(a + b) |
|
468 |
" |
|
469 |
! |
|
470 |
||
471 |
- aNumber |
|
472 |
"return the difference of the receiver and the argument, aNumber. |
|
473 |
Redefined to care for the scale if the argument is another fixPoint number. |
|
474 |
The results scale is the maximum of the receivers scale and the arguments |
|
475 |
scale." |
|
476 |
||
6636
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
477 |
|n d| |
1886 | 478 |
|
6704 | 479 |
(aNumber isInteger or:[aNumber isFraction]) ifTrue:[ |
1886 | 480 |
n := aNumber numerator. |
481 |
d := aNumber denominator. |
|
482 |
||
483 |
"save a multiplication if possible" |
|
484 |
denominator == d ifTrue:[ |
|
485 |
^ self class |
|
486 |
numerator:(numerator - n) |
|
487 |
denominator:d |
|
6636
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
488 |
scale:scale |
1886 | 489 |
]. |
490 |
||
491 |
^ self class |
|
492 |
numerator:((numerator * d) - (n * denominator)) |
|
493 |
denominator:(denominator * d) |
|
6636
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
494 |
scale:scale |
1886 | 495 |
]. |
6645
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
496 |
aNumber isFixedPoint ifTrue:[ |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
497 |
n := aNumber numerator. |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
498 |
d := aNumber denominator. |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
499 |
|
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
500 |
"save a multiplication if possible" |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
501 |
denominator == d ifTrue:[ |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
502 |
n := numerator - n |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
503 |
] ifFalse:[ |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
504 |
n := (numerator * d) - (n * denominator). |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
505 |
d := denominator * d |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
506 |
]. |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
507 |
^ self class |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
508 |
numerator:n |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
509 |
denominator:d |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
510 |
scale:(scale max:aNumber scale) |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
511 |
]. |
1886 | 512 |
^ aNumber differenceFromFixedPoint:self |
513 |
||
514 |
" |
|
515 |
|a b| |
|
516 |
||
517 |
a := (FixedPoint fromString:'123.456'). |
|
518 |
b := (FixedPoint fromString:'1.10'). |
|
519 |
a - b |
|
520 |
" |
|
521 |
||
522 |
" |
|
523 |
|a b| |
|
524 |
||
525 |
a := (FixedPoint fromString:'0.9999999'). |
|
526 |
b := (FixedPoint fromString:'0.0000009'). |
|
527 |
a - b |
|
528 |
" |
|
529 |
||
530 |
" |
|
531 |
|a b| |
|
532 |
||
533 |
a := (FixedPoint fromString:'0.99'). |
|
534 |
b := (FixedPoint fromString:'0.0000001'). |
|
535 |
a - b |
|
536 |
" |
|
537 |
||
538 |
" |
|
539 |
|a b| |
|
540 |
||
541 |
a := (FixedPoint fromString:'0.99'). |
|
542 |
b := (FixedPoint fromString:'0.0000001'). |
|
543 |
(a - b) withScale:2 |
|
544 |
" |
|
545 |
||
546 |
" |
|
547 |
|a b| |
|
548 |
||
549 |
a := (FixedPoint fromString:'0.99'). |
|
550 |
b := (FixedPoint fromString:'0.0000001'). |
|
551 |
(a - b) withScale:1 |
|
552 |
" |
|
553 |
||
554 |
" |
|
555 |
|a b| |
|
556 |
||
557 |
a := (FixedPoint fromString:'0.0000001'). |
|
558 |
b := (FixedPoint fromString:'0.99'). |
|
559 |
(a - b) withScale:2 |
|
560 |
" |
|
561 |
||
562 |
" |
|
563 |
|a b| |
|
564 |
||
565 |
a := 1. |
|
566 |
b := (FixedPoint fromString:'0.0000001'). |
|
567 |
Transcript showCR:((a - b) withScale:1). |
|
568 |
Transcript showCR:(a - b) |
|
569 |
" |
|
570 |
! |
|
571 |
||
572 |
/ aNumber |
|
1898 | 573 |
"return the quotient of the receiver and the argument, aNumber. |
1886 | 574 |
Redefined to care for the scale if the argument is another fixPoint number. |
575 |
The results scale is the maximum of the receivers scale and the arguments |
|
576 |
scale." |
|
577 |
||
6636
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
578 |
aNumber isInteger ifTrue:[ |
6886 | 579 |
^ self class |
6894
64f27ed2753f
do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents:
6886
diff
changeset
|
580 |
numerator:numerator |
64f27ed2753f
do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents:
6886
diff
changeset
|
581 |
denominator:(denominator * aNumber) |
64f27ed2753f
do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents:
6886
diff
changeset
|
582 |
scale:scale |
1886 | 583 |
]. |
584 |
||
6886 | 585 |
aNumber isFraction ifTrue:[ |
6645
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
586 |
^ self class |
6886 | 587 |
numerator:(numerator * aNumber denominator) |
588 |
denominator:(denominator * aNumber numerator) |
|
6645
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
589 |
scale:(scale max:aNumber scale) |
1886 | 590 |
]. |
591 |
^ aNumber quotientFromFixedPoint:self |
|
592 |
||
593 |
" |
|
594 |
|a r| |
|
595 |
||
596 |
a := (FixedPoint fromString:'123.456'). |
|
597 |
r := a / 5. |
|
598 |
Transcript showCR:r. |
|
599 |
Transcript showCR:(r withScale:2). |
|
600 |
Transcript showCR:(r withScale:9). |
|
601 |
" |
|
602 |
||
603 |
" |
|
604 |
|a b r| |
|
605 |
||
606 |
a := (FixedPoint fromString:'123.456'). |
|
607 |
b := (FixedPoint fromString:'1.10'). |
|
608 |
r := a / b. |
|
609 |
Transcript showCR:r. |
|
610 |
Transcript showCR:(r withScale:2). |
|
611 |
" |
|
612 |
||
613 |
" |
|
614 |
|a b r| |
|
615 |
||
616 |
a := (FixedPoint fromString:'-123.456'). |
|
617 |
b := (FixedPoint fromString:'-1.10'). |
|
618 |
r := a / b. |
|
619 |
Transcript showCR:r. |
|
620 |
Transcript showCR:(r withScale:2). |
|
621 |
" |
|
622 |
||
623 |
" |
|
624 |
|a b r| |
|
625 |
||
626 |
a := (FixedPoint fromString:'123.456'). |
|
627 |
b := (FixedPoint fromString:'-1.10'). |
|
628 |
r := a / b. |
|
629 |
Transcript showCR:r. |
|
630 |
Transcript showCR:(r withScale:2). |
|
631 |
" |
|
632 |
||
633 |
" |
|
634 |
|a b r| |
|
635 |
||
636 |
a := 1. |
|
637 |
b := (FixedPoint fromString:'0.9999999'). |
|
638 |
r := a / b. |
|
639 |
Transcript show:'fixed (exact) : '; showCR:r. |
|
640 |
Transcript show:'fixed (scale2) : '; showCR:(r withScale:2). |
|
641 |
||
642 |
Transcript show:'float (inexact): '; showCR:(1 / 0.9999999). |
|
643 |
" |
|
6570 | 644 |
! |
645 |
||
646 |
negated |
|
6894
64f27ed2753f
do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents:
6886
diff
changeset
|
647 |
"redefined from Fraction to preserve scale" |
6570 | 648 |
|
649 |
^ self class |
|
650 |
numerator:(numerator negated) |
|
651 |
denominator:denominator |
|
652 |
scale:scale |
|
653 |
! |
|
654 |
||
655 |
reciprocal |
|
6894
64f27ed2753f
do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents:
6886
diff
changeset
|
656 |
"redefined from Fraction to preserve scale" |
6570 | 657 |
|
658 |
numerator == 1 ifTrue:[^ denominator]. |
|
659 |
^ self class |
|
660 |
numerator:denominator |
|
661 |
denominator:numerator |
|
662 |
scale:scale |
|
1886 | 663 |
! ! |
664 |
||
665 |
!FixedPoint methodsFor:'coercing & converting'! |
|
666 |
||
667 |
asFixedPoint |
|
2140 | 668 |
"return the receiver as a fixedPoint number - thats the receiver itself" |
1886 | 669 |
|
670 |
^ self |
|
671 |
||
2140 | 672 |
"Modified: 10.1.1997 / 19:53:14 / cg" |
673 |
! |
|
674 |
||
675 |
asFixedPoint:newScale |
|
2538 | 676 |
"return a copy of the receiver, with newScale number of post-decimal |
2140 | 677 |
digits" |
678 |
||
679 |
^ self class |
|
680 |
numerator:numerator |
|
681 |
denominator:denominator |
|
682 |
scale:newScale |
|
683 |
||
684 |
" |
|
6638 | 685 |
'12345.12345' asFixedPoint:2 |
2140 | 686 |
(FixedPoint fromString:'12345.12345') asFixedPoint:2 |
687 |
||
688 |
((FixedPoint fromString:'0.33333333') |
|
689 |
+ |
|
690 |
(FixedPoint fromString:'0.33333333') |
|
691 |
) asFixedPoint:2 |
|
692 |
" |
|
693 |
||
2538 | 694 |
"Modified: 12.4.1997 / 11:20:37 / cg" |
1886 | 695 |
! |
696 |
||
697 |
asFraction |
|
698 |
"return the receiver as a fraction" |
|
699 |
||
700 |
^ (Fraction |
|
701 |
numerator:numerator |
|
6573
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
702 |
denominator:denominator) |
1886 | 703 |
|
704 |
" |
|
705 |
(FixedPoint fromString:'0.2') |
|
706 |
(FixedPoint fromString:'0.2') asFraction |
|
707 |
(FixedPoint fromString:'0.2') asFloat |
|
708 |
(FixedPoint fromString:'0.2') asShortFloat |
|
709 |
(FixedPoint fromString:'0.2') asInteger |
|
710 |
" |
|
711 |
! |
|
712 |
||
713 |
coerce:aNumber |
|
714 |
"return aNumber converted into receivers type" |
|
715 |
||
716 |
^ aNumber asFixedPoint |
|
717 |
||
718 |
! |
|
719 |
||
720 |
generality |
|
721 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
722 |
||
723 |
^ 65 |
|
724 |
||
725 |
" |
|
726 |
(FixedPoint fromString:'1.001') + 1 |
|
727 |
(FixedPoint fromString:'1.001') + 1.0 |
|
728 |
(FixedPoint fromString:'1.001') + (1/2) |
|
729 |
(FixedPoint fromString:'1.001') + 1.0 asShortFloat |
|
730 |
(FixedPoint fromString:'1.001') + 1 asLargeInteger |
|
731 |
||
732 |
1 + (FixedPoint fromString:'1.001') |
|
1892 | 733 |
1.0 + (FixedPoint fromString:'1.001') |
1891 | 734 |
(1/2) + (FixedPoint fromString:'1.001') |
1886 | 735 |
1.0 asShortFloat + (FixedPoint fromString:'1.001') |
736 |
1 asLargeInteger + (FixedPoint fromString:'1.001') |
|
737 |
" |
|
738 |
! ! |
|
739 |
||
740 |
!FixedPoint methodsFor:'double dispatching'! |
|
741 |
||
742 |
differenceFromInteger:anInteger |
|
743 |
"sent when an integer does not know how to subtract the receiver. |
|
744 |
Redefined here to preserve the scale." |
|
745 |
||
746 |
^ (self class |
|
747 |
numerator:((anInteger * denominator) - numerator) |
|
748 |
denominator:denominator |
|
6573
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
749 |
scale:scale) |
1886 | 750 |
! |
751 |
||
752 |
productFromInteger:anInteger |
|
753 |
"sent when an integer does not know how to multiply the receiver. |
|
754 |
Redefined here to preserve the scale." |
|
755 |
||
756 |
^ (self class |
|
757 |
numerator:(anInteger * numerator) |
|
758 |
denominator:denominator |
|
6573
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
759 |
scale:scale) |
1886 | 760 |
|
761 |
"Modified: 5.11.1996 / 10:32:28 / cg" |
|
762 |
! |
|
763 |
||
764 |
quotientFromInteger:anInteger |
|
765 |
"sent when an integer does not know how to divide by the receiver. |
|
766 |
Redefined here to preserve the scale." |
|
767 |
||
768 |
^ (self class |
|
769 |
numerator:(anInteger * denominator) |
|
770 |
denominator:numerator |
|
6573
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
771 |
scale:scale) |
1886 | 772 |
|
773 |
"Modified: 5.11.1996 / 10:32:35 / cg" |
|
774 |
! |
|
775 |
||
776 |
sumFromInteger:anInteger |
|
777 |
"sent when an integer does not know how to add the receiver. |
|
778 |
Redefined here to preserve the scale." |
|
779 |
||
780 |
^ (self class |
|
781 |
numerator:(numerator + (anInteger * denominator)) |
|
782 |
denominator:denominator |
|
6573
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
783 |
scale:scale) |
1886 | 784 |
|
785 |
"Modified: 5.11.1996 / 10:32:43 / cg" |
|
786 |
! ! |
|
787 |
||
5570 | 788 |
!FixedPoint methodsFor:'printing & storing'! |
1886 | 789 |
|
790 |
printOn: aStream |
|
2538 | 791 |
"append to the argument, aStream, a printed representation of the receiver. |
1886 | 792 |
For printout, only scale post-decimal digits are printed |
793 |
(and the printout is rounded to that many digits)" |
|
794 |
||
2563 | 795 |
|e integerPart fractionPart negative num| |
1886 | 796 |
|
797 |
numerator < 0 ifTrue:[ |
|
798 |
negative := true. |
|
799 |
num := numerator negated. |
|
800 |
] ifFalse:[ |
|
801 |
negative := false. |
|
802 |
num := numerator. |
|
803 |
]. |
|
804 |
integerPart := (num // denominator). |
|
805 |
e := 10 raisedTo:scale. |
|
806 |
fractionPart := (num \\ denominator). |
|
807 |
||
808 |
"/ the most common case is a denominator fitting the scale |
|
809 |
"/ (fixedPoint numbers are created this way) |
|
810 |
||
811 |
e == denominator ifFalse:[ |
|
812 |
fractionPart := fractionPart * (e * 10) // denominator. |
|
813 |
fractionPart := (fractionPart roundTo:10) // 10. |
|
814 |
||
815 |
fractionPart >= e ifTrue:[ |
|
816 |
integerPart := integerPart + 1. |
|
817 |
fractionPart := 0. |
|
818 |
] |
|
819 |
]. |
|
820 |
||
821 |
"/ |
|
822 |
"/ add a 1000..., so we can (mis-)use integer-printString ... |
|
823 |
"/ the highest-1 will be cutoff after padding. |
|
824 |
"/ |
|
825 |
fractionPart := e + fractionPart. |
|
826 |
||
827 |
negative ifTrue:[ |
|
828 |
aStream nextPut:$- |
|
829 |
]. |
|
830 |
integerPart printOn:aStream. |
|
6897 | 831 |
scale > 0 ifTrue:[ |
832 |
aStream nextPut: $.. |
|
833 |
((fractionPart printStringPaddedTo:scale with:$0) copyFrom:2) printOn:aStream |
|
834 |
]. |
|
1886 | 835 |
|
836 |
" |
|
837 |
(FixedPoint fromString:'0.66666666') |
|
838 |
(FixedPoint fromString:'0.66666666') withScale:2 |
|
839 |
(FixedPoint fromString:'0.99999999') |
|
840 |
(FixedPoint fromString:'0.99999999') withScale:2 |
|
841 |
(FixedPoint fromString:'1.00000001') |
|
842 |
(FixedPoint fromString:'1.00000001') withScale:2 |
|
843 |
(FixedPoint fromString:'1.005') |
|
844 |
(FixedPoint fromString:'1.005') withScale:2 |
|
845 |
(FixedPoint fromString:'1.005') withScale:1 |
|
6897 | 846 |
(FixedPoint fromString:'1.5') |
847 |
(FixedPoint fromString:'1.5') withScale:2 |
|
848 |
(FixedPoint fromString:'1.5') withScale:1 |
|
849 |
(FixedPoint fromString:'1.5') withScale:0 |
|
1886 | 850 |
|
851 |
(FixedPoint fromString:'-0.66666666') |
|
852 |
(FixedPoint fromString:'-0.66666666') withScale:2 |
|
853 |
(FixedPoint fromString:'-0.99999999') |
|
854 |
(FixedPoint fromString:'-0.99999999') withScale:2 |
|
855 |
(FixedPoint fromString:'-1.00000001') |
|
856 |
(FixedPoint fromString:'-1.00000001') withScale:2 |
|
857 |
(FixedPoint fromString:'-1.005') |
|
858 |
(FixedPoint fromString:'-1.005') withScale:2 |
|
859 |
(FixedPoint fromString:'-1.005') withScale:1 |
|
860 |
(FixedPoint fromString:'-1.05') |
|
861 |
(FixedPoint fromString:'-1.05') withScale:2 |
|
862 |
(FixedPoint fromString:'-1.05') withScale:1 |
|
863 |
(FixedPoint fromString:'-1.04') |
|
864 |
(FixedPoint fromString:'-1.04') withScale:2 |
|
865 |
(FixedPoint fromString:'-1.04') withScale:1 |
|
866 |
" |
|
867 |
||
868 |
" |
|
869 |
|a b r| |
|
870 |
||
871 |
a := (FixedPoint fromString:'0.66666666') withScale:2. |
|
872 |
b := (FixedPoint fromString:'0.33333333'). |
|
873 |
r := (a + b) withScale:4. |
|
874 |
Transcript show:'printout with scale of 4 :'; showCR:r. |
|
875 |
Transcript show:'more precise value :'; showCR:(r withScale:8) |
|
876 |
" |
|
2538 | 877 |
|
878 |
"Modified: 12.4.1997 / 11:20:51 / cg" |
|
1886 | 879 |
! ! |
880 |
||
881 |
!FixedPoint methodsFor:'private'! |
|
882 |
||
883 |
reduced |
|
884 |
|gc| |
|
885 |
||
886 |
scale isNil ifTrue:[ |
|
887 |
"/ to catch inherited Fraction reduce calls |
|
888 |
self error:'should not happen'. |
|
889 |
scale := 3 |
|
890 |
]. |
|
891 |
||
6573
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
892 |
(denominator < 0) ifTrue:[ |
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
893 |
numerator := numerator negated. |
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
894 |
denominator := denominator negated |
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
895 |
]. |
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
896 |
|
1886 | 897 |
denominator == 1 ifTrue:[^ numerator]. |
898 |
numerator == 1 ifTrue:[^ self]. |
|
899 |
numerator == 0 ifTrue:[^ 0]. |
|
900 |
||
901 |
gc := numerator gcd:denominator. |
|
6573
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
902 |
gc < 0 ifTrue:[ |
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
903 |
gc := gc negated |
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
904 |
]. |
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
905 |
gc := gc gcd:(10 raisedToInteger:scale). |
1886 | 906 |
|
6573
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
907 |
(gc ~~ 1) ifTrue:[ |
1886 | 908 |
numerator := numerator // gc. |
6573
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
909 |
denominator := denominator // gc. |
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
910 |
denominator == 1 ifTrue:[^ numerator]. |
1886 | 911 |
]. |
912 |
||
913 |
^ self |
|
914 |
! |
|
915 |
||
3731
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
916 |
scale:newScale |
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
917 |
"set the scale." |
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
918 |
|
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
919 |
scale := newScale. |
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
920 |
|
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
921 |
"Modified: / 12.4.1997 / 11:22:02 / cg" |
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
922 |
"Created: / 5.8.1998 / 13:28:49 / cg" |
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
923 |
! |
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
924 |
|
1886 | 925 |
setNumerator:nInteger denominator:d scale:s |
2538 | 926 |
"initialize the instance variables. |
927 |
Assumes that the fraction as specified by numerator and denominator |
|
928 |
is already reduced." |
|
1886 | 929 |
|
930 |
scale := s. |
|
931 |
super |
|
932 |
setNumerator:nInteger |
|
933 |
denominator:d |
|
2538 | 934 |
|
935 |
"Modified: 12.4.1997 / 11:21:47 / cg" |
|
1886 | 936 |
! |
937 |
||
938 |
setNumerator:nInteger scale:s |
|
2538 | 939 |
"initialize the instance variables. |
940 |
Assumes that the fraction as specified by numerator and denominator |
|
941 |
is already reduced." |
|
1886 | 942 |
|
943 |
scale := s. |
|
944 |
super |
|
945 |
setNumerator:nInteger |
|
946 |
denominator:(10 raisedTo:s) |
|
2538 | 947 |
|
948 |
"Modified: 12.4.1997 / 11:21:55 / cg" |
|
1886 | 949 |
! |
950 |
||
951 |
setScale:newScale |
|
2538 | 952 |
"initialize the scale instance variables." |
1886 | 953 |
|
954 |
scale := newScale. |
|
2538 | 955 |
|
956 |
"Modified: 12.4.1997 / 11:22:02 / cg" |
|
1886 | 957 |
! ! |
958 |
||
6650 | 959 |
!FixedPoint methodsFor:'testing'! |
1891 | 960 |
|
961 |
isFixedPoint |
|
962 |
"return true, if the receiver is some kind of fixedPoint number; |
|
963 |
true is returned here - the method is redefined from Object." |
|
964 |
||
965 |
^ true |
|
966 |
||
967 |
||
968 |
! ! |
|
969 |
||
1886 | 970 |
!FixedPoint class methodsFor:'documentation'! |
971 |
||
972 |
version |
|
7053
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
6898
diff
changeset
|
973 |
^ '$Header: /cvs/stx/stx/libbasic/FixedPoint.st,v 1.26 2003-02-25 10:17:54 cg Exp $' |
1886 | 974 |
! ! |