author | Claus Gittinger <cg@exept.de> |
Thu, 20 Jul 2006 22:43:19 +0200 | |
changeset 9452 | 921f2690dfe6 |
parent 9137 | 9692c2409b38 |
child 10192 | 7346a254d672 |
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' |
9137 | 19 |
classVariableNames:'PI_1000 PrintTruncated' |
6895 | 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 |
" |
|
7380 | 55 |
This class implements infinite precision fixed-point numbers, |
1886 | 56 |
which internally hold exact (fractional) results, but print themself with |
9137 | 57 |
a limited number of digits after the decimal point (rounded). |
58 |
||
59 |
NOTICE: |
|
60 |
it seems that squeak prints these truncated, |
|
61 |
whereas ST/X prints them rounded. |
|
62 |
This behavior should probably be controllable by providing |
|
63 |
a subclass (RoundedFixedPoint ?) which redefines the printOn: method. |
|
64 |
(it is now controlled by a classVar, which is of course not a thread-safe |
|
65 |
solution). |
|
7380 | 66 |
|
1886 | 67 |
These can be used in computation, where rounding errors should not accumulate, |
68 |
but only a limited precision is required for the final result. |
|
1898 | 69 |
(i.e. business applications) |
1886 | 70 |
|
71 |
It doesn't really do anything too interesting except creating instances, |
|
72 |
converting, and printing, since its superclass Fraction does all the work. |
|
73 |
||
74 |
Test: |
|
3060 | 75 |
'123456789012345678901234567890.123456789' asFixedPoint * 1000000000 |
1886 | 76 |
-> 123456789012345678901234567890123456789' |
77 |
||
78 |
Notes: 1) The current implementation does not convert arbitrarily-based |
|
79 |
String representations, which shouldn't be too much a problem |
|
80 |
for financial types. |
|
81 |
||
82 |
2) the implementation is a hack - it has not been optimized for speed |
|
83 |
in particular. |
|
84 |
||
1892 | 85 |
Mixed mode arithmetic: |
86 |
fix op fix -> fix, scale is max. of operands |
|
87 |
fix op fraction -> fix; scale is fix's scale |
|
88 |
fix op integer -> fix; scale is fix's scale |
|
89 |
fix op float -> float |
|
90 |
||
1886 | 91 |
[author:] |
92 |
Jan Steinman, Bytesmiths |
|
3060 | 93 |
adapted, modified & enhanced by Claus Gittinger |
1886 | 94 |
|
95 |
[see also:] |
|
9129 | 96 |
Number Fraction Integer Float ShortFloat LongFloat Complex |
1886 | 97 |
" |
98 |
! |
|
99 |
||
100 |
examples |
|
101 |
" |
|
102 |
[exBegin] |
|
103 |
|a b r| |
|
104 |
||
105 |
a := (FixedPoint fromString:'123.456'). |
|
3060 | 106 |
b := '1.10' 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.9999999'). |
|
3060 | 118 |
b := 0.0000001 asFixedPoint. |
1886 | 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:'0.9999998'). |
|
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:'1.0'). |
|
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 |
[exBegin] |
|
151 |
|a b r| |
|
152 |
||
153 |
a := (FixedPoint fromString:'0.99'). |
|
154 |
b := (FixedPoint fromString:'0.0000001'). |
|
155 |
r := a + b. |
|
156 |
Transcript showCR:r. |
|
157 |
Transcript showCR:(r withScale:2). |
|
158 |
Transcript showCR:(r withScale:1). |
|
159 |
Transcript showCR:(r rounded). |
|
160 |
[exEnd] |
|
161 |
||
162 |
" |
|
163 |
! ! |
|
164 |
||
165 |
!FixedPoint class methodsFor:'instance creation'! |
|
166 |
||
6237
4dc908af4f5c
report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents:
5570
diff
changeset
|
167 |
numerator:n denominator:d |
4dc908af4f5c
report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents:
5570
diff
changeset
|
168 |
self shouldNotImplement. "use #numerator:denominator:scale" |
4dc908af4f5c
report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents:
5570
diff
changeset
|
169 |
"/ ^ 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
|
170 |
|
4dc908af4f5c
report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents:
5570
diff
changeset
|
171 |
" |
4dc908af4f5c
report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents:
5570
diff
changeset
|
172 |
self numerator:123 denominator:100 |
4dc908af4f5c
report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents:
5570
diff
changeset
|
173 |
" |
4dc908af4f5c
report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents:
5570
diff
changeset
|
174 |
! |
4dc908af4f5c
report an error if #numerator:denominator: is invoked
Claus Gittinger <cg@exept.de>
parents:
5570
diff
changeset
|
175 |
|
1886 | 176 |
numerator:n denominator:d scale:s |
177 |
^ self basicNew |
|
6894
64f27ed2753f
do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents:
6886
diff
changeset
|
178 |
setNumerator:n denominator:d scale:s |
64f27ed2753f
do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents:
6886
diff
changeset
|
179 |
"/ ; reduced |
1886 | 180 |
! |
181 |
||
3060 | 182 |
readFrom:aStringOrStream |
183 |
"return the next FixedPoint from the (character-)stream aStream. |
|
184 |
||
185 |
NOTICE: |
|
186 |
This behaves different from the default readFrom:, in returning |
|
187 |
0 (instead of raising an error) in case no number can be read. |
|
188 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
189 |
- depending on the upcoming ANSI standard, this may change." |
|
190 |
||
191 |
^ self readFrom:aStringOrStream onError:0 |
|
1886 | 192 |
|
3060 | 193 |
" |
6636
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
194 |
FixedPoint readFrom:'123.456' |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
195 |
FixedPoint readFrom:'3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788' |
3060 | 196 |
FixedPoint readFrom:(ReadStream on:'foobar') |
197 |
FixedPoint readFrom:(ReadStream on:'foobar') onError:nil |
|
198 |
" |
|
199 |
||
200 |
"Modified: / 25.10.1997 / 15:30:29 / cg" |
|
6895 | 201 |
! |
202 |
||
203 |
readFrom:aStringOrStream onError:exceptionBlock |
|
204 |
"return an instance of me as described on the string or stream, aStringOrStream. |
|
205 |
If an error occurs during conversion, return the result |
|
206 |
from evaluating exceptionBlock" |
|
207 |
||
208 |
| aStream sign integerPart fractionStream char fractionPart scale | |
|
209 |
||
210 |
aStream := aStringOrStream readStream. |
|
211 |
||
212 |
aStream peek == $- ifTrue:[ |
|
213 |
sign := -1. |
|
214 |
aStream next. |
|
215 |
] ifFalse:[ |
|
216 |
sign := 1 |
|
217 |
]. |
|
218 |
||
219 |
(aStream atEnd or:[aStream peek isLetter]) ifTrue: [^ exceptionBlock value]. |
|
220 |
||
221 |
integerPart := (aStream upTo:$.) asNumber. |
|
222 |
(aStream atEnd or: [aStream peek isLetter]) ifTrue: [ |
|
223 |
fractionPart := 0. |
|
224 |
scale := 1. |
|
225 |
] ifFalse:[ |
|
226 |
fractionStream := ReadWriteStream on:(String new: 10). |
|
227 |
[ |
|
228 |
char := aStream next. |
|
229 |
char ~~ nil and:[char isDigit] |
|
230 |
] whileTrue:[ |
|
231 |
fractionStream nextPut:char |
|
232 |
]. |
|
233 |
||
7053
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
6898
diff
changeset
|
234 |
scale := fractionStream position0Based. |
6895 | 235 |
fractionStream reset. |
236 |
fractionPart := Number readFrom:fractionStream. |
|
237 |
]. |
|
238 |
||
239 |
^ self basicNew |
|
240 |
setNumerator:(integerPart * (10 raisedTo:scale) + fractionPart) * sign |
|
241 |
scale:scale |
|
242 |
||
243 |
" |
|
244 |
FixedPoint readFrom:'1.00' |
|
7053
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
6898
diff
changeset
|
245 |
FixedPoint readFrom:'123.456' |
6895 | 246 |
FixedPoint readFrom:'-123.456' |
247 |
FixedPoint readFrom:'123' |
|
248 |
FixedPoint readFrom:'-123' |
|
249 |
FixedPoint readFrom:'-123.abcd' onError:[47.5] |
|
250 |
FixedPoint readFrom:'-1a.bcd' onError:[47.5] |
|
251 |
FixedPoint readFrom:'foot' onError:['bad fixedpoint'] |
|
252 |
" |
|
253 |
||
254 |
"Created: / 25.10.1997 / 15:28:59 / cg" |
|
255 |
"Modified: / 25.10.1997 / 15:31:47 / cg" |
|
1886 | 256 |
! ! |
257 |
||
6636
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
258 |
!FixedPoint class methodsFor:'constants'! |
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 |
pi |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
261 |
"pi with 1000 valid digits..." |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
262 |
|
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
263 |
PI_1000 isNil ifTrue:[ |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
264 |
PI_1000 := self |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
265 |
readFrom:'3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788' |
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 |
^ PI_1000. |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
268 |
|
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
269 |
" |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
270 |
self pi squared |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
271 |
self pi reciprocal |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
272 |
1 / self pi |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
273 |
self pi * 1000000000000000000000000000000000000000000 |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
274 |
" |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
275 |
! ! |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
276 |
|
9137 | 277 |
!FixedPoint class methodsFor:'printing control'! |
278 |
||
279 |
printTruncated |
|
280 |
^ PrintTruncated ? false. |
|
281 |
! |
|
282 |
||
283 |
printTruncated:aBoolean |
|
284 |
PrintTruncated := aBoolean. |
|
285 |
! ! |
|
286 |
||
6898 | 287 |
!FixedPoint class methodsFor:'queries'! |
288 |
||
289 |
exponentCharacter |
|
290 |
^ $s |
|
291 |
! ! |
|
292 |
||
1886 | 293 |
!FixedPoint methodsFor:'accessing'! |
294 |
||
295 |
scale |
|
2538 | 296 |
"return the number of places of significance that is carried by the receiver." |
1886 | 297 |
|
298 |
^ scale |
|
2538 | 299 |
|
300 |
"Modified: 12.4.1997 / 11:21:05 / cg" |
|
1886 | 301 |
! ! |
302 |
||
303 |
!FixedPoint methodsFor:'arithmetic'! |
|
304 |
||
305 |
* aNumber |
|
306 |
"return the product of the receiver and the argument, aNumber. |
|
307 |
Redefined to care for the scale if the argument is another fixPoint number. |
|
7355 | 308 |
The results scale is the maximum of the receivers scale and the arguments scale." |
1886 | 309 |
|
7355 | 310 |
"/ notice: |
311 |
"/ the following code handles some common cases, |
|
312 |
"/ and exists as an optimization, to speed up those cases. |
|
313 |
"/ also notice, that checks for those cases must be inlinable without |
|
314 |
"/ a message send; otherwise double-dispatch is just as fast. |
|
315 |
||
316 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
|
1886 | 317 |
^ self class |
318 |
numerator:(numerator * aNumber) |
|
319 |
denominator:denominator |
|
320 |
scale:scale |
|
321 |
]. |
|
322 |
^ aNumber productFromFixedPoint:self |
|
323 |
||
324 |
" |
|
325 |
|a r| |
|
326 |
||
327 |
a := (FixedPoint fromString:'123.456'). |
|
328 |
r := a * 5. |
|
329 |
Transcript showCR:r. |
|
330 |
Transcript showCR:(r withScale:2). |
|
331 |
" |
|
332 |
||
333 |
" |
|
334 |
|a b r| |
|
335 |
||
336 |
a := (FixedPoint fromString:'123.456'). |
|
337 |
b := (FixedPoint fromString:'1.10'). |
|
338 |
r := a * b. |
|
339 |
Transcript showCR:r. |
|
340 |
Transcript showCR:(r withScale:2). |
|
341 |
" |
|
342 |
||
343 |
" |
|
344 |
|a b r| |
|
345 |
||
346 |
a := (FixedPoint fromString:'123.456'). |
|
347 |
b := (FixedPoint fromString:'-1.10'). |
|
348 |
r := a * b. |
|
349 |
Transcript showCR:r. |
|
350 |
Transcript showCR:(r withScale:2). |
|
351 |
" |
|
352 |
||
353 |
" |
|
354 |
|a b r| |
|
355 |
||
356 |
a := (FixedPoint fromString:'0.9999999'). |
|
357 |
b := (FixedPoint fromString:'0.9999999'). |
|
358 |
r := a * b. |
|
359 |
Transcript show:'fixed (exact) : '; showCR:r. |
|
360 |
Transcript show:'fixed (scale2) : '; showCR:(r withScale:2). |
|
361 |
||
362 |
Transcript show:'float (inexact): '; showCR:(0.9999999 * 0.9999999). |
|
363 |
" |
|
364 |
||
365 |
" |
|
366 |
|a b r| |
|
367 |
||
368 |
a := 1. |
|
369 |
b := (FixedPoint fromString:'0.9999999'). |
|
370 |
r := a * b. |
|
371 |
Transcript show:'fixed (exact) : '; showCR:r. |
|
372 |
Transcript show:'fixed (scale2) : '; showCR:(r withScale:2). |
|
373 |
||
374 |
Transcript show:'float (inexact): '; showCR:(0.9999999 * 0.9999999). |
|
375 |
" |
|
6636
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
376 |
|
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
377 |
" |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
378 |
|a r| |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
379 |
|
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
380 |
a := (FixedPoint fromString:'123.456'). |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
381 |
r := a * 5.0. |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
382 |
Transcript showCR:r. |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
383 |
Transcript showCR:(r withScale:2). |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
384 |
" |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
385 |
|
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
386 |
" |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
387 |
|a r| |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
388 |
|
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
389 |
a := (FixedPoint fromString:'123.456'). |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
390 |
r := 5.0 * a. |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
391 |
Transcript showCR:r. |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
392 |
Transcript showCR:(r withScale:2). |
dedc341eb3ab
arithmetic with integers fixed;
Claus Gittinger <cg@exept.de>
parents:
6625
diff
changeset
|
393 |
" |
1886 | 394 |
! |
395 |
||
396 |
+ aNumber |
|
397 |
"return the sum of the receiver and the argument, aNumber. |
|
398 |
Redefined to care for the scale if the argument is another fixPoint number. |
|
7355 | 399 |
The results scale will be the maximum of the receivers and the arguments scale." |
1886 | 400 |
|
7355 | 401 |
|n| |
1886 | 402 |
|
7355 | 403 |
"/ notice: |
404 |
"/ the following code handles some common cases, |
|
405 |
"/ and exists as an optimization, to speed up those cases. |
|
406 |
"/ also notice, that checks for those cases must be inlinable without |
|
407 |
"/ a message send; otherwise double-dispatch is just as fast. |
|
1886 | 408 |
|
7355 | 409 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
1886 | 410 |
"save a multiplication if possible" |
7355 | 411 |
denominator == 1 ifTrue:[ |
412 |
n := numerator + aNumber. |
|
6645
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
413 |
] ifFalse:[ |
7355 | 414 |
n := numerator + (aNumber * denominator). |
6645
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
415 |
]. |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
416 |
^ self class |
7355 | 417 |
numerator:n |
418 |
denominator:denominator |
|
419 |
scale:scale |
|
6645
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
420 |
]. |
1886 | 421 |
^ aNumber sumFromFixedPoint:self |
422 |
||
423 |
" |
|
7355 | 424 |
((1/3) asFixedPoint:2) + 1 |
425 |
((1/3) asFixedPoint:2) + (1/3) |
|
426 |
" |
|
427 |
" |
|
1886 | 428 |
|a b| |
429 |
||
430 |
a := (FixedPoint fromString:'123.456'). |
|
431 |
b := (FixedPoint fromString:'1.10'). |
|
432 |
a + b |
|
433 |
" |
|
434 |
" |
|
435 |
|a b| |
|
436 |
||
437 |
a := (FixedPoint fromString:'0.9999999'). |
|
438 |
b := (FixedPoint fromString:'0.0000001'). |
|
439 |
a + b |
|
440 |
" |
|
441 |
||
442 |
" |
|
443 |
|a b| |
|
444 |
||
445 |
a := (FixedPoint fromString:'0.99'). |
|
446 |
b := (FixedPoint fromString:'0.0000001'). |
|
447 |
a + b |
|
448 |
" |
|
449 |
||
450 |
" |
|
451 |
|a b| |
|
452 |
||
453 |
a := (FixedPoint fromString:'0.99'). |
|
454 |
b := (FixedPoint fromString:'0.0000001'). |
|
455 |
(a + b) withScale:2 |
|
456 |
" |
|
457 |
||
458 |
" |
|
459 |
|a b| |
|
460 |
||
461 |
a := (FixedPoint fromString:'0.99'). |
|
462 |
b := (FixedPoint fromString:'0.0000001'). |
|
463 |
(a + b) withScale:1 |
|
464 |
" |
|
465 |
||
466 |
" |
|
467 |
|a b| |
|
468 |
||
469 |
a := 1. |
|
470 |
b := (FixedPoint fromString:'0.0000001'). |
|
471 |
Transcript showCR:((a + b) withScale:1). |
|
472 |
Transcript showCR:(a + b) |
|
473 |
" |
|
474 |
! |
|
475 |
||
476 |
- aNumber |
|
477 |
"return the difference of the receiver and the argument, aNumber. |
|
478 |
Redefined to care for the scale if the argument is another fixPoint number. |
|
7355 | 479 |
The results scale is the maximum of the receivers scale and the arguments scale." |
1886 | 480 |
|
7355 | 481 |
|n| |
1886 | 482 |
|
7355 | 483 |
"/ notice: |
484 |
"/ the following code handles some common cases, |
|
485 |
"/ and exists as an optimization, to speed up those cases. |
|
486 |
"/ also notice, that checks for those cases must be inlinable without |
|
487 |
"/ a message send; otherwise double-dispatch is just as fast. |
|
1886 | 488 |
|
7355 | 489 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
1886 | 490 |
"save a multiplication if possible" |
7355 | 491 |
denominator == 1 ifTrue:[ |
492 |
n := numerator - aNumber. |
|
6645
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
493 |
] ifFalse:[ |
7355 | 494 |
n := numerator - (aNumber * denominator). |
6645
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
495 |
]. |
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
496 |
^ self class |
7355 | 497 |
numerator:n |
498 |
denominator:denominator |
|
499 |
scale:scale |
|
6645
b25be4db9230
arithmetic with other fixedPoints fixed
Claus Gittinger <cg@exept.de>
parents:
6638
diff
changeset
|
500 |
]. |
1886 | 501 |
^ aNumber differenceFromFixedPoint:self |
502 |
||
503 |
" |
|
504 |
|a b| |
|
505 |
||
506 |
a := (FixedPoint fromString:'123.456'). |
|
507 |
b := (FixedPoint fromString:'1.10'). |
|
7355 | 508 |
a - b |
1886 | 509 |
" |
510 |
||
511 |
" |
|
512 |
|a b| |
|
513 |
||
514 |
a := (FixedPoint fromString:'0.9999999'). |
|
515 |
b := (FixedPoint fromString:'0.0000009'). |
|
516 |
a - b |
|
517 |
" |
|
518 |
||
519 |
" |
|
520 |
|a b| |
|
521 |
||
522 |
a := (FixedPoint fromString:'0.99'). |
|
523 |
b := (FixedPoint fromString:'0.0000001'). |
|
524 |
a - b |
|
525 |
" |
|
526 |
||
527 |
" |
|
528 |
|a b| |
|
529 |
||
530 |
a := (FixedPoint fromString:'0.99'). |
|
531 |
b := (FixedPoint fromString:'0.0000001'). |
|
532 |
(a - b) withScale:2 |
|
533 |
" |
|
534 |
||
535 |
" |
|
536 |
|a b| |
|
537 |
||
538 |
a := (FixedPoint fromString:'0.99'). |
|
539 |
b := (FixedPoint fromString:'0.0000001'). |
|
540 |
(a - b) withScale:1 |
|
541 |
" |
|
542 |
||
543 |
" |
|
544 |
|a b| |
|
545 |
||
546 |
a := (FixedPoint fromString:'0.0000001'). |
|
547 |
b := (FixedPoint fromString:'0.99'). |
|
548 |
(a - b) withScale:2 |
|
549 |
" |
|
550 |
||
551 |
" |
|
552 |
|a b| |
|
553 |
||
554 |
a := 1. |
|
555 |
b := (FixedPoint fromString:'0.0000001'). |
|
556 |
Transcript showCR:((a - b) withScale:1). |
|
557 |
Transcript showCR:(a - b) |
|
558 |
" |
|
559 |
! |
|
560 |
||
561 |
/ aNumber |
|
1898 | 562 |
"return the quotient of the receiver and the argument, aNumber. |
1886 | 563 |
Redefined to care for the scale if the argument is another fixPoint number. |
7355 | 564 |
The results scale is the maximum of the receivers scale and the arguments scale." |
1886 | 565 |
|
7355 | 566 |
"/ notice: |
567 |
"/ the following code handles some common cases, |
|
568 |
"/ and exists as an optimization, to speed up those cases. |
|
569 |
"/ also notice, that checks for those cases must be inlinable without |
|
570 |
"/ a message send; otherwise double-dispatch is just as fast. |
|
571 |
||
572 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
|
6886 | 573 |
^ self class |
6894
64f27ed2753f
do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents:
6886
diff
changeset
|
574 |
numerator:numerator |
64f27ed2753f
do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents:
6886
diff
changeset
|
575 |
denominator:(denominator * aNumber) |
64f27ed2753f
do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents:
6886
diff
changeset
|
576 |
scale:scale |
1886 | 577 |
]. |
578 |
||
579 |
^ aNumber quotientFromFixedPoint:self |
|
580 |
||
581 |
" |
|
582 |
|a r| |
|
583 |
||
584 |
a := (FixedPoint fromString:'123.456'). |
|
585 |
r := a / 5. |
|
586 |
Transcript showCR:r. |
|
587 |
Transcript showCR:(r withScale:2). |
|
588 |
Transcript showCR:(r withScale:9). |
|
589 |
" |
|
590 |
||
591 |
" |
|
592 |
|a b r| |
|
593 |
||
594 |
a := (FixedPoint fromString:'123.456'). |
|
595 |
b := (FixedPoint fromString:'1.10'). |
|
596 |
r := a / b. |
|
597 |
Transcript showCR:r. |
|
598 |
Transcript showCR:(r withScale:2). |
|
599 |
" |
|
600 |
||
601 |
" |
|
602 |
|a b r| |
|
603 |
||
604 |
a := (FixedPoint fromString:'-123.456'). |
|
605 |
b := (FixedPoint fromString:'-1.10'). |
|
606 |
r := a / b. |
|
607 |
Transcript showCR:r. |
|
608 |
Transcript showCR:(r withScale:2). |
|
609 |
" |
|
610 |
||
611 |
" |
|
612 |
|a b r| |
|
613 |
||
614 |
a := (FixedPoint fromString:'123.456'). |
|
615 |
b := (FixedPoint fromString:'-1.10'). |
|
616 |
r := a / b. |
|
617 |
Transcript showCR:r. |
|
618 |
Transcript showCR:(r withScale:2). |
|
619 |
" |
|
620 |
||
621 |
" |
|
622 |
|a b r| |
|
623 |
||
624 |
a := 1. |
|
625 |
b := (FixedPoint fromString:'0.9999999'). |
|
626 |
r := a / b. |
|
627 |
Transcript show:'fixed (exact) : '; showCR:r. |
|
628 |
Transcript show:'fixed (scale2) : '; showCR:(r withScale:2). |
|
629 |
||
630 |
Transcript show:'float (inexact): '; showCR:(1 / 0.9999999). |
|
631 |
" |
|
6570 | 632 |
! |
633 |
||
634 |
negated |
|
6894
64f27ed2753f
do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents:
6886
diff
changeset
|
635 |
"redefined from Fraction to preserve scale" |
6570 | 636 |
|
637 |
^ self class |
|
638 |
numerator:(numerator negated) |
|
639 |
denominator:denominator |
|
640 |
scale:scale |
|
641 |
! |
|
642 |
||
643 |
reciprocal |
|
6894
64f27ed2753f
do not reduce in instance creation message;
Claus Gittinger <cg@exept.de>
parents:
6886
diff
changeset
|
644 |
"redefined from Fraction to preserve scale" |
6570 | 645 |
|
646 |
numerator == 1 ifTrue:[^ denominator]. |
|
647 |
^ self class |
|
648 |
numerator:denominator |
|
649 |
denominator:numerator |
|
650 |
scale:scale |
|
1886 | 651 |
! ! |
652 |
||
653 |
!FixedPoint methodsFor:'coercing & converting'! |
|
654 |
||
655 |
asFixedPoint |
|
2140 | 656 |
"return the receiver as a fixedPoint number - thats the receiver itself" |
1886 | 657 |
|
658 |
^ self |
|
659 |
||
2140 | 660 |
"Modified: 10.1.1997 / 19:53:14 / cg" |
661 |
! |
|
662 |
||
663 |
asFixedPoint:newScale |
|
7355 | 664 |
"return a fixedPoint with the same value as the receiver, |
665 |
and newScale number of post-decimal digits" |
|
666 |
||
9137 | 667 |
|minRequiredDenominator factor| |
668 |
||
7355 | 669 |
newScale == scale ifTrue:[^ self]. |
2140 | 670 |
|
9137 | 671 |
minRequiredDenominator := 10 raisedTo:newScale. |
672 |
denominator < minRequiredDenominator ifTrue:[ |
|
673 |
factor := minRequiredDenominator / denominator. |
|
674 |
||
675 |
^ self class |
|
676 |
numerator:(numerator * factor) |
|
677 |
denominator:minRequiredDenominator |
|
678 |
scale:newScale |
|
679 |
]. |
|
2140 | 680 |
^ self class |
681 |
numerator:numerator |
|
682 |
denominator:denominator |
|
683 |
scale:newScale |
|
684 |
||
685 |
" |
|
6638 | 686 |
'12345.12345' asFixedPoint:2 |
2140 | 687 |
(FixedPoint fromString:'12345.12345') asFixedPoint:2 |
688 |
||
689 |
((FixedPoint fromString:'0.33333333') |
|
690 |
+ |
|
691 |
(FixedPoint fromString:'0.33333333') |
|
692 |
) asFixedPoint:2 |
|
693 |
" |
|
694 |
||
2538 | 695 |
"Modified: 12.4.1997 / 11:20:37 / cg" |
1886 | 696 |
! |
697 |
||
698 |
asFraction |
|
699 |
"return the receiver as a fraction" |
|
700 |
||
7355 | 701 |
^ Fraction |
1886 | 702 |
numerator:numerator |
7355 | 703 |
denominator:denominator |
1886 | 704 |
|
705 |
" |
|
706 |
(FixedPoint fromString:'0.2') |
|
707 |
(FixedPoint fromString:'0.2') asFraction |
|
708 |
(FixedPoint fromString:'0.2') asFloat |
|
709 |
(FixedPoint fromString:'0.2') asShortFloat |
|
710 |
(FixedPoint fromString:'0.2') asInteger |
|
711 |
" |
|
712 |
! |
|
713 |
||
714 |
coerce:aNumber |
|
715 |
"return aNumber converted into receivers type" |
|
716 |
||
717 |
^ aNumber asFixedPoint |
|
718 |
||
719 |
! |
|
720 |
||
721 |
generality |
|
722 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
723 |
||
724 |
^ 65 |
|
725 |
||
726 |
" |
|
727 |
(FixedPoint fromString:'1.001') + 1 |
|
728 |
(FixedPoint fromString:'1.001') + 1.0 |
|
729 |
(FixedPoint fromString:'1.001') + (1/2) |
|
730 |
(FixedPoint fromString:'1.001') + 1.0 asShortFloat |
|
731 |
(FixedPoint fromString:'1.001') + 1 asLargeInteger |
|
732 |
||
733 |
1 + (FixedPoint fromString:'1.001') |
|
1892 | 734 |
1.0 + (FixedPoint fromString:'1.001') |
1891 | 735 |
(1/2) + (FixedPoint fromString:'1.001') |
1886 | 736 |
1.0 asShortFloat + (FixedPoint fromString:'1.001') |
737 |
1 asLargeInteger + (FixedPoint fromString:'1.001') |
|
738 |
" |
|
739 |
! ! |
|
740 |
||
741 |
!FixedPoint methodsFor:'double dispatching'! |
|
742 |
||
7355 | 743 |
differenceFromFixedPoint:aFixedPoint |
744 |
|n d otherNumerator otherDenominator| |
|
745 |
||
746 |
otherNumerator := aFixedPoint numerator. |
|
747 |
otherDenominator := aFixedPoint denominator. |
|
748 |
||
749 |
"save a multiplication if possible" |
|
750 |
denominator = otherDenominator ifTrue:[ |
|
751 |
n := otherNumerator - numerator. |
|
752 |
d := otherDenominator. |
|
753 |
] ifFalse:[ |
|
754 |
n := (otherNumerator * denominator) - (numerator * otherDenominator). |
|
755 |
d := denominator * otherDenominator. |
|
756 |
]. |
|
757 |
^ self class |
|
758 |
numerator:n |
|
759 |
denominator:d |
|
760 |
scale:(scale max:aFixedPoint scale) |
|
761 |
||
762 |
" |
|
763 |
((1/3) asFixedPoint:2) - ((1/3) asFixedPoint:2) |
|
764 |
((1/3) asFixedPoint:2) - ((2/3) asFixedPoint:2) |
|
765 |
(1/3) - ((2/3) asFixedPoint:2) |
|
766 |
||
767 |
((1/3) asFixedPoint:2) - ((1/3) asFixedPoint:4) |
|
768 |
((1/3) asFixedPoint:2) - ((2/3) asFixedPoint:4) |
|
769 |
" |
|
770 |
! |
|
771 |
||
772 |
differenceFromFraction:aFraction |
|
773 |
|n d otherNumerator otherDenominator| |
|
774 |
||
775 |
otherNumerator := aFraction numerator. |
|
776 |
otherDenominator := aFraction denominator. |
|
777 |
||
778 |
"save a multiplication if possible" |
|
779 |
denominator = otherDenominator ifTrue:[ |
|
780 |
n := otherNumerator - numerator. |
|
781 |
d := otherDenominator. |
|
782 |
] ifFalse:[ |
|
783 |
n := (otherNumerator * denominator) - (numerator * otherDenominator). |
|
784 |
d := denominator * otherDenominator. |
|
785 |
]. |
|
786 |
^ self class |
|
787 |
numerator:n |
|
788 |
denominator:d |
|
789 |
scale:scale |
|
790 |
||
791 |
" |
|
792 |
(1/3) - ((2/3) asFixedPoint:2) |
|
793 |
" |
|
794 |
! |
|
795 |
||
1886 | 796 |
differenceFromInteger:anInteger |
797 |
"sent when an integer does not know how to subtract the receiver. |
|
798 |
Redefined here to preserve the scale." |
|
799 |
||
7355 | 800 |
^ self class |
1886 | 801 |
numerator:((anInteger * denominator) - numerator) |
802 |
denominator:denominator |
|
7355 | 803 |
scale:scale |
804 |
! |
|
805 |
||
806 |
productFromFixedPoint:aFixedPoint |
|
807 |
^ aFixedPoint class |
|
808 |
numerator:(aFixedPoint numerator * numerator) |
|
809 |
denominator:(aFixedPoint denominator * denominator) |
|
810 |
scale:(scale max:aFixedPoint scale) |
|
811 |
||
812 |
" |
|
813 |
((1/3) asFixedPoint:2) * ((1/3) asFixedPoint:4) |
|
814 |
(1/3) * ((1/3) asFixedPoint:4) |
|
815 |
" |
|
816 |
! |
|
817 |
||
818 |
productFromFraction:aFraction |
|
819 |
^ self class |
|
820 |
numerator:(aFraction numerator * numerator) |
|
821 |
denominator:(aFraction denominator * denominator) |
|
822 |
scale:scale |
|
823 |
||
824 |
" |
|
825 |
(1/3) * ((1/3) asFixedPoint:4) |
|
826 |
" |
|
1886 | 827 |
! |
828 |
||
829 |
productFromInteger:anInteger |
|
830 |
"sent when an integer does not know how to multiply the receiver. |
|
831 |
Redefined here to preserve the scale." |
|
832 |
||
7355 | 833 |
^ self class |
1886 | 834 |
numerator:(anInteger * numerator) |
835 |
denominator:denominator |
|
7355 | 836 |
scale:scale |
1886 | 837 |
|
838 |
"Modified: 5.11.1996 / 10:32:28 / cg" |
|
839 |
! |
|
840 |
||
7355 | 841 |
quotientFromFixedPoint:aFixedPoint |
7442 | 842 |
"Return the quotient of the argument, aFixedPoint and the receiver." |
843 |
||
7355 | 844 |
^ aFixedPoint class |
845 |
numerator:(aFixedPoint numerator * denominator) |
|
846 |
denominator:(aFixedPoint denominator * numerator) |
|
847 |
scale:(scale max:aFixedPoint scale) |
|
848 |
||
849 |
" |
|
850 |
((1/3) asFixedPoint:2) / ((1/3) asFixedPoint:2) |
|
851 |
((1/3) asFixedPoint:2) / ((1/3) asFixedPoint:4) |
|
852 |
" |
|
853 |
! |
|
854 |
||
855 |
quotientFromFraction:aFraction |
|
7442 | 856 |
"Return the quotient of the argument, aFraction and the receiver. |
857 |
Sent when aFraction does not know how to divide by the receiver. |
|
858 |
Redefined here to preserve the scale." |
|
859 |
||
7355 | 860 |
^ aFraction class |
861 |
numerator:(aFraction numerator * denominator) |
|
862 |
denominator:(aFraction denominator * numerator) |
|
863 |
scale:scale |
|
864 |
||
865 |
" |
|
866 |
((1/3) asFixedPoint:2) / ((1/3) asFixedPoint:2) |
|
867 |
((1/3) asFixedPoint:2) / ((1/3) asFixedPoint:4) |
|
868 |
" |
|
869 |
! |
|
870 |
||
1886 | 871 |
quotientFromInteger:anInteger |
7442 | 872 |
"Return the quotient of the argument, anInteger and the receiver. |
873 |
Sent when anInteger does not know how to divide by the receiver. |
|
1886 | 874 |
Redefined here to preserve the scale." |
875 |
||
7355 | 876 |
^ self class |
1886 | 877 |
numerator:(anInteger * denominator) |
878 |
denominator:numerator |
|
7355 | 879 |
scale:scale |
1886 | 880 |
|
881 |
"Modified: 5.11.1996 / 10:32:35 / cg" |
|
882 |
! |
|
883 |
||
7355 | 884 |
sumFromFixedPoint:aFixedPoint |
885 |
|n d otherNumerator otherDenominator| |
|
886 |
||
887 |
otherNumerator := aFixedPoint numerator. |
|
888 |
otherDenominator := aFixedPoint denominator. |
|
889 |
||
890 |
"save a multiplication if possible" |
|
891 |
denominator = otherDenominator ifTrue:[ |
|
892 |
n := numerator + otherNumerator. |
|
893 |
d := otherDenominator. |
|
894 |
] ifFalse:[ |
|
895 |
n := (numerator * otherDenominator) + (otherNumerator * denominator). |
|
896 |
d := denominator * otherDenominator. |
|
897 |
]. |
|
898 |
^ self class |
|
899 |
numerator:n |
|
900 |
denominator:d |
|
901 |
scale:(scale max:aFixedPoint scale) |
|
902 |
||
903 |
" |
|
904 |
((1/3) asFixedPoint:2) + ((1/3) asFixedPoint:2) |
|
905 |
((1/3) asFixedPoint:2) + ((2/3) asFixedPoint:2) |
|
906 |
||
907 |
((1/3) asFixedPoint:2) + ((1/3) asFixedPoint:4) |
|
908 |
" |
|
909 |
! |
|
910 |
||
911 |
sumFromFraction:aFraction |
|
912 |
|n d otherNumerator otherDenominator| |
|
913 |
||
914 |
otherNumerator := aFraction numerator. |
|
915 |
otherDenominator := aFraction denominator. |
|
916 |
||
917 |
"save a multiplication if possible" |
|
918 |
denominator = otherDenominator ifTrue:[ |
|
919 |
n := numerator + otherNumerator. |
|
920 |
d := otherDenominator. |
|
921 |
] ifFalse:[ |
|
922 |
n := (numerator * otherDenominator) + (otherNumerator * denominator). |
|
923 |
d := denominator * otherDenominator. |
|
924 |
]. |
|
925 |
^ self class |
|
926 |
numerator:n |
|
927 |
denominator:d |
|
928 |
scale:scale |
|
929 |
||
930 |
" |
|
931 |
(1/3) + ((1/3) asFixedPoint:2) |
|
932 |
" |
|
933 |
! |
|
934 |
||
1886 | 935 |
sumFromInteger:anInteger |
936 |
"sent when an integer does not know how to add the receiver. |
|
937 |
Redefined here to preserve the scale." |
|
938 |
||
7355 | 939 |
^ self class |
1886 | 940 |
numerator:(numerator + (anInteger * denominator)) |
941 |
denominator:denominator |
|
7355 | 942 |
scale:scale |
1886 | 943 |
|
944 |
"Modified: 5.11.1996 / 10:32:43 / cg" |
|
945 |
! ! |
|
946 |
||
5570 | 947 |
!FixedPoint methodsFor:'printing & storing'! |
1886 | 948 |
|
9137 | 949 |
printDecimalOn:aStream roundToScale:roundToScale truncateToScale:truncateToScale |
950 |
"common helper for printing (with round or truncate) |
|
951 |
and storing (neither rounding, nor truncating)" |
|
1886 | 952 |
|
9133
2d3e3d083be8
storeOn fixed for negative numbers with 0 scale
Claus Gittinger <cg@exept.de>
parents:
9132
diff
changeset
|
953 |
|e integerPart fractionPart negative num rest| |
1886 | 954 |
|
955 |
numerator < 0 ifTrue:[ |
|
956 |
negative := true. |
|
957 |
num := numerator negated. |
|
958 |
] ifFalse:[ |
|
959 |
negative := false. |
|
960 |
num := numerator. |
|
961 |
]. |
|
962 |
integerPart := (num // denominator). |
|
963 |
fractionPart := (num \\ denominator). |
|
9137 | 964 |
(roundToScale or:[truncateToScale]) ifTrue:[ |
9131 | 965 |
e := 10 raisedTo:scale. |
966 |
] ifFalse:[ |
|
9137 | 967 |
(denominator isPowerOf:10) ifTrue:[ |
968 |
e := denominator. |
|
969 |
] ifFalse:[ |
|
970 |
e := 10 raisedTo:(denominator log:10) asInteger + scale + 2. |
|
971 |
]. |
|
9131 | 972 |
]. |
1886 | 973 |
|
974 |
"/ the most common case is a denominator fitting the scale |
|
975 |
"/ (fixedPoint numbers are created this way) |
|
9137 | 976 |
e = denominator ifFalse:[ |
1886 | 977 |
fractionPart := fractionPart * (e * 10) // denominator. |
9137 | 978 |
roundToScale ifTrue:[ |
979 |
fractionPart := (fractionPart roundTo:10) // 10. |
|
980 |
] ifFalse:[ |
|
981 |
fractionPart := fractionPart // 10. |
|
982 |
]. |
|
1886 | 983 |
fractionPart >= e ifTrue:[ |
984 |
integerPart := integerPart + 1. |
|
985 |
fractionPart := 0. |
|
986 |
] |
|
987 |
]. |
|
988 |
||
989 |
"/ |
|
990 |
"/ add a 1000..., so we can (mis-)use integer-printString ... |
|
991 |
"/ the highest-1 will be cutoff after padding. |
|
992 |
"/ |
|
993 |
fractionPart := e + fractionPart. |
|
994 |
||
995 |
negative ifTrue:[ |
|
996 |
aStream nextPut:$- |
|
997 |
]. |
|
998 |
integerPart printOn:aStream. |
|
9137 | 999 |
(roundToScale or:[truncateToScale]) ifTrue:[ |
9131 | 1000 |
scale > 0 ifTrue:[ |
1001 |
aStream nextPut: $.. |
|
1002 |
((fractionPart printStringPaddedTo:scale with:$0) copyFrom:2) printOn:aStream |
|
1003 |
]. |
|
1004 |
] ifFalse:[ |
|
9133
2d3e3d083be8
storeOn fixed for negative numbers with 0 scale
Claus Gittinger <cg@exept.de>
parents:
9132
diff
changeset
|
1005 |
rest := ((fractionPart printString) copyFrom:2). |
9137 | 1006 |
scale > 0 ifTrue:[ |
9133
2d3e3d083be8
storeOn fixed for negative numbers with 0 scale
Claus Gittinger <cg@exept.de>
parents:
9132
diff
changeset
|
1007 |
aStream nextPut: $.. |
9137 | 1008 |
(rest paddedTo:scale with:$0) printOn:aStream |
1009 |
] ifFalse:[ |
|
1010 |
rest notEmpty ifTrue:[ |
|
1011 |
aStream nextPut: $.. |
|
1012 |
rest printOn:aStream |
|
1013 |
] |
|
9133
2d3e3d083be8
storeOn fixed for negative numbers with 0 scale
Claus Gittinger <cg@exept.de>
parents:
9132
diff
changeset
|
1014 |
] |
6897 | 1015 |
]. |
1886 | 1016 |
|
1017 |
" |
|
9137 | 1018 |
((FixedPoint fromString:'0.66666666') withScale:2) |
1019 |
printDecimalOn:Transcript roundToScale:false truncateToScale:false |
|
1020 |
||
1021 |
((FixedPoint fromString:'0.66666666') withScale:2) |
|
1022 |
printDecimalOn:Transcript roundToScale:true truncateToScale:false |
|
1023 |
||
1024 |
((FixedPoint fromString:'0.66666666') withScale:2) |
|
1025 |
printDecimalOn:Transcript roundToScale:false truncateToScale:true |
|
1026 |
" |
|
1027 |
! |
|
1028 |
||
1029 |
printOn:aStream |
|
1030 |
"append to the argument, aStream, a printed representation of the receiver. |
|
1031 |
For printout, only scale post-decimal digits are printed |
|
1032 |
(By default, the printout is rounded to that many digits)" |
|
1033 |
||
1034 |
PrintTruncated == true ifTrue:[ |
|
1035 |
self printTruncatedOn:aStream |
|
1036 |
] ifFalse:[ |
|
1037 |
self printRoundedOn:aStream |
|
1038 |
]. |
|
1039 |
||
1040 |
" |
|
1886 | 1041 |
(FixedPoint fromString:'0.66666666') |
1042 |
(FixedPoint fromString:'0.66666666') withScale:2 |
|
1043 |
(FixedPoint fromString:'0.99999999') |
|
1044 |
(FixedPoint fromString:'0.99999999') withScale:2 |
|
1045 |
(FixedPoint fromString:'1.00000001') |
|
1046 |
(FixedPoint fromString:'1.00000001') withScale:2 |
|
1047 |
(FixedPoint fromString:'1.005') |
|
1048 |
(FixedPoint fromString:'1.005') withScale:2 |
|
1049 |
(FixedPoint fromString:'1.005') withScale:1 |
|
6897 | 1050 |
(FixedPoint fromString:'1.5') |
1051 |
(FixedPoint fromString:'1.5') withScale:2 |
|
1052 |
(FixedPoint fromString:'1.5') withScale:1 |
|
1053 |
(FixedPoint fromString:'1.5') withScale:0 |
|
1886 | 1054 |
|
1055 |
(FixedPoint fromString:'-0.66666666') |
|
1056 |
(FixedPoint fromString:'-0.66666666') withScale:2 |
|
1057 |
(FixedPoint fromString:'-0.99999999') |
|
1058 |
(FixedPoint fromString:'-0.99999999') withScale:2 |
|
1059 |
(FixedPoint fromString:'-1.00000001') |
|
1060 |
(FixedPoint fromString:'-1.00000001') withScale:2 |
|
1061 |
(FixedPoint fromString:'-1.005') |
|
1062 |
(FixedPoint fromString:'-1.005') withScale:2 |
|
1063 |
(FixedPoint fromString:'-1.005') withScale:1 |
|
1064 |
(FixedPoint fromString:'-1.05') |
|
1065 |
(FixedPoint fromString:'-1.05') withScale:2 |
|
1066 |
(FixedPoint fromString:'-1.05') withScale:1 |
|
1067 |
(FixedPoint fromString:'-1.04') |
|
1068 |
(FixedPoint fromString:'-1.04') withScale:2 |
|
1069 |
(FixedPoint fromString:'-1.04') withScale:1 |
|
1070 |
" |
|
1071 |
||
1072 |
" |
|
1073 |
|a b r| |
|
1074 |
||
1075 |
a := (FixedPoint fromString:'0.66666666') withScale:2. |
|
1076 |
b := (FixedPoint fromString:'0.33333333'). |
|
1077 |
r := (a + b) withScale:4. |
|
1078 |
Transcript show:'printout with scale of 4 :'; showCR:r. |
|
1079 |
Transcript show:'more precise value :'; showCR:(r withScale:8) |
|
1080 |
" |
|
2538 | 1081 |
|
1082 |
"Modified: 12.4.1997 / 11:20:51 / cg" |
|
9131 | 1083 |
! |
1084 |
||
9137 | 1085 |
printRoundedOn:aStream |
9131 | 1086 |
"append to the argument, aStream, a printed representation of the receiver. |
9137 | 1087 |
For printout, only scale post-decimal digits are printed, |
1088 |
the printout is rounded to that many digits" |
|
9131 | 1089 |
|
9137 | 1090 |
self printDecimalOn:aStream roundToScale:true truncateToScale:false |
9131 | 1091 |
|
1092 |
" |
|
9137 | 1093 |
((FixedPoint fromString:'0.66666666') withScale:2) printRoundedOn:Transcript |
1094 |
((FixedPoint fromString:'0.66666666') withScale:2) printTruncatedOn:Transcript |
|
9131 | 1095 |
" |
1096 |
||
9137 | 1097 |
"Modified: 12.4.1997 / 11:20:51 / cg" |
1098 |
! |
|
9131 | 1099 |
|
9137 | 1100 |
printTruncatedOn:aStream |
1101 |
"append to the argument, aStream, a printed representation of the receiver. |
|
1102 |
For printout, only scale post-decimal digits are printed, |
|
1103 |
the printout is truncated to that many digits" |
|
1104 |
||
1105 |
self printDecimalOn:aStream roundToScale:false truncateToScale:true |
|
1106 |
||
1107 |
" |
|
1108 |
((FixedPoint fromString:'0.66666666') withScale:2) printRoundedOn:Transcript |
|
1109 |
((FixedPoint fromString:'0.66666666') withScale:2) printTruncatedOn:Transcript |
|
9131 | 1110 |
" |
1111 |
||
1112 |
"Modified: 12.4.1997 / 11:20:51 / cg" |
|
1113 |
! |
|
1114 |
||
1115 |
storeOn:aStream |
|
9132 | 1116 |
"notice: we MUST preserve the full internal precision when storing/reloading" |
1117 |
||
9137 | 1118 |
self printDecimalOn:aStream roundToScale:false truncateToScale:false. |
9131 | 1119 |
aStream nextPut:$s. |
1120 |
scale storeOn:aStream. |
|
1121 |
||
1122 |
" |
|
1123 |
((FixedPoint fromString:'0.66666666') ) storeString |
|
1124 |
((FixedPoint fromString:'0.66666666') withScale:2 ) storeString |
|
1125 |
((FixedPoint fromString:'1.5') ) storeString |
|
1126 |
((FixedPoint fromString:'1.5') withScale:2 ) storeString |
|
1127 |
((FixedPoint fromString:'1.5') withScale:1 ) storeString |
|
1128 |
((FixedPoint fromString:'1.5') withScale:0 ) storeString |
|
1129 |
" |
|
1130 |
! |
|
1131 |
||
1132 |
storeString |
|
1133 |
^ String streamContents:[:s | self storeOn:s] |
|
1134 |
||
1135 |
" |
|
1136 |
((FixedPoint fromString:'0.66666666') ) storeString |
|
1137 |
((FixedPoint fromString:'0.66666666') withScale:2 ) storeString |
|
1138 |
((FixedPoint fromString:'1.5') ) storeString |
|
1139 |
((FixedPoint fromString:'1.5') withScale:2 ) storeString |
|
1140 |
((FixedPoint fromString:'1.5') withScale:1 ) storeString |
|
1141 |
((FixedPoint fromString:'1.5') withScale:0 ) storeString |
|
1142 |
" |
|
1886 | 1143 |
! ! |
1144 |
||
1145 |
!FixedPoint methodsFor:'private'! |
|
1146 |
||
1147 |
reduced |
|
1148 |
|gc| |
|
1149 |
||
1150 |
scale isNil ifTrue:[ |
|
1151 |
"/ to catch inherited Fraction reduce calls |
|
1152 |
self error:'should not happen'. |
|
1153 |
scale := 3 |
|
1154 |
]. |
|
1155 |
||
6573
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
1156 |
(denominator < 0) ifTrue:[ |
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
1157 |
numerator := numerator negated. |
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
1158 |
denominator := denominator negated |
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
1159 |
]. |
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
1160 |
|
1886 | 1161 |
denominator == 1 ifTrue:[^ numerator]. |
1162 |
numerator == 1 ifTrue:[^ self]. |
|
1163 |
numerator == 0 ifTrue:[^ 0]. |
|
1164 |
||
1165 |
gc := numerator gcd:denominator. |
|
6573
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
1166 |
gc < 0 ifTrue:[ |
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
1167 |
gc := gc negated |
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
1168 |
]. |
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
1169 |
gc := gc gcd:(10 raisedToInteger:scale). |
1886 | 1170 |
|
6573
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
1171 |
(gc ~~ 1) ifTrue:[ |
1886 | 1172 |
numerator := numerator // gc. |
6573
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
1173 |
denominator := denominator // gc. |
4ea758ea3d4b
reduce when creating new FixedPoints
Stefan Vogel <sv@exept.de>
parents:
6570
diff
changeset
|
1174 |
denominator == 1 ifTrue:[^ numerator]. |
1886 | 1175 |
]. |
1176 |
||
1177 |
^ self |
|
1178 |
! |
|
1179 |
||
3731
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1180 |
scale:newScale |
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1181 |
"set the scale." |
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1182 |
|
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1183 |
scale := newScale. |
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1184 |
|
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1185 |
"Modified: / 12.4.1997 / 11:22:02 / cg" |
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1186 |
"Created: / 5.8.1998 / 13:28:49 / cg" |
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1187 |
! |
8c2ef8f76670
added #scale: for protocol completeness.
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1188 |
|
1886 | 1189 |
setNumerator:nInteger denominator:d scale:s |
2538 | 1190 |
"initialize the instance variables. |
1191 |
Assumes that the fraction as specified by numerator and denominator |
|
1192 |
is already reduced." |
|
1886 | 1193 |
|
1194 |
scale := s. |
|
1195 |
super |
|
1196 |
setNumerator:nInteger |
|
1197 |
denominator:d |
|
2538 | 1198 |
|
1199 |
"Modified: 12.4.1997 / 11:21:47 / cg" |
|
1886 | 1200 |
! |
1201 |
||
1202 |
setNumerator:nInteger scale:s |
|
2538 | 1203 |
"initialize the instance variables. |
1204 |
Assumes that the fraction as specified by numerator and denominator |
|
1205 |
is already reduced." |
|
1886 | 1206 |
|
1207 |
scale := s. |
|
1208 |
super |
|
1209 |
setNumerator:nInteger |
|
1210 |
denominator:(10 raisedTo:s) |
|
2538 | 1211 |
|
1212 |
"Modified: 12.4.1997 / 11:21:55 / cg" |
|
1886 | 1213 |
! |
1214 |
||
1215 |
setScale:newScale |
|
2538 | 1216 |
"initialize the scale instance variables." |
1886 | 1217 |
|
1218 |
scale := newScale. |
|
2538 | 1219 |
|
1220 |
"Modified: 12.4.1997 / 11:22:02 / cg" |
|
1886 | 1221 |
! ! |
1222 |
||
6650 | 1223 |
!FixedPoint methodsFor:'testing'! |
1891 | 1224 |
|
1225 |
isFixedPoint |
|
1226 |
"return true, if the receiver is some kind of fixedPoint number; |
|
1227 |
true is returned here - the method is redefined from Object." |
|
1228 |
||
1229 |
^ true |
|
1230 |
||
1231 |
||
1232 |
! ! |
|
1233 |
||
7376
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1234 |
!FixedPoint methodsFor:'truncation & rounding'! |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1235 |
|
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1236 |
roundedToScale |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1237 |
"return the receiver rounded to my scale |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1238 |
(i.e. return a number which has the value which is shown when printing)" |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1239 |
|
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1240 |
|n scale10| |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1241 |
|
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1242 |
scale10 := 10 raisedToInteger:scale. |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1243 |
n := ((numerator * scale10) + (denominator bitShift:-1)) // denominator. |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1244 |
^ self class |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1245 |
numerator:n |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1246 |
denominator:scale10 |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1247 |
scale:scale. |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1248 |
|
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1249 |
" |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1250 |
((2/3) asFixedPoint:2) |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1251 |
((2/3) asFixedPoint:2) rounded |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1252 |
((2/3) asFixedPoint:2) roundedToScale |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1253 |
" |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1254 |
|
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1255 |
"Modified: 5.11.1996 / 12:18:46 / cg" |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1256 |
! |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1257 |
|
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1258 |
truncatedToScale |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1259 |
"return the receiver truncated towards zero to my scale" |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1260 |
|
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1261 |
|n scale10| |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1262 |
|
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1263 |
scale10 := 10 raisedToInteger:scale. |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1264 |
n := (numerator * scale10) quo:denominator. |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1265 |
^ self class |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1266 |
numerator:n |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1267 |
denominator:scale10 |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1268 |
scale:scale. |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1269 |
|
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1270 |
" |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1271 |
((2/3) asFixedPoint:2) |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1272 |
((2/3) asFixedPoint:2) truncated |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1273 |
((2/3) asFixedPoint:2) truncatedToScale |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1274 |
" |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1275 |
|
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1276 |
"Modified: 5.11.1996 / 12:18:46 / cg" |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1277 |
! ! |
a166651de99e
truncatedToScale/roundedToScale
Claus Gittinger <cg@exept.de>
parents:
7355
diff
changeset
|
1278 |
|
1886 | 1279 |
!FixedPoint class methodsFor:'documentation'! |
1280 |
||
1281 |
version |
|
9137 | 1282 |
^ '$Header: /cvs/stx/stx/libbasic/FixedPoint.st,v 1.35 2006-02-17 14:30:57 cg Exp $' |
1886 | 1283 |
! ! |