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