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