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