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