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