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