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