author | Claus Gittinger <cg@exept.de> |
Tue, 02 Sep 1997 22:56:14 +0200 | |
changeset 2894 | 344aec8ba014 |
parent 2853 | 0647da37ba9a |
child 3631 | 26a44b284912 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
213 | 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 |
" |
|
12 |
||
13 |
Number subclass:#Fraction |
|
1200 | 14 |
instanceVariableNames:'numerator denominator' |
15 |
classVariableNames:'FractionOne FractionZero' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Magnitude-Numbers' |
|
1 | 18 |
! |
19 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
20 |
!Fraction class methodsFor:'documentation'! |
88 | 21 |
|
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
213 | 25 |
All Rights Reserved |
88 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
36 |
documentation |
|
37 |
" |
|
38 |
Instances of Fraction represent fractional numbers consisting of |
|
39 |
a numerator and denominator. Both are themselfes arbitrary precision |
|
324 | 40 |
integers. Fractions are usually created by dividing Integers using / |
1295 | 41 |
(for exact division). |
42 |
||
1892 | 43 |
Mixed mode arithmetic: |
44 |
fraction op fraction -> fraction |
|
45 |
fraction op fix -> fix; scale is fix's scale |
|
46 |
fraction op integer -> fraction |
|
47 |
fraction op float -> float |
|
48 |
||
1295 | 49 |
[author:] |
50 |
Claus Gittinger |
|
1556 | 51 |
|
52 |
[see also:] |
|
53 |
Number |
|
1893 | 54 |
Float Integer FixedPoint |
1295 | 55 |
" |
88 | 56 |
! ! |
1 | 57 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
58 |
!Fraction class methodsFor:'initialization'! |
1 | 59 |
|
60 |
initialize |
|
302 | 61 |
FractionZero isNil ifTrue:[ |
62 |
FractionZero := self numerator:0 denominator:1. |
|
63 |
FractionOne := self numerator:1 denominator:1 |
|
64 |
] |
|
1 | 65 |
! ! |
66 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
67 |
!Fraction class methodsFor:'instance creation'! |
1 | 68 |
|
69 |
new |
|
70 |
"create and return a new fraction with value 0" |
|
71 |
||
54 | 72 |
^ self numerator:0 denominator:1 |
1 | 73 |
! |
74 |
||
75 |
numerator:num denominator:den |
|
76 |
"create and return a new fraction with numerator num and denominator den" |
|
77 |
||
2792 | 78 |
|newFraction| |
79 |
||
1 | 80 |
%{ /* NOCONTEXT */ |
369 | 81 |
|
13 | 82 |
/* this check allows subclassing .. */ |
1 | 83 |
if (self == Fraction) { |
2792 | 84 |
if (__bothSmallInteger(num, den)) { |
2894 | 85 |
if (__CanDoQuickAlignedNew(sizeof(struct __fraction))) { /* OBJECT ALLOCATION */ |
2792 | 86 |
OBJ newFraction; |
87 |
int spc; |
|
1 | 88 |
|
2792 | 89 |
__qCheckedAlignedNew(newFraction, sizeof(struct __fraction)); |
90 |
__InstPtr(newFraction)->o_class = self; |
|
91 |
if (__intVal(den) < 0) { |
|
92 |
__FractionInstPtr(newFraction)->f_numerator = __MKSMALLINT(- __intVal(num)); |
|
93 |
__FractionInstPtr(newFraction)->f_denominator = __MKSMALLINT(- __intVal(den)); |
|
94 |
} else { |
|
95 |
__FractionInstPtr(newFraction)->f_numerator = num; |
|
96 |
__FractionInstPtr(newFraction)->f_denominator = den; |
|
97 |
} |
|
98 |
if (num == __MKSMALLINT(1)) { |
|
99 |
RETURN ( newFraction ); |
|
100 |
} |
|
101 |
} |
|
102 |
} |
|
1 | 103 |
} |
2790 | 104 |
%}. |
2792 | 105 |
newFraction isNil ifTrue:[ |
106 |
newFraction := self basicNew setNumerator:num denominator:den. |
|
107 |
]. |
|
108 |
^ newFraction reduced |
|
1 | 109 |
! ! |
110 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
111 |
!Fraction class methodsFor:'constants'! |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
112 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
113 |
pi |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
114 |
"return the constant pi as Fraction" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
115 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
116 |
^ self |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
117 |
numerator: 31415926535897932384626434 |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
118 |
denominator:10000000000000000000000000 |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
119 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
120 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
121 |
Fraction pi |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
122 |
Fraction pi asFloat |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
123 |
Float pi |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
124 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
125 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
126 |
"Modified: 5.11.1996 / 11:11:44 / cg" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
127 |
! |
701 | 128 |
|
129 |
unity |
|
1555 | 130 |
"return the neutral element for multiplication (1 / 1)" |
701 | 131 |
|
132 |
^ FractionOne |
|
1555 | 133 |
|
134 |
"Modified: 18.7.1996 / 12:26:06 / cg" |
|
701 | 135 |
! |
136 |
||
137 |
zero |
|
1555 | 138 |
"return the neutral element for addition (0 / 1)" |
701 | 139 |
|
140 |
^ FractionZero |
|
1555 | 141 |
|
142 |
"Modified: 18.7.1996 / 12:26:12 / cg" |
|
701 | 143 |
! ! |
144 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
145 |
!Fraction class methodsFor:'queries'! |
3 | 146 |
|
147 |
isBuiltInClass |
|
1264 | 148 |
"return true if this class is known by the run-time-system. |
149 |
Here, true is returned for myself, false for subclasses." |
|
3 | 150 |
|
151 |
^ self == Fraction |
|
1264 | 152 |
|
153 |
"Modified: 23.4.1996 / 15:59:10 / cg" |
|
3 | 154 |
! ! |
155 |
||
1 | 156 |
!Fraction methodsFor:'accessing'! |
157 |
||
158 |
denominator |
|
159 |
"return the denominator" |
|
160 |
||
161 |
^ denominator |
|
162 |
! |
|
163 |
||
701 | 164 |
numerator |
165 |
"return the numerator" |
|
1 | 166 |
|
701 | 167 |
^ numerator |
1 | 168 |
! ! |
169 |
||
170 |
!Fraction methodsFor:'arithmetic'! |
|
171 |
||
701 | 172 |
* aNumber |
173 |
"return the product of the receiver and the argument, aNumber" |
|
174 |
||
175 |
|n d| |
|
176 |
||
177 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
|
1892 | 178 |
^ (self class numerator:(numerator * aNumber) |
2792 | 179 |
denominator:denominator) |
701 | 180 |
]. |
181 |
aNumber isFraction ifTrue:[ |
|
1892 | 182 |
aNumber isFixedPoint ifFalse:[ "/ the value was corrent, but the scale is lost |
183 |
n := numerator * aNumber numerator. |
|
184 |
d := denominator * aNumber denominator. |
|
2792 | 185 |
^ (self class numerator:n denominator:d) |
1892 | 186 |
] |
701 | 187 |
]. |
188 |
(aNumber isMemberOf:Float) ifTrue:[ |
|
1892 | 189 |
^ (numerator * aNumber) / denominator |
701 | 190 |
]. |
191 |
^ aNumber productFromFraction:self |
|
1892 | 192 |
|
2792 | 193 |
"Modified: 28.7.1997 / 19:09:23 / cg" |
701 | 194 |
! |
195 |
||
1 | 196 |
+ aNumber |
197 |
"return the sum of the receiver and the argument, aNumber" |
|
198 |
||
199 |
|n d| |
|
200 |
||
201 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
202 |
^ (self class numerator:(numerator + (denominator * aNumber)) |
2792 | 203 |
denominator:denominator) |
1 | 204 |
]. |
259 | 205 |
aNumber isFraction ifTrue:[ |
1892 | 206 |
aNumber isFixedPoint ifFalse:[ "/ the value was corrent, but the scale is lost |
207 |
n := aNumber numerator. |
|
208 |
d := aNumber denominator. |
|
1 | 209 |
|
1892 | 210 |
"save a multiplication if possible" |
211 |
denominator == d ifTrue:[ |
|
212 |
n := numerator + n |
|
213 |
] ifFalse:[ |
|
214 |
n := (numerator * d) + (n * denominator). |
|
215 |
d := denominator * d. |
|
216 |
]. |
|
2792 | 217 |
^ (self class numerator:n denominator:d) |
1892 | 218 |
] |
1 | 219 |
]. |
16 | 220 |
(aNumber isMemberOf:Float) ifTrue:[ |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
221 |
^ aNumber + (numerator asFloat / denominator asFloat) |
16 | 222 |
]. |
1 | 223 |
^ aNumber sumFromFraction:self |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
224 |
|
2792 | 225 |
"Modified: 28.7.1997 / 19:09:16 / cg" |
1 | 226 |
! |
227 |
||
228 |
- aNumber |
|
229 |
"return the difference of the receiver and the argument, aNumber" |
|
230 |
||
231 |
|n d| |
|
232 |
||
233 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
234 |
^ (self class numerator:(numerator - (denominator * aNumber)) |
2792 | 235 |
denominator:denominator) |
1 | 236 |
]. |
259 | 237 |
aNumber isFraction ifTrue:[ |
1892 | 238 |
aNumber isFixedPoint ifFalse:[ "/ the value was corrent, but the scale is lost |
239 |
n := aNumber numerator. |
|
240 |
d := aNumber denominator. |
|
1 | 241 |
|
1892 | 242 |
"save a multiplication if possible" |
243 |
denominator == d ifTrue:[ |
|
244 |
n := numerator - n |
|
245 |
] ifFalse:[ |
|
246 |
n := (numerator * d) - (n * denominator). |
|
247 |
d := denominator * d |
|
248 |
]. |
|
2792 | 249 |
^ (self class numerator:n denominator:d) |
1892 | 250 |
] |
1 | 251 |
]. |
16 | 252 |
(aNumber isMemberOf:Float) ifTrue:[ |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
253 |
^ (numerator asFloat / denominator asFloat) - aNumber |
16 | 254 |
]. |
1 | 255 |
^ aNumber differenceFromFraction:self |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
256 |
|
1892 | 257 |
" |
258 |
(1/3) - (1/9) |
|
259 |
(1/9) - (1/3) |
|
260 |
(999/1000) - (1/1000) |
|
261 |
(999/1000) - (1/1000000) |
|
262 |
(999000/1000000) - (1/1000000) |
|
263 |
" |
|
264 |
||
2792 | 265 |
"Modified: 28.7.1997 / 19:09:11 / cg" |
1 | 266 |
! |
267 |
||
268 |
/ aNumber |
|
269 |
"return the quotient of the receiver and the argument, aNumber" |
|
270 |
||
271 |
|n d| |
|
272 |
||
16 | 273 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
1892 | 274 |
^ (self class numerator:numerator |
2792 | 275 |
denominator:(denominator * aNumber)) |
16 | 276 |
]. |
259 | 277 |
aNumber isFraction ifTrue:[ |
1892 | 278 |
aNumber isFixedPoint ifFalse:[ "/ the value was corrent, but the scale is lost |
279 |
n := numerator * aNumber denominator. |
|
280 |
d := denominator * aNumber numerator. |
|
2792 | 281 |
^ (self class numerator:n denominator:d) |
1892 | 282 |
] |
1 | 283 |
]. |
16 | 284 |
(aNumber isMemberOf:Float) ifTrue:[ |
1892 | 285 |
^ numerator / (denominator * aNumber) |
16 | 286 |
]. |
1 | 287 |
^ aNumber quotientFromFraction:self |
1892 | 288 |
|
2792 | 289 |
"Modified: 28.7.1997 / 19:09:06 / cg" |
1 | 290 |
! |
291 |
||
292 |
// aNumber |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
293 |
"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
|
294 |
truncation towards negative infinity." |
1 | 295 |
|
296 |
self negative ifTrue:[ |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
297 |
^ ((numerator * aNumber denominator) // (denominator * aNumber numerator)) - 1 |
1 | 298 |
]. |
299 |
^ (numerator * aNumber denominator) // (denominator * aNumber numerator) |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
300 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
301 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
302 |
0.5 // 1 |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
303 |
-0.5 // 1 |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
304 |
(1/2) // 1 |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
305 |
(1/2) negated // 1 |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
306 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
307 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
308 |
"Modified: 5.11.1996 / 11:47:14 / cg" |
1 | 309 |
! |
310 |
||
311 |
negated |
|
312 |
"optional - could use inherited method ..." |
|
313 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
314 |
^ self class |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
315 |
numerator:(numerator negated) |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
316 |
denominator:denominator |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
317 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
318 |
"Modified: 5.11.1996 / 10:29:11 / cg" |
1 | 319 |
! |
320 |
||
321 |
reciprocal |
|
322 |
"optional - could use inherited method ..." |
|
323 |
||
324 |
numerator == 1 ifTrue:[^ denominator]. |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
325 |
^ self class |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
326 |
numerator:denominator |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
327 |
denominator:numerator |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
328 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
329 |
"Modified: 5.11.1996 / 10:29:22 / cg" |
1 | 330 |
! ! |
331 |
||
701 | 332 |
!Fraction methodsFor:'coercing & converting'! |
333 |
||
1892 | 334 |
asFixedPoint |
335 |
"return the receiver as fixedPoint number. |
|
336 |
Q: what should the scale be here ?" |
|
337 |
||
338 |
^ FixedPoint numerator:numerator denominator:denominator scale:2 |
|
339 |
||
340 |
" |
|
341 |
(1/2) asFixedPoint |
|
342 |
" |
|
343 |
||
344 |
"Created: 5.11.1996 / 15:15:54 / cg" |
|
345 |
! |
|
346 |
||
2140 | 347 |
asFixedPoint:scale |
348 |
"return the receiver as fixedPoint number, with the given number |
|
349 |
of post-decimal-point digits." |
|
350 |
||
351 |
^ FixedPoint numerator:numerator denominator:denominator scale:scale |
|
352 |
||
353 |
" |
|
354 |
(1/2) asFixedPoint:2 |
|
355 |
(1/3) asFixedPoint:2 |
|
356 |
(1/3) asFixedPoint:5 |
|
357 |
(2/3) asFixedPoint:2 |
|
358 |
(2/3) asFixedPoint:5 |
|
359 |
" |
|
360 |
||
361 |
"Created: 5.11.1996 / 15:15:54 / cg" |
|
362 |
"Modified: 10.1.1997 / 19:54:50 / cg" |
|
363 |
! |
|
364 |
||
701 | 365 |
asFloat |
366 |
"return a float with (approximately) my value" |
|
367 |
||
368 |
^ (numerator asFloat) / (denominator asFloat) |
|
369 |
! |
|
1 | 370 |
|
701 | 371 |
asFraction |
372 |
"return the receiver as fraction - thats itself" |
|
373 |
||
374 |
^ self |
|
375 |
! |
|
376 |
||
377 |
asInteger |
|
378 |
"return an integer with my value - will usually truncate" |
|
1 | 379 |
|
380 |
^ numerator // denominator |
|
381 |
! |
|
382 |
||
701 | 383 |
asLargeInteger |
384 |
"return an integer with my value - will usually truncate" |
|
385 |
||
386 |
^ self asInteger asLargeInteger |
|
387 |
! |
|
388 |
||
1200 | 389 |
asShortFloat |
390 |
"return a shortFloat with (approximately) my value" |
|
391 |
||
392 |
^ (numerator asShortFloat) / (denominator asShortFloat) |
|
393 |
||
394 |
"Created: 17.4.1996 / 12:21:08 / cg" |
|
395 |
! |
|
396 |
||
701 | 397 |
coerce:aNumber |
398 |
"return aNumber converted into receivers type" |
|
399 |
||
400 |
^ aNumber asFraction |
|
401 |
! |
|
402 |
||
403 |
generality |
|
404 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
405 |
||
406 |
^ 60 |
|
407 |
! ! |
|
408 |
||
409 |
!Fraction methodsFor:'comparing'! |
|
410 |
||
411 |
< aNumber |
|
412 |
"return true if the receiver is less |
|
413 |
than aNumber, false otherwise." |
|
1 | 414 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
415 |
|d n| |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
416 |
|
701 | 417 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
418 |
^ numerator < (denominator * aNumber) |
701 | 419 |
]. |
420 |
aNumber isFraction ifTrue:[ |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
421 |
d := aNumber denominator. |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
422 |
n := aNumber numerator. |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
423 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
424 |
"/ save a multiplication if possible |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
425 |
d == denominator ifTrue:[ |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
426 |
^ numerator < n |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
427 |
]. |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
428 |
^ (numerator * d) < (denominator * n) |
701 | 429 |
]. |
430 |
^ aNumber lessFromFraction:self |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
431 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
432 |
"Modified: 5.11.1996 / 10:30:52 / cg" |
701 | 433 |
! |
434 |
||
435 |
= aNumber |
|
436 |
"return true, if the argument represents the same numeric value |
|
437 |
as the receiver, false otherwise" |
|
438 |
||
439 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
|
440 |
(denominator = 1) ifFalse:[^ false]. |
|
441 |
^ numerator = aNumber |
|
1 | 442 |
]. |
701 | 443 |
aNumber isFraction ifTrue:[ |
444 |
(numerator = aNumber numerator) ifFalse:[^ false]. |
|
445 |
^ denominator = aNumber denominator |
|
446 |
]. |
|
447 |
^ self retry:#= coercing:aNumber |
|
448 |
! |
|
449 |
||
450 |
> aNumber |
|
451 |
"return true if the receiver is greater |
|
452 |
than aNumber, false otherwise." |
|
453 |
"optional - could use inherited method ..." |
|
454 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
455 |
|d n| |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
456 |
|
701 | 457 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
458 |
^ numerator > (denominator * aNumber) |
701 | 459 |
]. |
460 |
aNumber isFraction ifTrue:[ |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
461 |
d := aNumber denominator. |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
462 |
n := aNumber numerator. |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
463 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
464 |
"/ save a multiplication if possible |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
465 |
d == denominator ifTrue:[ |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
466 |
^ numerator > n |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
467 |
]. |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
468 |
^ (numerator * d) > (denominator * n) |
701 | 469 |
]. |
470 |
^ self retry:#> coercing:aNumber |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
471 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
472 |
"Modified: 5.11.1996 / 10:31:28 / cg" |
1 | 473 |
! ! |
474 |
||
475 |
!Fraction methodsFor:'double dispatching'! |
|
476 |
||
701 | 477 |
differenceFromFloat:aFloat |
478 |
"sent when a float does not know how to subtract the receiver, a fraction" |
|
1 | 479 |
|
701 | 480 |
^ (aFloat * denominator - numerator) / denominator |
1 | 481 |
! |
482 |
||
483 |
differenceFromInteger:anInteger |
|
213 | 484 |
"sent when an integer does not know how to subtract the receiver, a fraction" |
1 | 485 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
486 |
^ (self class |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
487 |
numerator:((anInteger * denominator) - numerator) |
2792 | 488 |
denominator:denominator) |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
489 |
|
2792 | 490 |
"Modified: 28.7.1997 / 19:08:53 / cg" |
1 | 491 |
! |
492 |
||
701 | 493 |
lessFromInteger:anInteger |
494 |
"sent when an integer does not know how to compare to the receiver, a fraction" |
|
495 |
||
496 |
^ (denominator * anInteger) < numerator |
|
497 |
! |
|
498 |
||
499 |
productFromFloat:aFloat |
|
500 |
"sent when a float does not know how to multiply the receiver, a fraction" |
|
501 |
||
502 |
^ aFloat * numerator / denominator |
|
503 |
! |
|
504 |
||
1 | 505 |
productFromInteger:anInteger |
213 | 506 |
"sent when an integer does not know how to multiply the receiver, a fraction" |
1 | 507 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
508 |
^ (self class |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
509 |
numerator:(anInteger * numerator) |
2792 | 510 |
denominator:denominator) |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
511 |
|
2792 | 512 |
"Modified: 28.7.1997 / 19:06:22 / cg" |
1 | 513 |
! |
514 |
||
701 | 515 |
quotientFromFloat:aFloat |
516 |
"sent when a float does not know how to divide by the receiver, a fraction" |
|
517 |
||
518 |
^ (aFloat * denominator) / numerator |
|
519 |
! |
|
520 |
||
324 | 521 |
quotientFromInteger:anInteger |
522 |
"sent when an integer does not know how to divide by the receiver, a fraction" |
|
523 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
524 |
^ (self class |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
525 |
numerator:(anInteger * denominator) |
2792 | 526 |
denominator:numerator) |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
527 |
|
2792 | 528 |
"Modified: 28.7.1997 / 19:08:46 / cg" |
324 | 529 |
! |
530 |
||
1 | 531 |
sumFromFloat:aFloat |
213 | 532 |
"sent when a float does not know how to add the receiver, a fraction" |
1 | 533 |
|
534 |
^ (aFloat * denominator + numerator) / denominator |
|
535 |
! |
|
536 |
||
701 | 537 |
sumFromInteger:anInteger |
538 |
"sent when an integer does not know how to add the receiver, a fraction" |
|
1 | 539 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
540 |
^ (self class |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
541 |
numerator:(numerator + (anInteger * denominator)) |
2792 | 542 |
denominator:denominator) |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
543 |
|
2792 | 544 |
"Modified: 28.7.1997 / 19:08:40 / cg" |
1 | 545 |
! ! |
546 |
||
92 | 547 |
!Fraction methodsFor:'printing & storing'! |
1 | 548 |
|
54 | 549 |
printOn:aStream |
550 |
"append a printed representation of the receiver to the |
|
551 |
argument, aStream" |
|
552 |
||
553 |
aStream nextPut:$(. |
|
554 |
numerator printOn:aStream. |
|
555 |
aStream nextPut:$/. |
|
556 |
denominator printOn:aStream. |
|
557 |
aStream nextPut:$) |
|
1 | 558 |
! ! |
701 | 559 |
|
560 |
!Fraction methodsFor:'private'! |
|
561 |
||
562 |
reduced |
|
563 |
"reduce the receiver" |
|
564 |
||
2790 | 565 |
|gcd den| |
701 | 566 |
|
2790 | 567 |
den := denominator. |
568 |
den < 0 ifTrue:[ |
|
2853 | 569 |
numerator := numerator negated. |
570 |
den := denominator := den negated. |
|
2789
e3e8707d26b4
make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents:
2140
diff
changeset
|
571 |
]. |
e3e8707d26b4
make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents:
2140
diff
changeset
|
572 |
|
2790 | 573 |
den == 1 ifTrue:[^ numerator]. |
701 | 574 |
numerator == 1 ifTrue:[^ self]. |
575 |
numerator == 0 ifTrue:[^ 0]. |
|
576 |
||
2790 | 577 |
gcd := numerator gcd:den. |
578 |
(gcd ~~ 1) ifTrue:[ |
|
2853 | 579 |
numerator := numerator // gcd. |
580 |
denominator := den := den // gcd. |
|
581 |
den < 0 ifTrue:[ |
|
582 |
numerator := numerator negated. |
|
583 |
den := denominator := den negated. |
|
584 |
]. |
|
585 |
(den == 1) ifTrue:[^ numerator]. |
|
701 | 586 |
]. |
587 |
^ self |
|
588 |
! |
|
589 |
||
590 |
setNumerator:num denominator:den |
|
591 |
"set both numerator and denominator" |
|
592 |
||
593 |
numerator := num. |
|
594 |
denominator := den |
|
595 |
! ! |
|
596 |
||
597 |
!Fraction methodsFor:'queries'! |
|
598 |
||
599 |
isFraction |
|
600 |
"return true, if the receiver is some kind of fraction; |
|
601 |
true is returned here - the method is redefined from Object." |
|
602 |
||
603 |
^ true |
|
604 |
! ! |
|
605 |
||
606 |
!Fraction methodsFor:'testing'! |
|
607 |
||
608 |
negative |
|
609 |
"return true if the receiver is negative" |
|
610 |
||
611 |
(numerator < 0) ifTrue:[ |
|
612 |
^ (denominator < 0) not |
|
613 |
]. |
|
614 |
^ (denominator < 0) |
|
615 |
! ! |
|
616 |
||
617 |
!Fraction methodsFor:'truncation and rounding'! |
|
618 |
||
619 |
rounded |
|
620 |
"return the receiver rounded to the nearest integer as integer" |
|
621 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
622 |
"/ mhmh - what about -(1/2) |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
623 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
624 |
|t| |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
625 |
|
701 | 626 |
self negative ifTrue:[ |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
627 |
t := self - (1/2) |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
628 |
] ifFalse:[ |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
629 |
t := self + (1/2) |
701 | 630 |
]. |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
631 |
^ t truncated. |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
632 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
633 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
634 |
(1/3) rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
635 |
(1/3) negated rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
636 |
(1/2) rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
637 |
(1/2) negated rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
638 |
0.5 rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
639 |
-0.5 rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
640 |
(2/3) rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
641 |
(2/3) negated rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
642 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
643 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
644 |
"Modified: 5.11.1996 / 11:32:32 / cg" |
701 | 645 |
! |
646 |
||
647 |
truncated |
|
648 |
"return the receiver truncated towards zero as Integer" |
|
649 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
650 |
^ numerator quo: denominator |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
651 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
652 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
653 |
(3/2) truncated |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
654 |
(3/2) negated truncated |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
655 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
656 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
657 |
"Modified: 5.11.1996 / 12:18:46 / cg" |
701 | 658 |
! ! |
659 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
660 |
!Fraction class methodsFor:'documentation'! |
701 | 661 |
|
662 |
version |
|
2894 | 663 |
^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.41 1997-09-02 20:52:53 cg Exp $' |
701 | 664 |
! ! |
665 |
Fraction initialize! |