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