author | Claus Gittinger <cg@exept.de> |
Mon, 25 Nov 2019 15:24:20 +0100 | |
changeset 24982 | f64317156f40 |
parent 24931 | 843e3aaea193 |
child 24995 | 1e0f6b44c6af |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
18239 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
5552 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
18839 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1 | 16 |
Number subclass:#Fraction |
5552 | 17 |
instanceVariableNames:'numerator denominator' |
24931 | 18 |
classVariableNames:'FractionOne FractionZero PrintWholeNumbers Pi Pi_1000 E Phi' |
5552 | 19 |
poolDictionaries:'' |
20 |
category:'Magnitude-Numbers' |
|
1 | 21 |
! |
22 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
23 |
!Fraction class methodsFor:'documentation'! |
88 | 24 |
|
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
18239 | 28 |
All Rights Reserved |
88 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
39 |
documentation |
|
40 |
" |
|
41 |
Instances of Fraction represent fractional numbers consisting of |
|
42 |
a numerator and denominator. Both are themselfes arbitrary precision |
|
18239 | 43 |
integers. |
6675 | 44 |
Fractions are usually created by dividing Integers using / (for exact division). |
45 |
Notice, that all operations on fractions reduce their result; this means, that |
|
46 |
the result of a fraction-operation may return an integer. |
|
47 |
Aka: |
|
21766 | 48 |
(1 / 7) * 7 -> 1 (not 0.99999999...) |
1295 | 49 |
|
1892 | 50 |
Mixed mode arithmetic: |
21766 | 51 |
fraction op fraction -> fraction/integer |
52 |
fraction op fix -> fix; scale is fix's scale |
|
53 |
fraction op integer -> fraction/integer |
|
54 |
fraction op float -> float |
|
1892 | 55 |
|
6675 | 56 |
|
57 |
[classVariables:] |
|
21766 | 58 |
PrintWholeNumbers Boolean experimental: |
59 |
controls how fractions which are greater than 1 are printed. |
|
60 |
if true, print them as a sum of an integral and the fractional part. |
|
61 |
(Large ones are easier to read this way) |
|
62 |
(17/3) printString -> '(5+(2/3))' |
|
63 |
for now, the default is false, for backward compatibility |
|
6675 | 64 |
|
1295 | 65 |
[author:] |
21766 | 66 |
Claus Gittinger |
1556 | 67 |
|
68 |
[see also:] |
|
21766 | 69 |
Number |
70 |
FixedPoint Float ShortFloat LongFloat Integer Complex |
|
1295 | 71 |
" |
88 | 72 |
! ! |
1 | 73 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
74 |
!Fraction class methodsFor:'instance creation'! |
1 | 75 |
|
76 |
new |
|
77 |
"create and return a new fraction with value 0" |
|
78 |
||
54 | 79 |
^ self numerator:0 denominator:1 |
1 | 80 |
! |
81 |
||
82 |
numerator:num denominator:den |
|
8629 | 83 |
"create and return a new fraction with numerator num and denominator den. |
84 |
Notice: stc inlines this message if sent to the global named Fraction." |
|
1 | 85 |
|
2792 | 86 |
|newFraction| |
87 |
||
1 | 88 |
%{ /* NOCONTEXT */ |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18239
diff
changeset
|
89 |
#ifdef __SCHTEAM__ |
18239 | 90 |
if (self == Fraction.Class) { |
24135 | 91 |
return context._RETURN(new STFraction(num, den)); |
18239 | 92 |
} |
93 |
#else |
|
13 | 94 |
/* this check allows subclassing .. */ |
1 | 95 |
if (self == Fraction) { |
24135 | 96 |
if (__bothSmallInteger(num, den)) { |
97 |
if (den != __mkSmallInteger(0)) { |
|
98 |
if (__CanDoQuickNew(sizeof(struct __Fraction))) { /* OBJECT ALLOCATION */ |
|
99 |
OBJ newFraction; |
|
100 |
INT iDen; |
|
101 |
INT iNum; |
|
19259 | 102 |
|
24135 | 103 |
__qCheckedNew(newFraction, sizeof(struct __Fraction)); |
104 |
__InstPtr(newFraction)->o_class = self; |
|
105 |
__qSTORE(newFraction, self); |
|
19259 | 106 |
|
24135 | 107 |
iDen = __intVal(den); |
108 |
iNum = __intVal(num); |
|
1 | 109 |
|
24135 | 110 |
if (iDen < 0) { |
111 |
iNum = -iNum; |
|
112 |
iDen = -iDen; |
|
113 |
} |
|
114 |
while ( (((iNum | iDen) & 1) == 0) && ( iNum != 0) && ( iDen != 0)) { |
|
115 |
/* both even and non-zero */ |
|
116 |
iNum = iNum >> 1; |
|
117 |
iDen = iDen >> 1; |
|
118 |
} |
|
119 |
if (iNum >= _MAX_INT) { |
|
120 |
__FractionInstPtr(newFraction)->f_numerator = __MKINT(iNum); |
|
121 |
} else { |
|
122 |
__FractionInstPtr(newFraction)->f_numerator = __MKSMALLINT(iNum); |
|
123 |
} |
|
124 |
if (iDen >= _MAX_INT) { |
|
125 |
__FractionInstPtr(newFraction)->f_denominator = __MKINT(iDen); |
|
126 |
} else { |
|
127 |
__FractionInstPtr(newFraction)->f_denominator = __MKSMALLINT(iDen); |
|
128 |
} |
|
129 |
if (iNum == 1) { |
|
130 |
/* no need to reduce */ |
|
131 |
RETURN ( newFraction ); |
|
132 |
} |
|
133 |
} |
|
134 |
} |
|
135 |
} |
|
1 | 136 |
} |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18239
diff
changeset
|
137 |
#endif /* not __SCHTEAM__ */ |
2790 | 138 |
%}. |
8629 | 139 |
den = 0 ifTrue:[ |
24135 | 140 |
^ ZeroDivide raiseRequestWith:thisContext. |
7140
f65ec31fc081
oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents:
7139
diff
changeset
|
141 |
]. |
2792 | 142 |
newFraction isNil ifTrue:[ |
24135 | 143 |
newFraction := self basicNew setNumerator:num denominator:den. |
2792 | 144 |
]. |
145 |
^ newFraction reduced |
|
7140
f65ec31fc081
oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents:
7139
diff
changeset
|
146 |
|
f65ec31fc081
oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents:
7139
diff
changeset
|
147 |
" |
24135 | 148 |
Fraction numerator:1 denominator:3 -> (1/3) |
149 |
Fraction numerator:-1 denominator:3 -> (-1/3) |
|
150 |
Fraction numerator:1 denominator:-3 -> (-1/3) |
|
151 |
Fraction numerator:-1 denominator:-3 -> (1/3) |
|
152 |
||
20308 | 153 |
Fraction numerator:2 denominator:3 |
154 |
Fraction numerator:2 denominator:6 |
|
19259 | 155 |
|
156 |
Fraction numerator:1 denominator:0 -> error |
|
157 |
Fraction numerator:2 denominator:0 -> error |
|
7140
f65ec31fc081
oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents:
7139
diff
changeset
|
158 |
|
20308 | 159 |
Fraction numerator:5 denominator:10 |
160 |
Fraction numerator:50 denominator:100 |
|
161 |
Fraction numerator:8 denominator:16 |
|
7140
f65ec31fc081
oops - noone cared for generating fractions with 0 denominator
Claus Gittinger <cg@exept.de>
parents:
7139
diff
changeset
|
162 |
" |
19259 | 163 |
|
19261 | 164 |
"Modified: / 27-02-2016 / 00:25:47 / cg" |
11899 | 165 |
! |
166 |
||
16938 | 167 |
readDecimalFractionFrom:aStringOrStream onError:exceptionBlock |
168 |
"Read an arbitrary number (>0) of digits representing a decimal fraction." |
|
169 |
||
22895 | 170 |
|numDigits factor fraction s ch denom| |
16938 | 171 |
|
172 |
s := aStringOrStream isStream ifTrue:[aStringOrStream] ifFalse:[aStringOrStream readStream]. |
|
173 |
||
174 |
factor := (1 / 10). |
|
175 |
fraction := 0. |
|
22895 | 176 |
numDigits := 0. |
16938 | 177 |
|
178 |
[ |
|
22895 | 179 |
ch := s peekOrNil. |
180 |
ch notNil and:[ch isDigit]. |
|
16938 | 181 |
] whileTrue: [ |
22895 | 182 |
s next. |
183 |
fraction := fraction*10 + (ch digitValue). |
|
184 |
numDigits := numDigits + 1. |
|
16938 | 185 |
]. |
186 |
||
22895 | 187 |
numDigits > 0 ifFalse:[^ exceptionBlock valueWithOptionalArgument: 'Missing digits in fraction']. |
188 |
numDigits <= 10 ifTrue:[ |
|
189 |
denom := #(10 100 1000 10000 100000 |
|
190 |
1000000 10000000 100000000 1000000000 10000000000) at:numDigits. |
|
191 |
] ifFalse:[ |
|
192 |
denom := 10 raisedTo:numDigits. |
|
193 |
]. |
|
194 |
^ fraction / denom |
|
16938 | 195 |
|
196 |
" |
|
197 |
Fraction readDecimalFractionFrom:'1' onError:[nil] -> 0.1 |
|
18239 | 198 |
Fraction readDecimalFractionFrom:'123' onError:[nil] -> 0.123 |
199 |
Fraction readDecimalFractionFrom:'5' onError:[nil] -> 0.5 |
|
200 |
Fraction readDecimalFractionFrom:'005' onError:[nil] -> 0.005 |
|
16938 | 201 |
Fraction readDecimalFractionFrom:'' onError:[nil] -> nil |
202 |
Fraction readDecimalFractionFrom:'aa' onError:[nil] -> nil |
|
203 |
" |
|
204 |
! |
|
205 |
||
11899 | 206 |
readFrom:aStringOrStream onError:exceptionBlock |
22607 | 207 |
|s numerator denominator hasParen result| |
11899 | 208 |
|
11901 | 209 |
"/ sigh - care for subclasses... |
210 |
self == Fraction ifFalse:[ |
|
22607 | 211 |
^ super readFrom:aStringOrStream onError:exceptionBlock |
11901 | 212 |
]. |
213 |
||
11899 | 214 |
s := aStringOrStream readStream. |
215 |
s skipSeparators. |
|
22607 | 216 |
hasParen := s peekFor:$(. |
11899 | 217 |
|
218 |
numerator := super readFrom:s onError:[^ exceptionBlock value]. |
|
219 |
numerator isInteger ifTrue:[ |
|
22607 | 220 |
s skipSeparators. |
221 |
(s peekFor:$/) ifTrue:[ |
|
222 |
denominator := Integer readFrom:s onError:[^ exceptionBlock value]. |
|
223 |
result := self numerator:numerator denominator:denominator |
|
22608 | 224 |
] ifFalse:[ |
225 |
result := numerator. |
|
22607 | 226 |
]. |
11899 | 227 |
]. |
22607 | 228 |
hasParen ifTrue:[ |
229 |
s skipSeparators. |
|
230 |
(s peekFor:$)) ifFalse:exceptionBlock. |
|
231 |
]. |
|
232 |
result notNil ifTrue:[ |
|
233 |
^ result. |
|
234 |
]. |
|
235 |
^ numerator asFraction. |
|
236 |
||
11899 | 237 |
|
238 |
" |
|
22607 | 239 |
Fraction readFromString:'1' |
240 |
Fraction readFromString:'2' |
|
241 |
Fraction readFromString:'1.5' |
|
242 |
Fraction readFromString:'1/5' |
|
243 |
Fraction readFromString:'(1/5)' |
|
244 |
Fraction readFromString:'(1/5' |
|
11899 | 245 |
" |
22607 | 246 |
|
22608 | 247 |
"Modified: / 14-03-2018 / 19:02:44 / stefan" |
1 | 248 |
! ! |
249 |
||
7442 | 250 |
!Fraction class methodsFor:'class initialization'! |
251 |
||
252 |
initialize |
|
253 |
FractionZero isNil ifTrue:[ |
|
18239 | 254 |
FractionZero := self numerator:0 denominator:1. |
255 |
FractionOne := self numerator:1 denominator:1 |
|
7442 | 256 |
] |
257 |
! ! |
|
258 |
||
21819 | 259 |
!Fraction class methodsFor:'coercing & converting'! |
260 |
||
261 |
coerce:aNumber |
|
262 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
|
263 |
||
264 |
^ aNumber asFraction |
|
265 |
! ! |
|
266 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
267 |
!Fraction class methodsFor:'constants'! |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
268 |
|
21974 | 269 |
e |
270 |
"return an approximation of the constant e as Fraction. |
|
271 |
The approx. returned here has an error smaller than representable by float instances |
|
272 |
(roughly 26 valid digits)" |
|
273 |
||
274 |
E isNil ifTrue:[ |
|
275 |
E := self |
|
276 |
numerator: 271828182845904523536028747 |
|
277 |
denominator:100000000000000000000000000 |
|
278 |
]. |
|
279 |
^ E |
|
280 |
||
281 |
"E := nil |
|
282 |
||
283 |
Fraction e |
|
284 |
Fraction e asFloat - Float e |
|
285 |
Fraction e asLongFloat - LongFloat e |
|
286 |
Float e |
|
287 |
FixedPoint e |
|
288 |
" |
|
289 |
||
290 |
"Created: / 03-07-2017 / 17:22:47 / cg" |
|
24265 | 291 |
"Modified (comment): / 06-06-2019 / 17:10:56 / Claus Gittinger" |
21974 | 292 |
! |
293 |
||
294 |
e_approximation |
|
295 |
"return an approximation of e as Fraction. |
|
296 |
The approx. returned is good for 6 valid digits and has an error of less than -2.67-07. |
|
297 |
The value might be useful to avoid floating point numbers in graphic rendering code, |
|
298 |
where 6 digits of precision are usually good enough." |
|
299 |
||
300 |
^ self |
|
301 |
numerator:67957 |
|
302 |
denominator:25000 |
|
303 |
||
304 |
" |
|
305 |
Fraction e |
|
306 |
Fraction e asFloat |
|
307 |
Fraction e_approximation asFloat |
|
308 |
||
309 |
(Fraction e - Fraction e_approximation asFloat) abs |
|
310 |
(Float e - Fraction e_approximation asFloat) abs |
|
311 |
||
312 |
19/7 can be used as an approx with ~1% error: |
|
313 |
Float e - (19/7) asFloat |
|
314 |
||
315 |
87/32 is another candidate: |
|
316 |
Float e - (87/32) asFloat |
|
317 |
||
318 |
and 106/39: |
|
319 |
Float e - (106/39) asFloat |
|
320 |
" |
|
321 |
||
322 |
"Created: / 03-07-2017 / 17:21:38 / cg" |
|
323 |
! |
|
324 |
||
24931 | 325 |
phi |
326 |
"return an approximation of the constant phi as Fraction. |
|
327 |
The approx. returned here has an error smaller than representable by float instances |
|
328 |
(roughly 26 valid digits)" |
|
329 |
||
330 |
Phi isNil ifTrue:[ |
|
331 |
Phi := self |
|
332 |
numerator: 161803398874989484820458683 |
|
333 |
denominator:100000000000000000000000000 |
|
334 |
]. |
|
335 |
^ Phi |
|
336 |
||
337 |
" |
|
338 |
Fraction phi |
|
339 |
Fraction phi asFloat - Float phi |
|
340 |
Fraction phi asLongFloat - LongFloat phi |
|
341 |
Float phi |
|
342 |
" |
|
343 |
||
344 |
"Modified (comment): / 03-07-2017 / 13:21:03 / cg" |
|
345 |
! |
|
346 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
347 |
pi |
11671 | 348 |
"return an approximation of the constant pi as Fraction. |
21944 | 349 |
The approx. returned here has an error smaller than representable by float instances |
350 |
(roughly 26 valid digits)" |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
351 |
|
21942 | 352 |
Pi isNil ifTrue:[ |
353 |
Pi := self |
|
354 |
numerator: 314159265358979323846264343 |
|
355 |
denominator:100000000000000000000000000 |
|
356 |
]. |
|
357 |
^ Pi |
|
358 |
||
359 |
" |
|
360 |
^ self |
|
361 |
numerator: 314159265358979323846264338327950288419716939937510582097494459 |
|
362 |
denominator:100000000000000000000000000000000000000000000000000000000000000 |
|
363 |
" |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
364 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
365 |
" |
18239 | 366 |
Fraction pi |
13357 | 367 |
Fraction pi asFloat - Float pi |
19248 | 368 |
Fraction pi asLongFloat - LongFloat pi |
18239 | 369 |
Float pi |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
370 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
371 |
|
21944 | 372 |
"Modified (comment): / 03-07-2017 / 13:21:03 / cg" |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
373 |
! |
701 | 374 |
|
19248 | 375 |
pi1000 |
21944 | 376 |
"return an approximation of the constant pi as Fraction (>= 1000 valid decimal digits)." |
19248 | 377 |
|
21942 | 378 |
Pi_1000 isNil ifTrue:[ |
379 |
Pi_1000 := self |
|
380 |
numerator: 31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788 |
|
381 |
denominator:10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000. |
|
19248 | 382 |
]. |
21942 | 383 |
^ Pi_1000 |
19248 | 384 |
|
385 |
" |
|
21942 | 386 |
Pi_1000 := nil. |
19248 | 387 |
Fraction pi1000 |
388 |
Fraction pi1000 asLongFloat - LongFloat pi |
|
389 |
LongFloat pi |
|
390 |
" |
|
391 |
||
21944 | 392 |
"Modified (comment): / 03-07-2017 / 13:21:15 / cg" |
19248 | 393 |
! |
394 |
||
11671 | 395 |
pi_approximation |
396 |
"return an approximation of the constant pi as Fraction. |
|
397 |
The approx. returned is good for 6 valid digits and has an error of less than -2.67-07. |
|
398 |
The value might be useful to avoid floating point numbers in graphic rendering code, |
|
399 |
where 6 digits of precision are usually good enough." |
|
400 |
||
18239 | 401 |
^ self |
21942 | 402 |
numerator:355 |
403 |
denominator:113 |
|
11671 | 404 |
|
405 |
" |
|
18239 | 406 |
Fraction pi |
11671 | 407 |
Fraction pi asFloat |
21942 | 408 |
|
21943 | 409 |
(Fraction pi - Fraction pi_approximation asFloat) abs |
21942 | 410 |
(Float pi - Fraction pi_approximation asFloat) abs |
21974 | 411 |
|
412 |
22/7 can be used as an approx with 1% error: |
|
413 |
Float pi - (22/7) asFloat |
|
11671 | 414 |
" |
21942 | 415 |
|
21974 | 416 |
"Modified (comment): / 03-07-2017 / 17:31:44 / cg" |
11671 | 417 |
! |
418 |
||
701 | 419 |
unity |
1555 | 420 |
"return the neutral element for multiplication (1 / 1)" |
701 | 421 |
|
422 |
^ FractionOne |
|
1555 | 423 |
|
424 |
"Modified: 18.7.1996 / 12:26:06 / cg" |
|
701 | 425 |
! |
426 |
||
427 |
zero |
|
1555 | 428 |
"return the neutral element for addition (0 / 1)" |
701 | 429 |
|
430 |
^ FractionZero |
|
1555 | 431 |
|
432 |
"Modified: 18.7.1996 / 12:26:12 / cg" |
|
701 | 433 |
! ! |
434 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
435 |
!Fraction class methodsFor:'queries'! |
3 | 436 |
|
437 |
isBuiltInClass |
|
1264 | 438 |
"return true if this class is known by the run-time-system. |
439 |
Here, true is returned for myself, false for subclasses." |
|
3 | 440 |
|
441 |
^ self == Fraction |
|
1264 | 442 |
|
443 |
"Modified: 23.4.1996 / 15:59:10 / cg" |
|
3 | 444 |
! ! |
445 |
||
1 | 446 |
!Fraction methodsFor:'accessing'! |
447 |
||
448 |
denominator |
|
449 |
"return the denominator" |
|
450 |
||
451 |
^ denominator |
|
452 |
! |
|
453 |
||
701 | 454 |
numerator |
455 |
"return the numerator" |
|
1 | 456 |
|
701 | 457 |
^ numerator |
1 | 458 |
! ! |
459 |
||
460 |
!Fraction methodsFor:'arithmetic'! |
|
461 |
||
701 | 462 |
* aNumber |
11734 | 463 |
"return the product of the receiver and the argument." |
701 | 464 |
|
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
465 |
"/ notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
466 |
"/ the following code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
467 |
"/ and exists as an optimization, to speed up those cases. |
7355 | 468 |
"/ also notice, that checks for those cases must be inlinable without |
469 |
"/ a message send; otherwise double-dispatch is just as fast. |
|
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
470 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
471 |
"/ Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
472 |
"/ mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
473 |
"/ (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
474 |
|
7355 | 475 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
22254 | 476 |
^ self class |
477 |
numerator:(numerator * aNumber) |
|
478 |
denominator:denominator |
|
701 | 479 |
]. |
22254 | 480 |
aNumber isFloat ifTrue:[ |
481 |
^ (numerator * aNumber) / denominator |
|
701 | 482 |
]. |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
483 |
|
701 | 484 |
^ aNumber productFromFraction:self |
1892 | 485 |
|
22254 | 486 |
" |
487 |
2/3 * 3 asLongFloat |
|
488 |
" |
|
489 |
||
490 |
"Modified: / 28-07-1997 / 19:09:23 / cg" |
|
491 |
"Modified (comment): / 14-09-2017 / 15:27:30 / stefan" |
|
701 | 492 |
! |
493 |
||
1 | 494 |
+ aNumber |
495 |
"return the sum of the receiver and the argument, aNumber" |
|
496 |
||
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
497 |
"/ notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
498 |
"/ the following code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
499 |
"/ and exists as an optimization, to speed up those cases. |
7355 | 500 |
"/ also notice, that checks for those cases must be inlinable without |
501 |
"/ a message send; otherwise double-dispatch is just as fast. |
|
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
502 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
503 |
"/ Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
504 |
"/ mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
505 |
"/ (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
506 |
|
7355 | 507 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
22254 | 508 |
^ self class |
509 |
numerator:(numerator + (denominator * aNumber)) |
|
510 |
denominator:denominator |
|
1 | 511 |
]. |
16 | 512 |
(aNumber isMemberOf:Float) ifTrue:[ |
22254 | 513 |
^ (numerator asFloat / denominator asFloat) + aNumber |
16 | 514 |
]. |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
515 |
|
1 | 516 |
^ aNumber sumFromFraction:self |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
517 |
|
22254 | 518 |
" |
519 |
2/3 + 10 asLongFloat |
|
520 |
" |
|
521 |
||
522 |
"Modified: / 28-07-1997 / 19:09:16 / cg" |
|
523 |
"Modified (comment): / 14-09-2017 / 15:26:46 / stefan" |
|
1 | 524 |
! |
525 |
||
526 |
- aNumber |
|
527 |
"return the difference of the receiver and the argument, aNumber" |
|
528 |
||
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
529 |
"/ notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
530 |
"/ the following code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
531 |
"/ and exists as an optimization, to speed up those cases. |
7355 | 532 |
"/ also notice, that checks for those cases must be inlinable without |
533 |
"/ a message send; otherwise double-dispatch is just as fast. |
|
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
534 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
535 |
"/ Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
536 |
"/ mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
537 |
"/ (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
538 |
|
7355 | 539 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
18239 | 540 |
^ self class |
541 |
numerator:(numerator - (denominator * aNumber)) |
|
542 |
denominator:denominator |
|
1 | 543 |
]. |
16 | 544 |
(aNumber isMemberOf:Float) ifTrue:[ |
18239 | 545 |
^ (numerator asFloat / denominator asFloat) - aNumber |
16 | 546 |
]. |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
547 |
|
1 | 548 |
^ aNumber differenceFromFraction:self |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
549 |
|
1892 | 550 |
" |
18239 | 551 |
(1/3) - (1/9) |
552 |
(1/9) - (1/3) |
|
553 |
(999/1000) - (1/1000) |
|
554 |
(999/1000) - (1/1000000) |
|
555 |
(999000/1000000) - (1/1000000) |
|
1892 | 556 |
" |
557 |
||
2792 | 558 |
"Modified: 28.7.1997 / 19:09:11 / cg" |
1 | 559 |
! |
560 |
||
561 |
/ aNumber |
|
562 |
"return the quotient of the receiver and the argument, aNumber" |
|
563 |
||
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
564 |
"/ notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
565 |
"/ the following code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
566 |
"/ and exists as an optimization, to speed up those cases. |
7355 | 567 |
"/ also notice, that checks for those cases must be inlinable without |
568 |
"/ a message send; otherwise double-dispatch is just as fast. |
|
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
569 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
570 |
"/ Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
571 |
"/ mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
572 |
"/ (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
573 |
|
7355 | 574 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
22254 | 575 |
^ self class |
576 |
numerator:numerator |
|
577 |
denominator:(denominator * aNumber) |
|
16 | 578 |
]. |
22254 | 579 |
aNumber isFloat ifTrue:[ |
580 |
^ numerator / (denominator * aNumber) |
|
16 | 581 |
]. |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
582 |
|
1 | 583 |
^ aNumber quotientFromFraction:self |
1892 | 584 |
|
22254 | 585 |
"Modified: / 28-07-1997 / 19:09:06 / cg" |
586 |
"Modified: / 14-09-2017 / 15:24:48 / stefan" |
|
1 | 587 |
! |
588 |
||
589 |
// aNumber |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
590 |
"return the integer quotient of dividing the receiver by aNumber with |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
591 |
truncation towards negative infinity." |
1 | 592 |
|
593 |
^ (numerator * aNumber denominator) // (denominator * aNumber numerator) |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
594 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
595 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
596 |
0.5 // 1 |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
597 |
-0.5 // 1 |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3899
diff
changeset
|
598 |
(1/2) // 1 = 0 ifFalse:[self halt]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3899
diff
changeset
|
599 |
(-1/2) // 1 = -1 ifFalse:[self halt]. |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
600 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
601 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3899
diff
changeset
|
602 |
"Modified: / 5.11.1996 / 11:47:14 / cg" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3899
diff
changeset
|
603 |
"Modified: / 13.2.1998 / 09:15:35 / stefan" |
1 | 604 |
! |
605 |
||
606 |
negated |
|
607 |
"optional - could use inherited method ..." |
|
608 |
||
18861 | 609 |
"/ no need to reduce - I am already |
610 |
^ self class basicNew |
|
20308 | 611 |
setNumerator:(numerator negated) |
612 |
denominator:denominator |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
613 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
614 |
"Modified: 5.11.1996 / 10:29:11 / cg" |
1 | 615 |
! |
616 |
||
617 |
reciprocal |
|
618 |
"optional - could use inherited method ..." |
|
619 |
||
620 |
numerator == 1 ifTrue:[^ denominator]. |
|
18861 | 621 |
"/ no need to reduce - I am already |
622 |
^ self class basicNew |
|
20308 | 623 |
setNumerator:denominator |
624 |
denominator:numerator |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
625 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
626 |
"Modified: 5.11.1996 / 10:29:22 / cg" |
1 | 627 |
! ! |
628 |
||
701 | 629 |
!Fraction methodsFor:'coercing & converting'! |
630 |
||
1892 | 631 |
asFixedPoint |
632 |
"return the receiver as fixedPoint number. |
|
633 |
Q: what should the scale be here ?" |
|
634 |
||
635 |
^ FixedPoint numerator:numerator denominator:denominator scale:2 |
|
636 |
||
637 |
" |
|
638 |
(1/2) asFixedPoint |
|
639 |
" |
|
640 |
||
641 |
"Created: 5.11.1996 / 15:15:54 / cg" |
|
642 |
! |
|
643 |
||
2140 | 644 |
asFixedPoint:scale |
645 |
"return the receiver as fixedPoint number, with the given number |
|
646 |
of post-decimal-point digits." |
|
647 |
||
648 |
^ FixedPoint numerator:numerator denominator:denominator scale:scale |
|
649 |
||
650 |
" |
|
18239 | 651 |
(1/2) asFixedPoint:2 |
652 |
(1/3) asFixedPoint:2 |
|
653 |
(1/3) asFixedPoint:5 |
|
654 |
(2/3) asFixedPoint:2 |
|
655 |
(2/3) asFixedPoint:5 |
|
2140 | 656 |
" |
657 |
||
658 |
"Created: 5.11.1996 / 15:15:54 / cg" |
|
659 |
"Modified: 10.1.1997 / 19:54:50 / cg" |
|
660 |
! |
|
661 |
||
701 | 662 |
asFloat |
11239 | 663 |
"return a float with (approximately) my value. |
664 |
Since floats have a limited precision, you usually loose bits when doing this." |
|
701 | 665 |
|
7442 | 666 |
|num den numShift denShift bits rslt| |
6571
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
667 |
|
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
668 |
(numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[ |
18239 | 669 |
^ (numerator asFloat) / (denominator asFloat) |
6571
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
670 |
]. |
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
671 |
|
6576 | 672 |
"Do it the hard way: reduce magnitude and undo reduction on the quotient" |
6571
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
673 |
|
6576 | 674 |
bits := Float precision * 2. "number of bits to preserve (conservative)" |
6571
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
675 |
num := numerator abs. |
6576 | 676 |
numShift := bits - num highBit. "(num highBit - bits) negated" |
677 |
numShift < 0 ifTrue:[num := num bitShift:numShift] ifFalse:[numShift := 0]. |
|
678 |
||
679 |
den := denominator. |
|
680 |
denShift := bits - den highBit. "(den highBit - bits) negated" |
|
681 |
denShift < 0 ifTrue:[den := den bitShift:denShift] ifFalse:[denShift := 0]. |
|
682 |
||
7442 | 683 |
rslt := (num asFloat / den asFloat) * (2 raisedToInteger:denShift-numShift). |
684 |
numerator negative ifTrue:[ ^ rslt negated ]. |
|
685 |
^ rslt. |
|
6571
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
686 |
|
18239 | 687 |
" |
6576 | 688 |
(5/9) asFloat |
689 |
(-5/9) asFloat |
|
690 |
(500000000000/900000000000) asFloat |
|
691 |
(-500000000000/900000000000) asFloat |
|
692 |
(500000000000/9) asFloat |
|
693 |
(5/900000000000) asFloat |
|
694 |
89012345678901234567 asFloat / 123456789123456789 asFloat |
|
695 |
(89012345678901234567 / 123456789123456789) asFloat |
|
696 |
||
697 |
( |
|
698 |
180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125 |
|
18239 | 699 |
/ |
6576 | 700 |
180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250 |
701 |
) asFloat |
|
702 |
||
703 |
180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125 |
|
18239 | 704 |
asFloat / |
6576 | 705 |
180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250 |
18239 | 706 |
asFloat |
6576 | 707 |
" |
701 | 708 |
! |
1 | 709 |
|
701 | 710 |
asFraction |
20416 | 711 |
"return the receiver as fraction - that's the receiver itself" |
701 | 712 |
|
713 |
^ self |
|
714 |
! |
|
715 |
||
716 |
asInteger |
|
717 |
"return an integer with my value - will usually truncate" |
|
1 | 718 |
|
719 |
^ numerator // denominator |
|
720 |
! |
|
721 |
||
7442 | 722 |
asLargeFloat |
723 |
"return a large float with (approximately) my value" |
|
724 |
||
725 |
^ (numerator asLargeFloat) / (denominator asLargeFloat) |
|
726 |
||
727 |
" |
|
728 |
(5/9) asLargeFloat |
|
729 |
(500000000000/900000000000) asLargeFloat |
|
730 |
(500000000000/9) asLargeFloat |
|
731 |
" |
|
732 |
! |
|
733 |
||
24201 | 734 |
asLargeFloatPrecision:n |
735 |
"Answer a Floating point with arbitrary precision |
|
736 |
close to the receiver." |
|
737 |
||
738 |
"Note: form below would not be the closest approximation |
|
739 |
^ (numerator asLargeFloatPrecision: n) |
|
740 |
inPlaceDivideBy: (denominator asLargeFloatPrecision: n)" |
|
741 |
||
742 |
^ LargeFloat fromFraction:self precision:n |
|
743 |
||
744 |
" |
|
745 |
(5/9) asFloat 0.555555555555556 |
|
746 |
(5/9) asLargeFloatPrecision:200 0.555556 |
|
747 |
(5/9) asLargeFloat |
|
748 |
||
749 |
(500000000000/900000000000) asFloat * 900000000000 - 500000000000 |
|
750 |
((500000000000/900000000000) asLargeFloatPrecision:200) * 900000000000 - 500000000000 |
|
751 |
(500000000000/900000000000) asLargeFloat * 900000000000 - 500000000000 |
|
752 |
||
753 |
(500000000000/9) asLargeFloatPrecision:200 |
|
754 |
(500000000000/9) asLargeFloat:200 |
|
755 |
" |
|
756 |
||
757 |
"Created: / 27-05-2019 / 08:30:08 / Claus Gittinger" |
|
758 |
"Modified (comment): / 28-05-2019 / 06:04:48 / Claus Gittinger" |
|
759 |
! |
|
760 |
||
701 | 761 |
asLargeInteger |
762 |
"return an integer with my value - will usually truncate" |
|
763 |
||
764 |
^ self asInteger asLargeInteger |
|
765 |
! |
|
766 |
||
7355 | 767 |
asLongFloat |
15278 | 768 |
"return a long float with (approximately) my value. |
769 |
Since floats have a limited precision, you usually loose bits when doing this." |
|
7355 | 770 |
|
7442 | 771 |
|num den numShift denShift numBits rslt| |
7355 | 772 |
|
773 |
(numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[ |
|
18239 | 774 |
^ (numerator asLongFloat) / (denominator asLongFloat) |
7355 | 775 |
]. |
776 |
||
777 |
"Do it the hard way: reduce magnitude and undo reduction on the quotient" |
|
778 |
||
7442 | 779 |
numBits := LongFloat precision * 2. "number of bits to preserve (conservative)" |
7355 | 780 |
num := numerator abs. |
7442 | 781 |
numShift := numBits - num highBit. "(num highBit - bits) negated" |
15278 | 782 |
numShift < 0 ifTrue:[num := num bitShift:numShift] ifFalse:[ numShift := 0]. |
7355 | 783 |
|
784 |
den := denominator. |
|
7442 | 785 |
denShift := numBits - den highBit. "(den highBit - bits) negated" |
15278 | 786 |
denShift < 0 ifTrue:[den := den bitShift:denShift] ifFalse:[denShift := 0]. |
7355 | 787 |
|
7730
dda82553acc7
make #asLongFloat return a LongFloat (returned Float)
Stefan Vogel <sv@exept.de>
parents:
7571
diff
changeset
|
788 |
rslt := (num asLongFloat / den asLongFloat) * (2 raisedToInteger:denShift-numShift). |
7442 | 789 |
numerator negative ifTrue:[ ^ rslt negated ]. |
790 |
^ rslt. |
|
7355 | 791 |
|
18239 | 792 |
" |
793 |
(5/9) asLongFloat |
|
794 |
(-5/9) asLongFloat |
|
7442 | 795 |
(Fraction basicNew setNumerator:500000000000 denominator:900000000000) asLongFloat = (5/9) asLongFloat |
796 |
(Fraction basicNew setNumerator:500000000001 denominator:900000000000) asLongFloat = (5/9) asLongFloat |
|
18239 | 797 |
(500000000001/900000000000) asLongFloat |
798 |
(-500000000001/900000000000) asLongFloat |
|
7442 | 799 |
(500000000001/900000000000) asLongFloat = (5/9) asLongFloat |
800 |
||
18239 | 801 |
(500000000000/9) asLongFloat |
802 |
(5/900000000000) asLongFloat |
|
803 |
89012345678901234567 asFloat / 123456789123456789 asLongFloat |
|
804 |
(89012345678901234567 / 123456789123456789) asLongFloat |
|
805 |
(-89012345678901234567 / 123456789123456789) asLongFloat |
|
7355 | 806 |
|
807 |
( |
|
808 |
180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125 |
|
18239 | 809 |
/ |
7355 | 810 |
180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250 |
18239 | 811 |
) asLongFloat |
7355 | 812 |
|
813 |
180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125 |
|
18239 | 814 |
asLongFloat / |
7355 | 815 |
180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250 |
18239 | 816 |
asLongFloat |
7355 | 817 |
" |
818 |
! |
|
819 |
||
1200 | 820 |
asShortFloat |
6576 | 821 |
"return a short float with (approximately) my value" |
822 |
||
823 |
(numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[ |
|
18239 | 824 |
^ (numerator asShortFloat) / (denominator asShortFloat) |
6576 | 825 |
]. |
1200 | 826 |
|
6576 | 827 |
^ self asFloat asShortFloat |
1200 | 828 |
|
6576 | 829 |
" |
830 |
(5/9) asShortFloat |
|
831 |
(500000000000/900000000000) asShortFloat |
|
832 |
(500000000000/9) asShortFloat |
|
833 |
" |
|
1200 | 834 |
! |
835 |
||
701 | 836 |
coerce:aNumber |
18839 | 837 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
701 | 838 |
|
839 |
^ aNumber asFraction |
|
840 |
! |
|
841 |
||
842 |
generality |
|
843 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
844 |
||
845 |
^ 60 |
|
846 |
! ! |
|
847 |
||
848 |
!Fraction methodsFor:'comparing'! |
|
849 |
||
850 |
< aNumber |
|
851 |
"return true if the receiver is less |
|
852 |
than aNumber, false otherwise." |
|
1 | 853 |
|
701 | 854 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
18239 | 855 |
^ numerator < (denominator * aNumber) |
701 | 856 |
]. |
857 |
^ aNumber lessFromFraction:self |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
858 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
859 |
"Modified: 5.11.1996 / 10:30:52 / cg" |
701 | 860 |
! |
861 |
||
862 |
= aNumber |
|
863 |
"return true, if the argument represents the same numeric value |
|
864 |
as the receiver, false otherwise" |
|
865 |
||
866 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
|
18239 | 867 |
(denominator == 1) ifFalse:[ |
868 |
^ numerator = (aNumber * denominator) |
|
869 |
]. |
|
870 |
^ numerator = aNumber |
|
1 | 871 |
]. |
7355 | 872 |
^ aNumber equalFromFraction:self |
3631
26a44b284912
handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
873 |
|
26a44b284912
handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
874 |
"Modified: / 7.7.1998 / 17:17:07 / cg" |
701 | 875 |
! |
876 |
||
877 |
> aNumber |
|
878 |
"return true if the receiver is greater |
|
879 |
than aNumber, false otherwise." |
|
880 |
"optional - could use inherited method ..." |
|
881 |
||
882 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
|
18239 | 883 |
^ numerator > (denominator * aNumber) |
701 | 884 |
]. |
7355 | 885 |
^ aNumber < self |
4593 | 886 |
! |
887 |
||
888 |
hash |
|
889 |
"return a number for hashing; redefined, since fractions compare |
|
18861 | 890 |
by numeric value (i.e. (1/2) = 0.5), hash values must be the same" |
4593 | 891 |
|
18861 | 892 |
(denominator == 1) ifTrue:[^ numerator hash]. |
893 |
(denominator == -1) ifTrue:[^ numerator hash negated]. |
|
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
894 |
^ self asFloat hash |
4593 | 895 |
|
896 |
" |
|
18239 | 897 |
3 hash |
898 |
(9/3) hash |
|
899 |
3.0 hash |
|
900 |
(1/2) hash |
|
901 |
(1/4) hash |
|
902 |
0.0 hash |
|
903 |
0.5 hash |
|
904 |
0.25 hash |
|
905 |
0.4 hash |
|
20308 | 906 |
|
907 |
0.25 hash |
|
908 |
-0.25 hash |
|
909 |
(1/4) hash |
|
910 |
(-1/4) hash |
|
4593 | 911 |
" |
5364 | 912 |
! |
913 |
||
914 |
sameFractionValueAs:aNumber |
|
915 |
"return true, if the argument represents the same numeric value |
|
916 |
as the receiver, false otherwise" |
|
917 |
||
918 |
|rSelf rNum| |
|
919 |
||
920 |
rSelf := self reduced. |
|
921 |
rNum := aNumber reduced. |
|
922 |
rSelf denominator = rNum denominator ifTrue:[ |
|
18239 | 923 |
^ rSelf numerator = rNum numerator |
5364 | 924 |
]. |
925 |
^ false |
|
1 | 926 |
! ! |
927 |
||
928 |
!Fraction methodsFor:'double dispatching'! |
|
929 |
||
7355 | 930 |
differenceFromFixedPoint:aFixedPoint |
931 |
|n d otherDenominator otherNumerator| |
|
932 |
||
933 |
otherDenominator := aFixedPoint denominator. |
|
934 |
otherNumerator := aFixedPoint numerator. |
|
935 |
||
936 |
"save a multiplication if possible" |
|
937 |
otherDenominator == denominator ifTrue:[ |
|
18239 | 938 |
n := otherNumerator - numerator. |
939 |
d := otherDenominator. |
|
7355 | 940 |
] ifFalse:[ |
18239 | 941 |
n := (otherNumerator * denominator) - (numerator * otherDenominator). |
942 |
d := otherDenominator * denominator. |
|
7355 | 943 |
]. |
18239 | 944 |
^ aFixedPoint class |
945 |
numerator:n |
|
946 |
denominator:d |
|
947 |
scale:(aFixedPoint scale) |
|
7355 | 948 |
|
949 |
" |
|
18239 | 950 |
((1/3) asFixedPoint:2) - (1/3) |
951 |
((1/3) asFixedPoint:2) - (2/3) |
|
7355 | 952 |
" |
953 |
! |
|
954 |
||
701 | 955 |
differenceFromFloat:aFloat |
956 |
"sent when a float does not know how to subtract the receiver, a fraction" |
|
1 | 957 |
|
701 | 958 |
^ (aFloat * denominator - numerator) / denominator |
1 | 959 |
! |
960 |
||
7355 | 961 |
differenceFromFraction:aFraction |
962 |
|n d otherDenominator otherNumerator| |
|
963 |
||
964 |
otherDenominator := aFraction denominator. |
|
965 |
otherNumerator := aFraction numerator. |
|
966 |
||
967 |
"save a multiplication if possible" |
|
968 |
otherDenominator == denominator ifTrue:[ |
|
18239 | 969 |
n := otherNumerator - numerator. |
970 |
d := otherDenominator. |
|
7355 | 971 |
] ifFalse:[ |
18239 | 972 |
n := (otherNumerator * denominator) - (numerator * otherDenominator). |
973 |
d := otherDenominator * denominator. |
|
7355 | 974 |
]. |
18239 | 975 |
^ aFraction class |
976 |
numerator:n |
|
977 |
denominator:d |
|
7355 | 978 |
|
979 |
" |
|
18239 | 980 |
((1/3) asFixedPoint:2) - (1/3) |
981 |
((1/3) asFixedPoint:2) - (2/3) |
|
7355 | 982 |
" |
983 |
! |
|
984 |
||
1 | 985 |
differenceFromInteger:anInteger |
213 | 986 |
"sent when an integer does not know how to subtract the receiver, a fraction" |
1 | 987 |
|
18239 | 988 |
^ self class |
989 |
numerator:((anInteger * denominator) - numerator) |
|
990 |
denominator:denominator |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
991 |
|
2792 | 992 |
"Modified: 28.7.1997 / 19:08:53 / cg" |
1 | 993 |
! |
994 |
||
7355 | 995 |
equalFromFraction:aFraction |
7571
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
996 |
denominator = aFraction denominator ifFalse:[ |
18239 | 997 |
^ false " must always be reduced " |
998 |
"/ ^ (numerator * aFraction denominator) = (aFraction numerator * denominator) |
|
7571
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
999 |
]. |
7355 | 1000 |
^ numerator = aFraction numerator |
1001 |
! |
|
1002 |
||
1003 |
equalFromInteger:anInteger |
|
1004 |
"sent when an integer does not know how to compare to the receiver, a fraction" |
|
1005 |
||
1006 |
"as I am always reduced, this test should not be required. |
|
1007 |
However, it is here for subclasses and to allow comparing unnormalized fractions, |
|
1008 |
which might be encountered internally" |
|
1009 |
||
7571
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
1010 |
denominator = 1 ifFalse:[ |
18239 | 1011 |
^ numerator = (anInteger * denominator) |
7571
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
1012 |
]. |
7355 | 1013 |
^ numerator = anInteger |
7571
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
1014 |
|
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
1015 |
" |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
1016 |
1 = (1 asFixedPoint:1) |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
1017 |
(1 asFixedPoint:1) = 1 |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
1018 |
" |
7355 | 1019 |
! |
1020 |
||
10486 | 1021 |
lessEqFromInteger:anInteger |
1022 |
"sent when an integer does not know how to compare to the receiver, a fraction" |
|
1023 |
||
1024 |
^ (denominator * anInteger) <= numerator |
|
1025 |
! |
|
1026 |
||
7355 | 1027 |
lessFromFraction:aFraction |
18861 | 1028 |
"sent when a fraction does not know how to compare to the receiver. |
1029 |
Return true if aFraction < self." |
|
7355 | 1030 |
|
1031 |
|n d| |
|
1032 |
||
1033 |
d := aFraction denominator. |
|
1034 |
n := aFraction numerator. |
|
1035 |
||
1036 |
"/ save a multiplication if possible |
|
1037 |
d == denominator ifTrue:[ |
|
20308 | 1038 |
^ n < numerator |
7355 | 1039 |
]. |
18239 | 1040 |
^ (denominator * n) < (numerator * d) |
7355 | 1041 |
! |
1042 |
||
701 | 1043 |
lessFromInteger:anInteger |
18861 | 1044 |
"sent when an integer does not know how to compare to the receiver, a fraction. |
1045 |
Return true if anInteger < self." |
|
701 | 1046 |
|
1047 |
^ (denominator * anInteger) < numerator |
|
1048 |
! |
|
1049 |
||
7355 | 1050 |
productFromFixedPoint:aFixedPoint |
18239 | 1051 |
^ aFixedPoint class |
1052 |
numerator:(aFixedPoint numerator * numerator) |
|
1053 |
denominator:(aFixedPoint denominator * denominator) |
|
1054 |
scale:(aFixedPoint scale) |
|
7355 | 1055 |
|
1056 |
" |
|
18239 | 1057 |
((1/3) asFixedPoint:2) * 2 |
1058 |
((1/3) asFixedPoint:2) * (1/2) |
|
1059 |
((1/3) asFixedPoint:2) * (3/2) |
|
7355 | 1060 |
" |
1061 |
! |
|
1062 |
||
701 | 1063 |
productFromFloat:aFloat |
1064 |
"sent when a float does not know how to multiply the receiver, a fraction" |
|
1065 |
||
1066 |
^ aFloat * numerator / denominator |
|
1067 |
! |
|
1068 |
||
7355 | 1069 |
productFromFraction:aFraction |
18239 | 1070 |
^ aFraction class |
1071 |
numerator:(aFraction numerator * numerator) |
|
1072 |
denominator:(aFraction denominator * denominator) |
|
7355 | 1073 |
|
1074 |
" |
|
18239 | 1075 |
((1/3) asFixedPoint:2) * 2 |
1076 |
((1/3) asFixedPoint:2) * (1/2) |
|
1077 |
((1/3) asFixedPoint:2) * (3/2) |
|
7355 | 1078 |
" |
1079 |
! |
|
1080 |
||
1 | 1081 |
productFromInteger:anInteger |
213 | 1082 |
"sent when an integer does not know how to multiply the receiver, a fraction" |
1 | 1083 |
|
18239 | 1084 |
^ self class |
1085 |
numerator:(anInteger * numerator) |
|
1086 |
denominator:denominator |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1087 |
|
2792 | 1088 |
"Modified: 28.7.1997 / 19:06:22 / cg" |
1 | 1089 |
! |
1090 |
||
7355 | 1091 |
quotientFromFixedPoint:aFixedPoint |
7442 | 1092 |
"Return the quotient of the argument, aFixedPoint and the receiver. |
1093 |
Sent when aFixedPoint does not know how to divide by the receiver." |
|
1094 |
||
18239 | 1095 |
^ aFixedPoint class |
1096 |
numerator:(aFixedPoint numerator * denominator) |
|
1097 |
denominator:(aFixedPoint denominator * numerator) |
|
1098 |
scale:(aFixedPoint scale) |
|
7355 | 1099 |
|
1100 |
" |
|
18239 | 1101 |
((1/3) asFixedPoint:2) / 2 |
1102 |
((1/3) asFixedPoint:2) / (1/2) |
|
7355 | 1103 |
" |
1104 |
! |
|
1105 |
||
701 | 1106 |
quotientFromFloat:aFloat |
7442 | 1107 |
"Return the quotient of the argument, aFloat and the receiver. |
1108 |
Sent when aFloat does not know how to divide by the receiver." |
|
701 | 1109 |
|
1110 |
^ (aFloat * denominator) / numerator |
|
1111 |
! |
|
1112 |
||
7355 | 1113 |
quotientFromFraction:aFraction |
7442 | 1114 |
"Return the quotient of the argument, aFraction and the receiver. |
1115 |
Sent when aFraction does not know how to divide by the receiver." |
|
1116 |
||
18239 | 1117 |
^ aFraction class |
1118 |
numerator:(aFraction numerator * denominator) |
|
1119 |
denominator:(aFraction denominator * numerator) |
|
7355 | 1120 |
|
1121 |
" |
|
18239 | 1122 |
(1/3) / (1/2) |
1123 |
(1/3) / (3/2) |
|
7355 | 1124 |
" |
1125 |
! |
|
1126 |
||
324 | 1127 |
quotientFromInteger:anInteger |
7442 | 1128 |
"Return the quotient of the argument, anInteger and the receiver. |
1129 |
Sent when anInteger does not know how to divide by the receiver." |
|
324 | 1130 |
|
18239 | 1131 |
^ self class |
1132 |
numerator:(anInteger * denominator) |
|
1133 |
denominator:numerator |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1134 |
|
2792 | 1135 |
"Modified: 28.7.1997 / 19:08:46 / cg" |
324 | 1136 |
! |
1137 |
||
22099 | 1138 |
raisedFromFloat:aFloat |
1139 |
"aFloat does not know how to be raised to the receiver" |
|
1140 |
||
1141 |
numerator == 1 ifTrue:[ |
|
1142 |
^ aFloat nthRoot:denominator |
|
1143 |
]. |
|
1144 |
^ self asFloat raisedFromFloat:aFloat |
|
1145 |
||
1146 |
" |
|
1147 |
100 raisedTo:(2/5) asFloat |
|
1148 |
100 raisedTo:(1/5) asFloat |
|
1149 |
||
1150 |
100 nthRoot:2 10.0 |
|
1151 |
100 nthRoot:3 4.641588833612778893 |
|
1152 |
100 raisedTo:(1/3) 3.16227766016838 |
|
1153 |
100 raisedTo:(1/4) 3.16227766016838 |
|
1154 |
100 raisedTo:(1/5) 2.51188643150958 |
|
1155 |
100 raisedTo:(1/6) 2.15443469003188 |
|
1156 |
100 raisedTo:(1/7) 1.93069772888325 |
|
1157 |
" |
|
1158 |
||
1159 |
"Created: / 25-07-2017 / 16:13:26 / cg" |
|
1160 |
! |
|
1161 |
||
7355 | 1162 |
sumFromFixedPoint:aFixedPoint |
1163 |
|n d otherDenominator otherNumerator| |
|
1164 |
||
1165 |
otherDenominator := aFixedPoint denominator. |
|
1166 |
otherNumerator := aFixedPoint numerator. |
|
1167 |
||
1168 |
"save a multiplication if possible" |
|
1169 |
otherDenominator == denominator ifTrue:[ |
|
18239 | 1170 |
n := otherNumerator + numerator. |
1171 |
d := otherDenominator. |
|
7355 | 1172 |
] ifFalse:[ |
18239 | 1173 |
n := (otherNumerator * denominator) + (numerator * otherDenominator). |
1174 |
d := otherDenominator * denominator. |
|
7355 | 1175 |
]. |
18239 | 1176 |
^ aFixedPoint class |
1177 |
numerator:n |
|
1178 |
denominator:d |
|
1179 |
scale:(aFixedPoint scale) |
|
7355 | 1180 |
|
1181 |
" |
|
18239 | 1182 |
((1/3) asFixedPoint:2) + (1/3) |
1183 |
((1/3) asFixedPoint:2) + (2/3) |
|
7355 | 1184 |
" |
1185 |
! |
|
1186 |
||
1 | 1187 |
sumFromFloat:aFloat |
213 | 1188 |
"sent when a float does not know how to add the receiver, a fraction" |
1 | 1189 |
|
1190 |
^ (aFloat * denominator + numerator) / denominator |
|
1191 |
! |
|
1192 |
||
7355 | 1193 |
sumFromFraction:aFraction |
1194 |
|n d otherDenominator otherNumerator| |
|
1195 |
||
1196 |
otherDenominator := aFraction denominator. |
|
1197 |
otherNumerator := aFraction numerator. |
|
1198 |
||
1199 |
"save a multiplication if possible" |
|
1200 |
otherDenominator == denominator ifTrue:[ |
|
18239 | 1201 |
n := otherNumerator + numerator. |
1202 |
d := otherDenominator. |
|
7355 | 1203 |
] ifFalse:[ |
18239 | 1204 |
n := (otherNumerator * denominator) + (numerator * otherDenominator). |
1205 |
d := otherDenominator * denominator. |
|
7355 | 1206 |
]. |
18239 | 1207 |
^ aFraction class |
1208 |
numerator:n |
|
1209 |
denominator:d |
|
7355 | 1210 |
|
1211 |
" |
|
18239 | 1212 |
(1/3) + (1/3) |
1213 |
(1/3) + (2/3) |
|
7355 | 1214 |
" |
1215 |
! |
|
1216 |
||
701 | 1217 |
sumFromInteger:anInteger |
1218 |
"sent when an integer does not know how to add the receiver, a fraction" |
|
1 | 1219 |
|
18239 | 1220 |
^ self class |
1221 |
numerator:(numerator + (anInteger * denominator)) |
|
1222 |
denominator:denominator |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1223 |
|
2792 | 1224 |
"Modified: 28.7.1997 / 19:08:40 / cg" |
1 | 1225 |
! ! |
1226 |
||
22895 | 1227 |
|
92 | 1228 |
!Fraction methodsFor:'printing & storing'! |
1 | 1229 |
|
54 | 1230 |
printOn:aStream |
1231 |
"append a printed representation of the receiver to the |
|
1232 |
argument, aStream" |
|
1233 |
||
6675 | 1234 |
|t| |
1235 |
||
1236 |
PrintWholeNumbers == true ifTrue:[ |
|
18239 | 1237 |
"/ experimental: print fractions which are greater than 1 as a sum of |
1238 |
"/ an integral and the fractional part. They are easier to read this way. |
|
1239 |
numerator > denominator ifTrue:[ |
|
1240 |
aStream nextPut:$(. |
|
1241 |
t := numerator // denominator. |
|
1242 |
t printOn:aStream. |
|
1243 |
aStream nextPutAll:'+('. |
|
1244 |
(numerator - (t*denominator)) printOn:aStream. |
|
1245 |
aStream nextPut:$/. |
|
1246 |
denominator printOn:aStream. |
|
1247 |
aStream nextPutAll:'))'. |
|
1248 |
^ self |
|
1249 |
]. |
|
6675 | 1250 |
]. |
1251 |
||
54 | 1252 |
aStream nextPut:$(. |
1253 |
numerator printOn:aStream. |
|
1254 |
aStream nextPut:$/. |
|
1255 |
denominator printOn:aStream. |
|
1256 |
aStream nextPut:$) |
|
6675 | 1257 |
|
1258 |
"Modified: / 31.7.2002 / 09:56:41 / cg" |
|
1 | 1259 |
! ! |
701 | 1260 |
|
1261 |
!Fraction methodsFor:'private'! |
|
1262 |
||
1263 |
reduced |
|
7139 | 1264 |
"reduce the receiver; divide the numerator and denominator by their |
1265 |
greatest common divisor; if the result is integral, return an Integer. |
|
1266 |
Otherwise, return the normalized receiver. |
|
11250 | 1267 |
CAVEAT: bad name; should be called reduce, as it has a side effect |
1268 |
(i.e. this is destructive wrt. the instance values)." |
|
701 | 1269 |
|
2790 | 1270 |
|gcd den| |
701 | 1271 |
|
2790 | 1272 |
den := denominator. |
1273 |
den < 0 ifTrue:[ |
|
18239 | 1274 |
numerator := numerator negated. |
1275 |
den := denominator := den negated. |
|
2789
e3e8707d26b4
make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents:
2140
diff
changeset
|
1276 |
]. |
e3e8707d26b4
make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents:
2140
diff
changeset
|
1277 |
|
2790 | 1278 |
den == 1 ifTrue:[^ numerator]. |
701 | 1279 |
numerator == 1 ifTrue:[^ self]. |
1280 |
numerator == 0 ifTrue:[^ 0]. |
|
1281 |
||
2790 | 1282 |
gcd := numerator gcd:den. |
1283 |
(gcd ~~ 1) ifTrue:[ |
|
18239 | 1284 |
gcd < 0 ifTrue:[ |
1285 |
gcd := gcd negated. |
|
1286 |
]. |
|
1287 |
numerator := numerator // gcd. |
|
1288 |
denominator := den := den // gcd. |
|
1289 |
(den == 1) ifTrue:[^ numerator]. |
|
701 | 1290 |
]. |
1291 |
^ self |
|
1292 |
! |
|
1293 |
||
1294 |
setNumerator:num denominator:den |
|
1295 |
"set both numerator and denominator" |
|
1296 |
||
1297 |
numerator := num. |
|
1298 |
denominator := den |
|
1299 |
! ! |
|
1300 |
||
6650 | 1301 |
!Fraction methodsFor:'testing'! |
701 | 1302 |
|
1303 |
isFraction |
|
1304 |
"return true, if the receiver is some kind of fraction; |
|
1305 |
true is returned here - the method is redefined from Object." |
|
1306 |
||
1307 |
^ true |
|
6650 | 1308 |
! |
701 | 1309 |
|
4658
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1310 |
isLiteral |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1311 |
"return true, if the receiver can be used as a literal constant in ST syntax |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1312 |
(i.e. can be used in constant arrays)" |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1313 |
|
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1314 |
^ true |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1315 |
|
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1316 |
! |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1317 |
|
701 | 1318 |
negative |
18861 | 1319 |
"return true if the receiver is less than zero" |
701 | 1320 |
|
1321 |
(numerator < 0) ifTrue:[ |
|
20308 | 1322 |
^ (denominator < 0) not |
701 | 1323 |
]. |
1324 |
^ (denominator < 0) |
|
1325 |
! ! |
|
1326 |
||
5552 | 1327 |
!Fraction methodsFor:'truncation & rounding'! |
701 | 1328 |
|
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1329 |
fractionPart |
7355 | 1330 |
"extract the after-decimal fraction part, |
1331 |
such that (self truncated + self fractionPart) = self" |
|
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1332 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1333 |
numerator abs < denominator abs ifTrue:[ |
18239 | 1334 |
^ self |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1335 |
]. |
7355 | 1336 |
^ (numerator rem: denominator) / denominator |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1337 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1338 |
" |
18239 | 1339 |
(3/2) fractionPart + (3/2) truncated |
1340 |
(-3/2) fractionPart + (-3/2) truncated |
|
7355 | 1341 |
|
18239 | 1342 |
(3/2) fractionPart |
1343 |
(-3/2) fractionPart |
|
1344 |
(3/2) asFloat fractionPart |
|
1345 |
(-3/2) asFloat fractionPart |
|
1346 |
(2/3) fractionPart |
|
1347 |
((3/2)*(15/4)) fractionPart |
|
1348 |
((2/3)*(4/15)) fractionPart |
|
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1349 |
" |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1350 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1351 |
"Modified: / 5.11.2001 / 17:55:25 / cg" |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1352 |
! |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1353 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1354 |
integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1355 |
"extract the pre-decimal integer part." |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1356 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1357 |
numerator abs < denominator abs ifTrue:[ |
18239 | 1358 |
^ 0 |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1359 |
]. |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1360 |
^ super integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1361 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1362 |
" |
18239 | 1363 |
(3/2) integerPart |
1364 |
(-3/2) integerPart |
|
1365 |
(2/3) integerPart |
|
1366 |
((3/2)*(15/4)) integerPart |
|
1367 |
((2/3)*(4/15)) integerPart |
|
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1368 |
" |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1369 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1370 |
"Modified: / 5.11.2001 / 17:55:01 / cg" |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1371 |
! |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1372 |
|
701 | 1373 |
rounded |
1374 |
"return the receiver rounded to the nearest integer as integer" |
|
1375 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1376 |
"/ mhmh - what about -(1/2) |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1377 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1378 |
|t| |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1379 |
|
701 | 1380 |
self negative ifTrue:[ |
18239 | 1381 |
t := self - (1/2) |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1382 |
] ifFalse:[ |
18239 | 1383 |
t := self + (1/2) |
701 | 1384 |
]. |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1385 |
^ t truncated. |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1386 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1387 |
" |
18239 | 1388 |
(1/3) rounded |
1389 |
(1/3) negated rounded |
|
1390 |
(1/2) rounded |
|
1391 |
(1/2) negated rounded |
|
1392 |
0.5 rounded |
|
1393 |
-0.5 rounded |
|
1394 |
(2/3) rounded |
|
1395 |
(2/3) negated rounded |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1396 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1397 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1398 |
"Modified: 5.11.1996 / 11:32:32 / cg" |
701 | 1399 |
! |
1400 |
||
1401 |
truncated |
|
1402 |
"return the receiver truncated towards zero as Integer" |
|
1403 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1404 |
^ numerator quo: denominator |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1405 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1406 |
" |
18239 | 1407 |
(3/2) truncated |
1408 |
(3/2) negated truncated |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1409 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1410 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1411 |
"Modified: 5.11.1996 / 12:18:46 / cg" |
701 | 1412 |
! ! |
1413 |
||
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1414 |
!Fraction methodsFor:'visiting'! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1415 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1416 |
acceptVisitor:aVisitor with:aParameter |
16729 | 1417 |
"dispatch for visitor pattern; send #visitFraction:with: to aVisitor" |
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1418 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1419 |
^ aVisitor visitFraction:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1420 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1421 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1422 |
!Fraction class methodsFor:'documentation'! |
701 | 1423 |
|
1424 |
version |
|
18839 | 1425 |
^ '$Header$' |
13357 | 1426 |
! |
1427 |
||
1428 |
version_CVS |
|
18839 | 1429 |
^ '$Header$' |
701 | 1430 |
! ! |
6891
212aa8576cf7
oops - did not compare correctly against unnormalized numbers
Claus Gittinger <cg@exept.de>
parents:
6675
diff
changeset
|
1431 |
|
15278 | 1432 |
|
701 | 1433 |
Fraction initialize! |