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