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