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