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