author | Claus Gittinger <cg@exept.de> |
Thu, 06 Jun 2019 18:03:56 +0200 | |
changeset 24265 | 2bc155f7d89b |
parent 24201 | 511a59fbb595 |
child 24281 | 69518cfc5366 |
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 |
||
1200 | 800 |
asShortFloat |
6576 | 801 |
"return a short float with (approximately) my value" |
802 |
||
803 |
(numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[ |
|
18239 | 804 |
^ (numerator asShortFloat) / (denominator asShortFloat) |
6576 | 805 |
]. |
1200 | 806 |
|
6576 | 807 |
^ self asFloat asShortFloat |
1200 | 808 |
|
6576 | 809 |
" |
810 |
(5/9) asShortFloat |
|
811 |
(500000000000/900000000000) asShortFloat |
|
812 |
(500000000000/9) asShortFloat |
|
813 |
" |
|
1200 | 814 |
! |
815 |
||
701 | 816 |
coerce:aNumber |
18839 | 817 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
701 | 818 |
|
819 |
^ aNumber asFraction |
|
820 |
! |
|
821 |
||
822 |
generality |
|
823 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
824 |
||
825 |
^ 60 |
|
826 |
! ! |
|
827 |
||
828 |
!Fraction methodsFor:'comparing'! |
|
829 |
||
830 |
< aNumber |
|
831 |
"return true if the receiver is less |
|
832 |
than aNumber, false otherwise." |
|
1 | 833 |
|
701 | 834 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
18239 | 835 |
^ numerator < (denominator * aNumber) |
701 | 836 |
]. |
837 |
^ aNumber lessFromFraction:self |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
838 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
839 |
"Modified: 5.11.1996 / 10:30:52 / cg" |
701 | 840 |
! |
841 |
||
842 |
= aNumber |
|
843 |
"return true, if the argument represents the same numeric value |
|
844 |
as the receiver, false otherwise" |
|
845 |
||
846 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
|
18239 | 847 |
(denominator == 1) ifFalse:[ |
848 |
^ numerator = (aNumber * denominator) |
|
849 |
]. |
|
850 |
^ numerator = aNumber |
|
1 | 851 |
]. |
7355 | 852 |
^ aNumber equalFromFraction:self |
3631
26a44b284912
handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
853 |
|
26a44b284912
handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
854 |
"Modified: / 7.7.1998 / 17:17:07 / cg" |
701 | 855 |
! |
856 |
||
857 |
> aNumber |
|
858 |
"return true if the receiver is greater |
|
859 |
than aNumber, false otherwise." |
|
860 |
"optional - could use inherited method ..." |
|
861 |
||
862 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
|
18239 | 863 |
^ numerator > (denominator * aNumber) |
701 | 864 |
]. |
7355 | 865 |
^ aNumber < self |
4593 | 866 |
! |
867 |
||
868 |
hash |
|
869 |
"return a number for hashing; redefined, since fractions compare |
|
18861 | 870 |
by numeric value (i.e. (1/2) = 0.5), hash values must be the same" |
4593 | 871 |
|
18861 | 872 |
(denominator == 1) ifTrue:[^ numerator hash]. |
873 |
(denominator == -1) ifTrue:[^ numerator hash negated]. |
|
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
874 |
^ self asFloat hash |
4593 | 875 |
|
876 |
" |
|
18239 | 877 |
3 hash |
878 |
(9/3) hash |
|
879 |
3.0 hash |
|
880 |
(1/2) hash |
|
881 |
(1/4) hash |
|
882 |
0.0 hash |
|
883 |
0.5 hash |
|
884 |
0.25 hash |
|
885 |
0.4 hash |
|
20308 | 886 |
|
887 |
0.25 hash |
|
888 |
-0.25 hash |
|
889 |
(1/4) hash |
|
890 |
(-1/4) hash |
|
4593 | 891 |
" |
5364 | 892 |
! |
893 |
||
894 |
sameFractionValueAs:aNumber |
|
895 |
"return true, if the argument represents the same numeric value |
|
896 |
as the receiver, false otherwise" |
|
897 |
||
898 |
|rSelf rNum| |
|
899 |
||
900 |
rSelf := self reduced. |
|
901 |
rNum := aNumber reduced. |
|
902 |
rSelf denominator = rNum denominator ifTrue:[ |
|
18239 | 903 |
^ rSelf numerator = rNum numerator |
5364 | 904 |
]. |
905 |
^ false |
|
1 | 906 |
! ! |
907 |
||
908 |
!Fraction methodsFor:'double dispatching'! |
|
909 |
||
7355 | 910 |
differenceFromFixedPoint:aFixedPoint |
911 |
|n d otherDenominator otherNumerator| |
|
912 |
||
913 |
otherDenominator := aFixedPoint denominator. |
|
914 |
otherNumerator := aFixedPoint numerator. |
|
915 |
||
916 |
"save a multiplication if possible" |
|
917 |
otherDenominator == denominator ifTrue:[ |
|
18239 | 918 |
n := otherNumerator - numerator. |
919 |
d := otherDenominator. |
|
7355 | 920 |
] ifFalse:[ |
18239 | 921 |
n := (otherNumerator * denominator) - (numerator * otherDenominator). |
922 |
d := otherDenominator * denominator. |
|
7355 | 923 |
]. |
18239 | 924 |
^ aFixedPoint class |
925 |
numerator:n |
|
926 |
denominator:d |
|
927 |
scale:(aFixedPoint scale) |
|
7355 | 928 |
|
929 |
" |
|
18239 | 930 |
((1/3) asFixedPoint:2) - (1/3) |
931 |
((1/3) asFixedPoint:2) - (2/3) |
|
7355 | 932 |
" |
933 |
! |
|
934 |
||
701 | 935 |
differenceFromFloat:aFloat |
936 |
"sent when a float does not know how to subtract the receiver, a fraction" |
|
1 | 937 |
|
701 | 938 |
^ (aFloat * denominator - numerator) / denominator |
1 | 939 |
! |
940 |
||
7355 | 941 |
differenceFromFraction:aFraction |
942 |
|n d otherDenominator otherNumerator| |
|
943 |
||
944 |
otherDenominator := aFraction denominator. |
|
945 |
otherNumerator := aFraction numerator. |
|
946 |
||
947 |
"save a multiplication if possible" |
|
948 |
otherDenominator == denominator ifTrue:[ |
|
18239 | 949 |
n := otherNumerator - numerator. |
950 |
d := otherDenominator. |
|
7355 | 951 |
] ifFalse:[ |
18239 | 952 |
n := (otherNumerator * denominator) - (numerator * otherDenominator). |
953 |
d := otherDenominator * denominator. |
|
7355 | 954 |
]. |
18239 | 955 |
^ aFraction class |
956 |
numerator:n |
|
957 |
denominator:d |
|
7355 | 958 |
|
959 |
" |
|
18239 | 960 |
((1/3) asFixedPoint:2) - (1/3) |
961 |
((1/3) asFixedPoint:2) - (2/3) |
|
7355 | 962 |
" |
963 |
! |
|
964 |
||
1 | 965 |
differenceFromInteger:anInteger |
213 | 966 |
"sent when an integer does not know how to subtract the receiver, a fraction" |
1 | 967 |
|
18239 | 968 |
^ self class |
969 |
numerator:((anInteger * denominator) - numerator) |
|
970 |
denominator:denominator |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
971 |
|
2792 | 972 |
"Modified: 28.7.1997 / 19:08:53 / cg" |
1 | 973 |
! |
974 |
||
7355 | 975 |
equalFromFraction:aFraction |
7571
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
976 |
denominator = aFraction denominator ifFalse:[ |
18239 | 977 |
^ false " must always be reduced " |
978 |
"/ ^ (numerator * aFraction denominator) = (aFraction numerator * denominator) |
|
7571
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
979 |
]. |
7355 | 980 |
^ numerator = aFraction numerator |
981 |
! |
|
982 |
||
983 |
equalFromInteger:anInteger |
|
984 |
"sent when an integer does not know how to compare to the receiver, a fraction" |
|
985 |
||
986 |
"as I am always reduced, this test should not be required. |
|
987 |
However, it is here for subclasses and to allow comparing unnormalized fractions, |
|
988 |
which might be encountered internally" |
|
989 |
||
7571
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
990 |
denominator = 1 ifFalse:[ |
18239 | 991 |
^ numerator = (anInteger * denominator) |
7571
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
992 |
]. |
7355 | 993 |
^ numerator = anInteger |
7571
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
994 |
|
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
995 |
" |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
996 |
1 = (1 asFixedPoint:1) |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
997 |
(1 asFixedPoint:1) = 1 |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
998 |
" |
7355 | 999 |
! |
1000 |
||
10486 | 1001 |
lessEqFromInteger:anInteger |
1002 |
"sent when an integer does not know how to compare to the receiver, a fraction" |
|
1003 |
||
1004 |
^ (denominator * anInteger) <= numerator |
|
1005 |
! |
|
1006 |
||
7355 | 1007 |
lessFromFraction:aFraction |
18861 | 1008 |
"sent when a fraction does not know how to compare to the receiver. |
1009 |
Return true if aFraction < self." |
|
7355 | 1010 |
|
1011 |
|n d| |
|
1012 |
||
1013 |
d := aFraction denominator. |
|
1014 |
n := aFraction numerator. |
|
1015 |
||
1016 |
"/ save a multiplication if possible |
|
1017 |
d == denominator ifTrue:[ |
|
20308 | 1018 |
^ n < numerator |
7355 | 1019 |
]. |
18239 | 1020 |
^ (denominator * n) < (numerator * d) |
7355 | 1021 |
! |
1022 |
||
701 | 1023 |
lessFromInteger:anInteger |
18861 | 1024 |
"sent when an integer does not know how to compare to the receiver, a fraction. |
1025 |
Return true if anInteger < self." |
|
701 | 1026 |
|
1027 |
^ (denominator * anInteger) < numerator |
|
1028 |
! |
|
1029 |
||
7355 | 1030 |
productFromFixedPoint:aFixedPoint |
18239 | 1031 |
^ aFixedPoint class |
1032 |
numerator:(aFixedPoint numerator * numerator) |
|
1033 |
denominator:(aFixedPoint denominator * denominator) |
|
1034 |
scale:(aFixedPoint scale) |
|
7355 | 1035 |
|
1036 |
" |
|
18239 | 1037 |
((1/3) asFixedPoint:2) * 2 |
1038 |
((1/3) asFixedPoint:2) * (1/2) |
|
1039 |
((1/3) asFixedPoint:2) * (3/2) |
|
7355 | 1040 |
" |
1041 |
! |
|
1042 |
||
701 | 1043 |
productFromFloat:aFloat |
1044 |
"sent when a float does not know how to multiply the receiver, a fraction" |
|
1045 |
||
1046 |
^ aFloat * numerator / denominator |
|
1047 |
! |
|
1048 |
||
7355 | 1049 |
productFromFraction:aFraction |
18239 | 1050 |
^ aFraction class |
1051 |
numerator:(aFraction numerator * numerator) |
|
1052 |
denominator:(aFraction denominator * denominator) |
|
7355 | 1053 |
|
1054 |
" |
|
18239 | 1055 |
((1/3) asFixedPoint:2) * 2 |
1056 |
((1/3) asFixedPoint:2) * (1/2) |
|
1057 |
((1/3) asFixedPoint:2) * (3/2) |
|
7355 | 1058 |
" |
1059 |
! |
|
1060 |
||
1 | 1061 |
productFromInteger:anInteger |
213 | 1062 |
"sent when an integer does not know how to multiply the receiver, a fraction" |
1 | 1063 |
|
18239 | 1064 |
^ self class |
1065 |
numerator:(anInteger * numerator) |
|
1066 |
denominator:denominator |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1067 |
|
2792 | 1068 |
"Modified: 28.7.1997 / 19:06:22 / cg" |
1 | 1069 |
! |
1070 |
||
7355 | 1071 |
quotientFromFixedPoint:aFixedPoint |
7442 | 1072 |
"Return the quotient of the argument, aFixedPoint and the receiver. |
1073 |
Sent when aFixedPoint does not know how to divide by the receiver." |
|
1074 |
||
18239 | 1075 |
^ aFixedPoint class |
1076 |
numerator:(aFixedPoint numerator * denominator) |
|
1077 |
denominator:(aFixedPoint denominator * numerator) |
|
1078 |
scale:(aFixedPoint scale) |
|
7355 | 1079 |
|
1080 |
" |
|
18239 | 1081 |
((1/3) asFixedPoint:2) / 2 |
1082 |
((1/3) asFixedPoint:2) / (1/2) |
|
7355 | 1083 |
" |
1084 |
! |
|
1085 |
||
701 | 1086 |
quotientFromFloat:aFloat |
7442 | 1087 |
"Return the quotient of the argument, aFloat and the receiver. |
1088 |
Sent when aFloat does not know how to divide by the receiver." |
|
701 | 1089 |
|
1090 |
^ (aFloat * denominator) / numerator |
|
1091 |
! |
|
1092 |
||
7355 | 1093 |
quotientFromFraction:aFraction |
7442 | 1094 |
"Return the quotient of the argument, aFraction and the receiver. |
1095 |
Sent when aFraction does not know how to divide by the receiver." |
|
1096 |
||
18239 | 1097 |
^ aFraction class |
1098 |
numerator:(aFraction numerator * denominator) |
|
1099 |
denominator:(aFraction denominator * numerator) |
|
7355 | 1100 |
|
1101 |
" |
|
18239 | 1102 |
(1/3) / (1/2) |
1103 |
(1/3) / (3/2) |
|
7355 | 1104 |
" |
1105 |
! |
|
1106 |
||
324 | 1107 |
quotientFromInteger:anInteger |
7442 | 1108 |
"Return the quotient of the argument, anInteger and the receiver. |
1109 |
Sent when anInteger does not know how to divide by the receiver." |
|
324 | 1110 |
|
18239 | 1111 |
^ self class |
1112 |
numerator:(anInteger * denominator) |
|
1113 |
denominator:numerator |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1114 |
|
2792 | 1115 |
"Modified: 28.7.1997 / 19:08:46 / cg" |
324 | 1116 |
! |
1117 |
||
22099 | 1118 |
raisedFromFloat:aFloat |
1119 |
"aFloat does not know how to be raised to the receiver" |
|
1120 |
||
1121 |
numerator == 1 ifTrue:[ |
|
1122 |
^ aFloat nthRoot:denominator |
|
1123 |
]. |
|
1124 |
^ self asFloat raisedFromFloat:aFloat |
|
1125 |
||
1126 |
" |
|
1127 |
100 raisedTo:(2/5) asFloat |
|
1128 |
100 raisedTo:(1/5) asFloat |
|
1129 |
||
1130 |
100 nthRoot:2 10.0 |
|
1131 |
100 nthRoot:3 4.641588833612778893 |
|
1132 |
100 raisedTo:(1/3) 3.16227766016838 |
|
1133 |
100 raisedTo:(1/4) 3.16227766016838 |
|
1134 |
100 raisedTo:(1/5) 2.51188643150958 |
|
1135 |
100 raisedTo:(1/6) 2.15443469003188 |
|
1136 |
100 raisedTo:(1/7) 1.93069772888325 |
|
1137 |
" |
|
1138 |
||
1139 |
"Created: / 25-07-2017 / 16:13:26 / cg" |
|
1140 |
! |
|
1141 |
||
7355 | 1142 |
sumFromFixedPoint:aFixedPoint |
1143 |
|n d otherDenominator otherNumerator| |
|
1144 |
||
1145 |
otherDenominator := aFixedPoint denominator. |
|
1146 |
otherNumerator := aFixedPoint numerator. |
|
1147 |
||
1148 |
"save a multiplication if possible" |
|
1149 |
otherDenominator == denominator ifTrue:[ |
|
18239 | 1150 |
n := otherNumerator + numerator. |
1151 |
d := otherDenominator. |
|
7355 | 1152 |
] ifFalse:[ |
18239 | 1153 |
n := (otherNumerator * denominator) + (numerator * otherDenominator). |
1154 |
d := otherDenominator * denominator. |
|
7355 | 1155 |
]. |
18239 | 1156 |
^ aFixedPoint class |
1157 |
numerator:n |
|
1158 |
denominator:d |
|
1159 |
scale:(aFixedPoint scale) |
|
7355 | 1160 |
|
1161 |
" |
|
18239 | 1162 |
((1/3) asFixedPoint:2) + (1/3) |
1163 |
((1/3) asFixedPoint:2) + (2/3) |
|
7355 | 1164 |
" |
1165 |
! |
|
1166 |
||
1 | 1167 |
sumFromFloat:aFloat |
213 | 1168 |
"sent when a float does not know how to add the receiver, a fraction" |
1 | 1169 |
|
1170 |
^ (aFloat * denominator + numerator) / denominator |
|
1171 |
! |
|
1172 |
||
7355 | 1173 |
sumFromFraction:aFraction |
1174 |
|n d otherDenominator otherNumerator| |
|
1175 |
||
1176 |
otherDenominator := aFraction denominator. |
|
1177 |
otherNumerator := aFraction numerator. |
|
1178 |
||
1179 |
"save a multiplication if possible" |
|
1180 |
otherDenominator == denominator ifTrue:[ |
|
18239 | 1181 |
n := otherNumerator + numerator. |
1182 |
d := otherDenominator. |
|
7355 | 1183 |
] ifFalse:[ |
18239 | 1184 |
n := (otherNumerator * denominator) + (numerator * otherDenominator). |
1185 |
d := otherDenominator * denominator. |
|
7355 | 1186 |
]. |
18239 | 1187 |
^ aFraction class |
1188 |
numerator:n |
|
1189 |
denominator:d |
|
7355 | 1190 |
|
1191 |
" |
|
18239 | 1192 |
(1/3) + (1/3) |
1193 |
(1/3) + (2/3) |
|
7355 | 1194 |
" |
1195 |
! |
|
1196 |
||
701 | 1197 |
sumFromInteger:anInteger |
1198 |
"sent when an integer does not know how to add the receiver, a fraction" |
|
1 | 1199 |
|
18239 | 1200 |
^ self class |
1201 |
numerator:(numerator + (anInteger * denominator)) |
|
1202 |
denominator:denominator |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1203 |
|
2792 | 1204 |
"Modified: 28.7.1997 / 19:08:40 / cg" |
1 | 1205 |
! ! |
1206 |
||
22895 | 1207 |
|
92 | 1208 |
!Fraction methodsFor:'printing & storing'! |
1 | 1209 |
|
54 | 1210 |
printOn:aStream |
1211 |
"append a printed representation of the receiver to the |
|
1212 |
argument, aStream" |
|
1213 |
||
6675 | 1214 |
|t| |
1215 |
||
1216 |
PrintWholeNumbers == true ifTrue:[ |
|
18239 | 1217 |
"/ experimental: print fractions which are greater than 1 as a sum of |
1218 |
"/ an integral and the fractional part. They are easier to read this way. |
|
1219 |
numerator > denominator ifTrue:[ |
|
1220 |
aStream nextPut:$(. |
|
1221 |
t := numerator // denominator. |
|
1222 |
t printOn:aStream. |
|
1223 |
aStream nextPutAll:'+('. |
|
1224 |
(numerator - (t*denominator)) printOn:aStream. |
|
1225 |
aStream nextPut:$/. |
|
1226 |
denominator printOn:aStream. |
|
1227 |
aStream nextPutAll:'))'. |
|
1228 |
^ self |
|
1229 |
]. |
|
6675 | 1230 |
]. |
1231 |
||
54 | 1232 |
aStream nextPut:$(. |
1233 |
numerator printOn:aStream. |
|
1234 |
aStream nextPut:$/. |
|
1235 |
denominator printOn:aStream. |
|
1236 |
aStream nextPut:$) |
|
6675 | 1237 |
|
1238 |
"Modified: / 31.7.2002 / 09:56:41 / cg" |
|
1 | 1239 |
! ! |
701 | 1240 |
|
1241 |
!Fraction methodsFor:'private'! |
|
1242 |
||
1243 |
reduced |
|
7139 | 1244 |
"reduce the receiver; divide the numerator and denominator by their |
1245 |
greatest common divisor; if the result is integral, return an Integer. |
|
1246 |
Otherwise, return the normalized receiver. |
|
11250 | 1247 |
CAVEAT: bad name; should be called reduce, as it has a side effect |
1248 |
(i.e. this is destructive wrt. the instance values)." |
|
701 | 1249 |
|
2790 | 1250 |
|gcd den| |
701 | 1251 |
|
2790 | 1252 |
den := denominator. |
1253 |
den < 0 ifTrue:[ |
|
18239 | 1254 |
numerator := numerator negated. |
1255 |
den := denominator := den negated. |
|
2789
e3e8707d26b4
make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents:
2140
diff
changeset
|
1256 |
]. |
e3e8707d26b4
make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents:
2140
diff
changeset
|
1257 |
|
2790 | 1258 |
den == 1 ifTrue:[^ numerator]. |
701 | 1259 |
numerator == 1 ifTrue:[^ self]. |
1260 |
numerator == 0 ifTrue:[^ 0]. |
|
1261 |
||
2790 | 1262 |
gcd := numerator gcd:den. |
1263 |
(gcd ~~ 1) ifTrue:[ |
|
18239 | 1264 |
gcd < 0 ifTrue:[ |
1265 |
gcd := gcd negated. |
|
1266 |
]. |
|
1267 |
numerator := numerator // gcd. |
|
1268 |
denominator := den := den // gcd. |
|
1269 |
(den == 1) ifTrue:[^ numerator]. |
|
701 | 1270 |
]. |
1271 |
^ self |
|
1272 |
! |
|
1273 |
||
1274 |
setNumerator:num denominator:den |
|
1275 |
"set both numerator and denominator" |
|
1276 |
||
1277 |
numerator := num. |
|
1278 |
denominator := den |
|
1279 |
! ! |
|
1280 |
||
6650 | 1281 |
!Fraction methodsFor:'testing'! |
701 | 1282 |
|
1283 |
isFraction |
|
1284 |
"return true, if the receiver is some kind of fraction; |
|
1285 |
true is returned here - the method is redefined from Object." |
|
1286 |
||
1287 |
^ true |
|
6650 | 1288 |
! |
701 | 1289 |
|
4658
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1290 |
isLiteral |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1291 |
"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
|
1292 |
(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
|
1293 |
|
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1294 |
^ true |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1295 |
|
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1296 |
! |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1297 |
|
701 | 1298 |
negative |
18861 | 1299 |
"return true if the receiver is less than zero" |
701 | 1300 |
|
1301 |
(numerator < 0) ifTrue:[ |
|
20308 | 1302 |
^ (denominator < 0) not |
701 | 1303 |
]. |
1304 |
^ (denominator < 0) |
|
1305 |
! ! |
|
1306 |
||
5552 | 1307 |
!Fraction methodsFor:'truncation & rounding'! |
701 | 1308 |
|
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1309 |
fractionPart |
7355 | 1310 |
"extract the after-decimal fraction part, |
1311 |
such that (self truncated + self fractionPart) = self" |
|
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1312 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1313 |
numerator abs < denominator abs ifTrue:[ |
18239 | 1314 |
^ self |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1315 |
]. |
7355 | 1316 |
^ (numerator rem: denominator) / denominator |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1317 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1318 |
" |
18239 | 1319 |
(3/2) fractionPart + (3/2) truncated |
1320 |
(-3/2) fractionPart + (-3/2) truncated |
|
7355 | 1321 |
|
18239 | 1322 |
(3/2) fractionPart |
1323 |
(-3/2) fractionPart |
|
1324 |
(3/2) asFloat fractionPart |
|
1325 |
(-3/2) asFloat fractionPart |
|
1326 |
(2/3) fractionPart |
|
1327 |
((3/2)*(15/4)) fractionPart |
|
1328 |
((2/3)*(4/15)) fractionPart |
|
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1329 |
" |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1330 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1331 |
"Modified: / 5.11.2001 / 17:55:25 / cg" |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1332 |
! |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1333 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1334 |
integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1335 |
"extract the pre-decimal integer part." |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1336 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1337 |
numerator abs < denominator abs ifTrue:[ |
18239 | 1338 |
^ 0 |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1339 |
]. |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1340 |
^ super integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1341 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1342 |
" |
18239 | 1343 |
(3/2) integerPart |
1344 |
(-3/2) integerPart |
|
1345 |
(2/3) integerPart |
|
1346 |
((3/2)*(15/4)) integerPart |
|
1347 |
((2/3)*(4/15)) integerPart |
|
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1348 |
" |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1349 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1350 |
"Modified: / 5.11.2001 / 17:55:01 / cg" |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1351 |
! |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1352 |
|
701 | 1353 |
rounded |
1354 |
"return the receiver rounded to the nearest integer as integer" |
|
1355 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1356 |
"/ mhmh - what about -(1/2) |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1357 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1358 |
|t| |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1359 |
|
701 | 1360 |
self negative ifTrue:[ |
18239 | 1361 |
t := self - (1/2) |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1362 |
] ifFalse:[ |
18239 | 1363 |
t := self + (1/2) |
701 | 1364 |
]. |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1365 |
^ t truncated. |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1366 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1367 |
" |
18239 | 1368 |
(1/3) rounded |
1369 |
(1/3) negated rounded |
|
1370 |
(1/2) rounded |
|
1371 |
(1/2) negated rounded |
|
1372 |
0.5 rounded |
|
1373 |
-0.5 rounded |
|
1374 |
(2/3) rounded |
|
1375 |
(2/3) negated rounded |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1376 |
" |
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 |
"Modified: 5.11.1996 / 11:32:32 / cg" |
701 | 1379 |
! |
1380 |
||
1381 |
truncated |
|
1382 |
"return the receiver truncated towards zero as Integer" |
|
1383 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1384 |
^ numerator quo: denominator |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1385 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1386 |
" |
18239 | 1387 |
(3/2) truncated |
1388 |
(3/2) negated truncated |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1389 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1390 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1391 |
"Modified: 5.11.1996 / 12:18:46 / cg" |
701 | 1392 |
! ! |
1393 |
||
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1394 |
!Fraction methodsFor:'visiting'! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1395 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1396 |
acceptVisitor:aVisitor with:aParameter |
16729 | 1397 |
"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
|
1398 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1399 |
^ aVisitor visitFraction:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1400 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1401 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1402 |
!Fraction class methodsFor:'documentation'! |
701 | 1403 |
|
1404 |
version |
|
18839 | 1405 |
^ '$Header$' |
13357 | 1406 |
! |
1407 |
||
1408 |
version_CVS |
|
18839 | 1409 |
^ '$Header$' |
701 | 1410 |
! ! |
6891
212aa8576cf7
oops - did not compare correctly against unnormalized numbers
Claus Gittinger <cg@exept.de>
parents:
6675
diff
changeset
|
1411 |
|
15278 | 1412 |
|
701 | 1413 |
Fraction initialize! |