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