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