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