author | Claus Gittinger <cg@exept.de> |
Wed, 31 Jul 2002 12:24:32 +0200 | |
changeset 6675 | c7e2757d5e80 |
parent 6650 | 35de1d8400b2 |
child 6891 | 212aa8576cf7 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
5364 | 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 |
||
5552 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
1 | 15 |
Number subclass:#Fraction |
5552 | 16 |
instanceVariableNames:'numerator denominator' |
6675 | 17 |
classVariableNames:'FractionOne FractionZero PrintWholeNumbers' |
5552 | 18 |
poolDictionaries:'' |
19 |
category:'Magnitude-Numbers' |
|
1 | 20 |
! |
21 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
22 |
!Fraction class methodsFor:'documentation'! |
88 | 23 |
|
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
5364 | 27 |
All Rights Reserved |
88 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
38 |
documentation |
|
39 |
" |
|
40 |
Instances of Fraction represent fractional numbers consisting of |
|
41 |
a numerator and denominator. Both are themselfes arbitrary precision |
|
6675 | 42 |
integers. |
43 |
Fractions are usually created by dividing Integers using / (for exact division). |
|
44 |
Notice, that all operations on fractions reduce their result; this means, that |
|
45 |
the result of a fraction-operation may return an integer. |
|
46 |
Aka: |
|
47 |
(1 / 7) * 7 -> 1 (not 0.99999999...) |
|
1295 | 48 |
|
1892 | 49 |
Mixed mode arithmetic: |
6675 | 50 |
fraction op fraction -> fraction/integer |
5364 | 51 |
fraction op fix -> fix; scale is fix's scale |
6675 | 52 |
fraction op integer -> fraction/integer |
5364 | 53 |
fraction op float -> float |
1892 | 54 |
|
6675 | 55 |
|
56 |
[classVariables:] |
|
57 |
PrintWholeNumbers Booolean experimental: |
|
58 |
controls how fractions which are greater than 1 are printed. |
|
59 |
if true, print them as a sum of an integral and the fractional part. |
|
60 |
(Large ones are easier to read this way) |
|
61 |
(17/3) printString -> '(5+(2/3))' |
|
62 |
for now, the default is false, for backward compatibility |
|
63 |
||
1295 | 64 |
[author:] |
5364 | 65 |
Claus Gittinger |
1556 | 66 |
|
67 |
[see also:] |
|
5364 | 68 |
Number |
69 |
Float Integer FixedPoint |
|
1295 | 70 |
" |
88 | 71 |
! ! |
1 | 72 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
73 |
!Fraction class methodsFor:'initialization'! |
1 | 74 |
|
75 |
initialize |
|
302 | 76 |
FractionZero isNil ifTrue:[ |
5364 | 77 |
FractionZero := self numerator:0 denominator:1. |
78 |
FractionOne := self numerator:1 denominator:1 |
|
302 | 79 |
] |
1 | 80 |
! ! |
81 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
82 |
!Fraction class methodsFor:'instance creation'! |
1 | 83 |
|
84 |
new |
|
85 |
"create and return a new fraction with value 0" |
|
86 |
||
54 | 87 |
^ self numerator:0 denominator:1 |
1 | 88 |
! |
89 |
||
90 |
numerator:num denominator:den |
|
91 |
"create and return a new fraction with numerator num and denominator den" |
|
92 |
||
2792 | 93 |
|newFraction| |
94 |
||
1 | 95 |
%{ /* NOCONTEXT */ |
369 | 96 |
|
13 | 97 |
/* this check allows subclassing .. */ |
1 | 98 |
if (self == Fraction) { |
5364 | 99 |
if (__bothSmallInteger(num, den)) { |
100 |
if (__CanDoQuickAlignedNew(sizeof(struct __Fraction))) { /* OBJECT ALLOCATION */ |
|
101 |
OBJ newFraction; |
|
102 |
int spc; |
|
1 | 103 |
|
5364 | 104 |
__qCheckedAlignedNew(newFraction, sizeof(struct __Fraction)); |
105 |
__InstPtr(newFraction)->o_class = self; |
|
106 |
if (__intVal(den) < 0) { |
|
107 |
__FractionInstPtr(newFraction)->f_numerator = __MKSMALLINT(- __intVal(num)); |
|
108 |
__FractionInstPtr(newFraction)->f_denominator = __MKSMALLINT(- __intVal(den)); |
|
109 |
} else { |
|
110 |
__FractionInstPtr(newFraction)->f_numerator = num; |
|
111 |
__FractionInstPtr(newFraction)->f_denominator = den; |
|
112 |
} |
|
113 |
if (num == __MKSMALLINT(1)) { |
|
114 |
RETURN ( newFraction ); |
|
115 |
} |
|
116 |
} |
|
117 |
} |
|
1 | 118 |
} |
2790 | 119 |
%}. |
2792 | 120 |
newFraction isNil ifTrue:[ |
5364 | 121 |
newFraction := self basicNew setNumerator:num denominator:den. |
2792 | 122 |
]. |
123 |
^ newFraction reduced |
|
1 | 124 |
! ! |
125 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
126 |
!Fraction class methodsFor:'constants'! |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
127 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
128 |
pi |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
129 |
"return the constant pi as Fraction" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
130 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
131 |
^ self |
6063 | 132 |
numerator: 314159265358979323846264343 |
133 |
denominator:100000000000000000000000000 |
|
134 |
||
135 |
"/ ^ self |
|
136 |
"/ numerator: 314159265358979323846264338327950288419716939937510582097494459 |
|
137 |
"/ denominator:100000000000000000000000000000000000000000000000000000000000000 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
138 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
139 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
140 |
Fraction pi |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
141 |
Fraction pi asFloat |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
142 |
Float pi |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
143 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
144 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
145 |
"Modified: 5.11.1996 / 11:11:44 / cg" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
146 |
! |
701 | 147 |
|
148 |
unity |
|
1555 | 149 |
"return the neutral element for multiplication (1 / 1)" |
701 | 150 |
|
151 |
^ FractionOne |
|
1555 | 152 |
|
153 |
"Modified: 18.7.1996 / 12:26:06 / cg" |
|
701 | 154 |
! |
155 |
||
156 |
zero |
|
1555 | 157 |
"return the neutral element for addition (0 / 1)" |
701 | 158 |
|
159 |
^ FractionZero |
|
1555 | 160 |
|
161 |
"Modified: 18.7.1996 / 12:26:12 / cg" |
|
701 | 162 |
! ! |
163 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
164 |
!Fraction class methodsFor:'queries'! |
3 | 165 |
|
166 |
isBuiltInClass |
|
1264 | 167 |
"return true if this class is known by the run-time-system. |
168 |
Here, true is returned for myself, false for subclasses." |
|
3 | 169 |
|
170 |
^ self == Fraction |
|
1264 | 171 |
|
172 |
"Modified: 23.4.1996 / 15:59:10 / cg" |
|
3 | 173 |
! ! |
174 |
||
1 | 175 |
!Fraction methodsFor:'accessing'! |
176 |
||
177 |
denominator |
|
178 |
"return the denominator" |
|
179 |
||
180 |
^ denominator |
|
181 |
! |
|
182 |
||
701 | 183 |
numerator |
184 |
"return the numerator" |
|
1 | 185 |
|
701 | 186 |
^ numerator |
1 | 187 |
! ! |
188 |
||
189 |
!Fraction methodsFor:'arithmetic'! |
|
190 |
||
701 | 191 |
* aNumber |
192 |
"return the product of the receiver and the argument, aNumber" |
|
193 |
||
194 |
|n d| |
|
195 |
||
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
196 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
197 |
"/ notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
198 |
"/ the following code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
199 |
"/ and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
200 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
201 |
"/ Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
202 |
"/ mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
203 |
"/ (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
204 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
205 |
|
6646 | 206 |
aNumber isInteger ifTrue:[ |
207 |
^ self class |
|
208 |
numerator:(numerator * aNumber) |
|
209 |
denominator:denominator |
|
701 | 210 |
]. |
211 |
aNumber isFraction ifTrue:[ |
|
5364 | 212 |
aNumber isFixedPoint ifFalse:[ "/ the value was corrent, but the scale is lost |
213 |
n := numerator * aNumber numerator. |
|
214 |
d := denominator * aNumber denominator. |
|
6646 | 215 |
^ self class |
216 |
numerator:n |
|
217 |
denominator:d |
|
5364 | 218 |
] |
701 | 219 |
]. |
220 |
(aNumber isMemberOf:Float) ifTrue:[ |
|
5364 | 221 |
^ (numerator * aNumber) / denominator |
701 | 222 |
]. |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
223 |
|
701 | 224 |
^ aNumber productFromFraction:self |
1892 | 225 |
|
2792 | 226 |
"Modified: 28.7.1997 / 19:09:23 / cg" |
701 | 227 |
! |
228 |
||
1 | 229 |
+ aNumber |
230 |
"return the sum of the receiver and the argument, aNumber" |
|
231 |
||
232 |
|n d| |
|
233 |
||
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
234 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
235 |
"/ notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
236 |
"/ the following code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
237 |
"/ and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
238 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
239 |
"/ Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
240 |
"/ mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
241 |
"/ (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
242 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
243 |
|
6646 | 244 |
aNumber isInteger ifTrue:[ |
245 |
^ self class |
|
246 |
numerator:(numerator + (denominator * aNumber)) |
|
247 |
denominator:denominator |
|
1 | 248 |
]. |
259 | 249 |
aNumber isFraction ifTrue:[ |
6576 | 250 |
aNumber isFixedPoint ifFalse:[ "/ the value was correct, but the scale is lost |
5364 | 251 |
n := aNumber numerator. |
252 |
d := aNumber denominator. |
|
1 | 253 |
|
5364 | 254 |
"save a multiplication if possible" |
255 |
denominator == d ifTrue:[ |
|
256 |
n := numerator + n |
|
257 |
] ifFalse:[ |
|
258 |
n := (numerator * d) + (n * denominator). |
|
259 |
d := denominator * d. |
|
260 |
]. |
|
6646 | 261 |
^ self class |
262 |
numerator:n |
|
263 |
denominator:d |
|
5364 | 264 |
] |
1 | 265 |
]. |
16 | 266 |
(aNumber isMemberOf:Float) ifTrue:[ |
6576 | 267 |
^ aNumber + self asFloat |
16 | 268 |
]. |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
269 |
|
1 | 270 |
^ aNumber sumFromFraction:self |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
271 |
|
2792 | 272 |
"Modified: 28.7.1997 / 19:09:16 / cg" |
1 | 273 |
! |
274 |
||
275 |
- aNumber |
|
276 |
"return the difference of the receiver and the argument, aNumber" |
|
277 |
||
278 |
|n d| |
|
279 |
||
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
280 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
281 |
"/ notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
282 |
"/ the following code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
283 |
"/ and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
284 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
285 |
"/ Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
286 |
"/ mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
287 |
"/ (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
288 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
289 |
|
6646 | 290 |
aNumber isInteger ifTrue:[ |
291 |
^ self class |
|
292 |
numerator:(numerator - (denominator * aNumber)) |
|
293 |
denominator:denominator |
|
1 | 294 |
]. |
259 | 295 |
aNumber isFraction ifTrue:[ |
5364 | 296 |
aNumber isFixedPoint ifFalse:[ "/ the value was corrent, but the scale is lost |
297 |
n := aNumber numerator. |
|
298 |
d := aNumber denominator. |
|
1 | 299 |
|
5364 | 300 |
"save a multiplication if possible" |
301 |
denominator == d ifTrue:[ |
|
302 |
n := numerator - n |
|
303 |
] ifFalse:[ |
|
304 |
n := (numerator * d) - (n * denominator). |
|
305 |
d := denominator * d |
|
306 |
]. |
|
6646 | 307 |
^ self class |
308 |
numerator:n |
|
309 |
denominator:d |
|
5364 | 310 |
] |
1 | 311 |
]. |
16 | 312 |
(aNumber isMemberOf:Float) ifTrue:[ |
5364 | 313 |
^ (numerator asFloat / denominator asFloat) - aNumber |
16 | 314 |
]. |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
315 |
|
1 | 316 |
^ aNumber differenceFromFraction:self |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
317 |
|
1892 | 318 |
" |
319 |
(1/3) - (1/9) |
|
320 |
(1/9) - (1/3) |
|
321 |
(999/1000) - (1/1000) |
|
322 |
(999/1000) - (1/1000000) |
|
323 |
(999000/1000000) - (1/1000000) |
|
324 |
" |
|
325 |
||
2792 | 326 |
"Modified: 28.7.1997 / 19:09:11 / cg" |
1 | 327 |
! |
328 |
||
329 |
/ aNumber |
|
330 |
"return the quotient of the receiver and the argument, aNumber" |
|
331 |
||
332 |
|n d| |
|
333 |
||
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
334 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
335 |
"/ notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
336 |
"/ the following code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
337 |
"/ and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
338 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
339 |
"/ Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
340 |
"/ mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
341 |
"/ (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
342 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
343 |
|
6646 | 344 |
aNumber isInteger ifTrue:[ |
345 |
^ self class |
|
346 |
numerator:numerator |
|
347 |
denominator:(denominator * aNumber) |
|
16 | 348 |
]. |
259 | 349 |
aNumber isFraction ifTrue:[ |
5364 | 350 |
aNumber isFixedPoint ifFalse:[ "/ the value was corrent, but the scale is lost |
351 |
n := numerator * aNumber denominator. |
|
352 |
d := denominator * aNumber numerator. |
|
6646 | 353 |
^ self class |
354 |
numerator:n |
|
355 |
denominator:d |
|
5364 | 356 |
] |
1 | 357 |
]. |
16 | 358 |
(aNumber isMemberOf:Float) ifTrue:[ |
5364 | 359 |
^ numerator / (denominator * aNumber) |
16 | 360 |
]. |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
361 |
|
1 | 362 |
^ aNumber quotientFromFraction:self |
1892 | 363 |
|
2792 | 364 |
"Modified: 28.7.1997 / 19:09:06 / cg" |
1 | 365 |
! |
366 |
||
367 |
// aNumber |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
368 |
"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
|
369 |
truncation towards negative infinity." |
1 | 370 |
|
371 |
^ (numerator * aNumber denominator) // (denominator * aNumber numerator) |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
372 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
373 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
374 |
0.5 // 1 |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
375 |
-0.5 // 1 |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3899
diff
changeset
|
376 |
(1/2) // 1 = 0 ifFalse:[self halt]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3899
diff
changeset
|
377 |
(-1/2) // 1 = -1 ifFalse:[self halt]. |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
378 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
379 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3899
diff
changeset
|
380 |
"Modified: / 5.11.1996 / 11:47:14 / cg" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3899
diff
changeset
|
381 |
"Modified: / 13.2.1998 / 09:15:35 / stefan" |
1 | 382 |
! |
383 |
||
384 |
negated |
|
385 |
"optional - could use inherited method ..." |
|
386 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
387 |
^ self class |
5364 | 388 |
numerator:(numerator negated) |
389 |
denominator:denominator |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
390 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
391 |
"Modified: 5.11.1996 / 10:29:11 / cg" |
1 | 392 |
! |
393 |
||
394 |
reciprocal |
|
395 |
"optional - could use inherited method ..." |
|
396 |
||
397 |
numerator == 1 ifTrue:[^ denominator]. |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
398 |
^ self class |
5364 | 399 |
numerator:denominator |
400 |
denominator:numerator |
|
1878
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 |
"Modified: 5.11.1996 / 10:29:22 / cg" |
1 | 403 |
! ! |
404 |
||
701 | 405 |
!Fraction methodsFor:'coercing & converting'! |
406 |
||
1892 | 407 |
asFixedPoint |
408 |
"return the receiver as fixedPoint number. |
|
409 |
Q: what should the scale be here ?" |
|
410 |
||
411 |
^ FixedPoint numerator:numerator denominator:denominator scale:2 |
|
412 |
||
413 |
" |
|
414 |
(1/2) asFixedPoint |
|
415 |
" |
|
416 |
||
417 |
"Created: 5.11.1996 / 15:15:54 / cg" |
|
418 |
! |
|
419 |
||
2140 | 420 |
asFixedPoint:scale |
421 |
"return the receiver as fixedPoint number, with the given number |
|
422 |
of post-decimal-point digits." |
|
423 |
||
424 |
^ FixedPoint numerator:numerator denominator:denominator scale:scale |
|
425 |
||
426 |
" |
|
427 |
(1/2) asFixedPoint:2 |
|
428 |
(1/3) asFixedPoint:2 |
|
429 |
(1/3) asFixedPoint:5 |
|
430 |
(2/3) asFixedPoint:2 |
|
431 |
(2/3) asFixedPoint:5 |
|
432 |
" |
|
433 |
||
434 |
"Created: 5.11.1996 / 15:15:54 / cg" |
|
435 |
"Modified: 10.1.1997 / 19:54:50 / cg" |
|
436 |
! |
|
437 |
||
701 | 438 |
asFloat |
439 |
"return a float with (approximately) my value" |
|
440 |
||
6576 | 441 |
|num den numShift denShift bits| |
6571
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
442 |
|
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
443 |
(numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[ |
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
444 |
^ (numerator asFloat) / (denominator asFloat) |
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
445 |
]. |
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
446 |
|
6576 | 447 |
"Do it the hard way: reduce magnitude and undo reduction on the quotient" |
6571
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
448 |
|
6576 | 449 |
bits := Float precision * 2. "number of bits to preserve (conservative)" |
6571
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
450 |
num := numerator abs. |
6576 | 451 |
numShift := bits - num highBit. "(num highBit - bits) negated" |
452 |
numShift < 0 ifTrue:[num := num bitShift:numShift] ifFalse:[numShift := 0]. |
|
453 |
||
454 |
den := denominator. |
|
455 |
denShift := bits - den highBit. "(den highBit - bits) negated" |
|
456 |
denShift < 0 ifTrue:[den := den bitShift:denShift] ifFalse:[denShift := 0]. |
|
457 |
||
458 |
^ (num asFloat / den asFloat) * (2.0 raisedToInteger:denShift-numShift) * (num sign) |
|
6571
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
459 |
|
6576 | 460 |
" |
461 |
(5/9) asFloat |
|
462 |
(-5/9) asFloat |
|
463 |
(500000000000/900000000000) asFloat |
|
464 |
(-500000000000/900000000000) asFloat |
|
465 |
(500000000000/9) asFloat |
|
466 |
(5/900000000000) asFloat |
|
467 |
89012345678901234567 asFloat / 123456789123456789 asFloat |
|
468 |
(89012345678901234567 / 123456789123456789) asFloat |
|
469 |
||
470 |
( |
|
471 |
180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125 |
|
472 |
/ |
|
473 |
180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250 |
|
474 |
) asFloat |
|
475 |
||
476 |
180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125 |
|
477 |
asFloat / |
|
478 |
180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250 |
|
479 |
asFloat |
|
480 |
" |
|
701 | 481 |
! |
1 | 482 |
|
701 | 483 |
asFraction |
484 |
"return the receiver as fraction - thats itself" |
|
485 |
||
486 |
^ self |
|
487 |
! |
|
488 |
||
489 |
asInteger |
|
490 |
"return an integer with my value - will usually truncate" |
|
1 | 491 |
|
492 |
^ numerator // denominator |
|
493 |
! |
|
494 |
||
701 | 495 |
asLargeInteger |
496 |
"return an integer with my value - will usually truncate" |
|
497 |
||
498 |
^ self asInteger asLargeInteger |
|
499 |
! |
|
500 |
||
1200 | 501 |
asShortFloat |
6576 | 502 |
"return a short float with (approximately) my value" |
503 |
||
504 |
(numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[ |
|
505 |
^ (numerator asShortFloat) / (denominator asShortFloat) |
|
506 |
]. |
|
1200 | 507 |
|
6576 | 508 |
^ self asFloat asShortFloat |
1200 | 509 |
|
6576 | 510 |
" |
511 |
(5/9) asShortFloat |
|
512 |
(500000000000/900000000000) asShortFloat |
|
513 |
(500000000000/9) asShortFloat |
|
514 |
" |
|
1200 | 515 |
! |
516 |
||
701 | 517 |
coerce:aNumber |
518 |
"return aNumber converted into receivers type" |
|
519 |
||
520 |
^ aNumber asFraction |
|
521 |
! |
|
522 |
||
523 |
generality |
|
524 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
525 |
||
526 |
^ 60 |
|
527 |
! ! |
|
528 |
||
529 |
!Fraction methodsFor:'comparing'! |
|
530 |
||
531 |
< aNumber |
|
532 |
"return true if the receiver is less |
|
533 |
than aNumber, false otherwise." |
|
1 | 534 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
535 |
|d n| |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
536 |
|
701 | 537 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
5364 | 538 |
^ numerator < (denominator * aNumber) |
701 | 539 |
]. |
540 |
aNumber isFraction ifTrue:[ |
|
5364 | 541 |
d := aNumber denominator. |
542 |
n := aNumber numerator. |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
543 |
|
5364 | 544 |
"/ save a multiplication if possible |
545 |
d == denominator ifTrue:[ |
|
546 |
^ numerator < n |
|
547 |
]. |
|
548 |
^ (numerator * d) < (denominator * n) |
|
701 | 549 |
]. |
550 |
^ aNumber lessFromFraction:self |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
551 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
552 |
"Modified: 5.11.1996 / 10:30:52 / cg" |
701 | 553 |
! |
554 |
||
555 |
= aNumber |
|
556 |
"return true, if the argument represents the same numeric value |
|
557 |
as the receiver, false otherwise" |
|
558 |
||
559 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
|
5364 | 560 |
(denominator = 1) ifFalse:[^ false]. |
561 |
^ numerator = aNumber |
|
1 | 562 |
]. |
701 | 563 |
aNumber isFraction ifTrue:[ |
5364 | 564 |
denominator = aNumber denominator ifTrue:[ |
565 |
^ numerator = aNumber numerator |
|
566 |
]. |
|
567 |
^ self sameFractionValueAs:aNumber |
|
3631
26a44b284912
handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
568 |
]. |
26a44b284912
handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
569 |
(aNumber isInteger) ifTrue:[ |
5364 | 570 |
(denominator = 1) ifFalse:[^ false]. |
571 |
^ numerator = aNumber |
|
701 | 572 |
]. |
573 |
^ self retry:#= coercing:aNumber |
|
3631
26a44b284912
handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
574 |
|
26a44b284912
handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
575 |
"Modified: / 7.7.1998 / 17:17:07 / cg" |
701 | 576 |
! |
577 |
||
578 |
> aNumber |
|
579 |
"return true if the receiver is greater |
|
580 |
than aNumber, false otherwise." |
|
581 |
"optional - could use inherited method ..." |
|
582 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
583 |
|d n| |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
584 |
|
701 | 585 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
5364 | 586 |
^ numerator > (denominator * aNumber) |
701 | 587 |
]. |
588 |
aNumber isFraction ifTrue:[ |
|
5364 | 589 |
d := aNumber denominator. |
590 |
n := aNumber numerator. |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
591 |
|
5364 | 592 |
"/ save a multiplication if possible |
593 |
d == denominator ifTrue:[ |
|
594 |
^ numerator > n |
|
595 |
]. |
|
596 |
^ (numerator * d) > (denominator * n) |
|
701 | 597 |
]. |
598 |
^ self retry:#> coercing:aNumber |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
599 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
600 |
"Modified: 5.11.1996 / 10:31:28 / cg" |
4593 | 601 |
! |
602 |
||
603 |
hash |
|
604 |
"return a number for hashing; redefined, since fractions compare |
|
605 |
by numeric value (i.e. (9/3) = 3), therefore (9/3) hash must be the same |
|
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
606 |
as 3 hash." |
4593 | 607 |
|
608 |
(denominator = 1) ifTrue:[^ numerator hash]. |
|
609 |
||
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
610 |
^ self asFloat hash |
4593 | 611 |
|
612 |
" |
|
613 |
3 hash |
|
614 |
(9/3) hash |
|
615 |
3.0 hash |
|
616 |
(1/2) hash |
|
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
617 |
(1/4) hash |
4593 | 618 |
0.0 hash |
619 |
0.5 hash |
|
620 |
0.25 hash |
|
621 |
0.4 hash |
|
622 |
" |
|
5364 | 623 |
! |
624 |
||
625 |
sameFractionValueAs:aNumber |
|
626 |
"return true, if the argument represents the same numeric value |
|
627 |
as the receiver, false otherwise" |
|
628 |
||
629 |
|rSelf rNum| |
|
630 |
||
631 |
rSelf := self reduced. |
|
632 |
rNum := aNumber reduced. |
|
633 |
rSelf denominator = rNum denominator ifTrue:[ |
|
634 |
^ rSelf numerator = rNum numerator |
|
635 |
]. |
|
636 |
^ false |
|
1 | 637 |
! ! |
638 |
||
639 |
!Fraction methodsFor:'double dispatching'! |
|
640 |
||
701 | 641 |
differenceFromFloat:aFloat |
642 |
"sent when a float does not know how to subtract the receiver, a fraction" |
|
1 | 643 |
|
701 | 644 |
^ (aFloat * denominator - numerator) / denominator |
1 | 645 |
! |
646 |
||
647 |
differenceFromInteger:anInteger |
|
213 | 648 |
"sent when an integer does not know how to subtract the receiver, a fraction" |
1 | 649 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
650 |
^ (self class |
5364 | 651 |
numerator:((anInteger * denominator) - numerator) |
652 |
denominator:denominator) |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
653 |
|
2792 | 654 |
"Modified: 28.7.1997 / 19:08:53 / cg" |
1 | 655 |
! |
656 |
||
701 | 657 |
lessFromInteger:anInteger |
658 |
"sent when an integer does not know how to compare to the receiver, a fraction" |
|
659 |
||
660 |
^ (denominator * anInteger) < numerator |
|
661 |
! |
|
662 |
||
663 |
productFromFloat:aFloat |
|
664 |
"sent when a float does not know how to multiply the receiver, a fraction" |
|
665 |
||
666 |
^ aFloat * numerator / denominator |
|
667 |
! |
|
668 |
||
1 | 669 |
productFromInteger:anInteger |
213 | 670 |
"sent when an integer does not know how to multiply the receiver, a fraction" |
1 | 671 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
672 |
^ (self class |
5364 | 673 |
numerator:(anInteger * numerator) |
674 |
denominator:denominator) |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
675 |
|
2792 | 676 |
"Modified: 28.7.1997 / 19:06:22 / cg" |
1 | 677 |
! |
678 |
||
701 | 679 |
quotientFromFloat:aFloat |
680 |
"sent when a float does not know how to divide by the receiver, a fraction" |
|
681 |
||
682 |
^ (aFloat * denominator) / numerator |
|
683 |
! |
|
684 |
||
324 | 685 |
quotientFromInteger:anInteger |
686 |
"sent when an integer does not know how to divide by the receiver, a fraction" |
|
687 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
688 |
^ (self class |
5364 | 689 |
numerator:(anInteger * denominator) |
690 |
denominator:numerator) |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
691 |
|
2792 | 692 |
"Modified: 28.7.1997 / 19:08:46 / cg" |
324 | 693 |
! |
694 |
||
1 | 695 |
sumFromFloat:aFloat |
213 | 696 |
"sent when a float does not know how to add the receiver, a fraction" |
1 | 697 |
|
698 |
^ (aFloat * denominator + numerator) / denominator |
|
699 |
! |
|
700 |
||
701 | 701 |
sumFromInteger:anInteger |
702 |
"sent when an integer does not know how to add the receiver, a fraction" |
|
1 | 703 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
704 |
^ (self class |
5364 | 705 |
numerator:(numerator + (anInteger * denominator)) |
706 |
denominator:denominator) |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
707 |
|
2792 | 708 |
"Modified: 28.7.1997 / 19:08:40 / cg" |
1 | 709 |
! ! |
710 |
||
6486 | 711 |
!Fraction methodsFor:'encoding'! |
712 |
||
713 |
encodeOn:anEncoder with:aParameter |
|
714 |
||
715 |
anEncoder encodeFraction:self with:aParameter |
|
716 |
! ! |
|
717 |
||
92 | 718 |
!Fraction methodsFor:'printing & storing'! |
1 | 719 |
|
54 | 720 |
printOn:aStream |
721 |
"append a printed representation of the receiver to the |
|
722 |
argument, aStream" |
|
723 |
||
6675 | 724 |
|t| |
725 |
||
726 |
PrintWholeNumbers == true ifTrue:[ |
|
727 |
"/ experimental: print fractions which are greater than 1 as a sum of |
|
728 |
"/ an integral and the fractional part. They are easier to read this way. |
|
729 |
numerator > denominator ifTrue:[ |
|
730 |
aStream nextPut:$(. |
|
731 |
t := numerator // denominator. |
|
732 |
t printOn:aStream. |
|
733 |
aStream nextPutAll:'+('. |
|
734 |
(numerator - (t*denominator)) printOn:aStream. |
|
735 |
aStream nextPut:$/. |
|
736 |
denominator printOn:aStream. |
|
737 |
aStream nextPutAll:'))'. |
|
738 |
^ self |
|
739 |
]. |
|
740 |
]. |
|
741 |
||
54 | 742 |
aStream nextPut:$(. |
743 |
numerator printOn:aStream. |
|
744 |
aStream nextPut:$/. |
|
745 |
denominator printOn:aStream. |
|
746 |
aStream nextPut:$) |
|
6675 | 747 |
|
748 |
"Modified: / 31.7.2002 / 09:56:41 / cg" |
|
1 | 749 |
! ! |
701 | 750 |
|
751 |
!Fraction methodsFor:'private'! |
|
752 |
||
753 |
reduced |
|
754 |
"reduce the receiver" |
|
755 |
||
2790 | 756 |
|gcd den| |
701 | 757 |
|
2790 | 758 |
den := denominator. |
759 |
den < 0 ifTrue:[ |
|
5364 | 760 |
numerator := numerator negated. |
761 |
den := denominator := den negated. |
|
2789
e3e8707d26b4
make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents:
2140
diff
changeset
|
762 |
]. |
e3e8707d26b4
make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents:
2140
diff
changeset
|
763 |
|
2790 | 764 |
den == 1 ifTrue:[^ numerator]. |
701 | 765 |
numerator == 1 ifTrue:[^ self]. |
766 |
numerator == 0 ifTrue:[^ 0]. |
|
767 |
||
2790 | 768 |
gcd := numerator gcd:den. |
769 |
(gcd ~~ 1) ifTrue:[ |
|
6576 | 770 |
gcd < 0 ifTrue:[ |
771 |
gcd := gcd negated. |
|
772 |
]. |
|
5364 | 773 |
numerator := numerator // gcd. |
774 |
denominator := den := den // gcd. |
|
775 |
(den == 1) ifTrue:[^ numerator]. |
|
701 | 776 |
]. |
777 |
^ self |
|
778 |
! |
|
779 |
||
780 |
setNumerator:num denominator:den |
|
781 |
"set both numerator and denominator" |
|
782 |
||
783 |
numerator := num. |
|
784 |
denominator := den |
|
785 |
! ! |
|
786 |
||
6650 | 787 |
!Fraction methodsFor:'testing'! |
701 | 788 |
|
789 |
isFraction |
|
790 |
"return true, if the receiver is some kind of fraction; |
|
791 |
true is returned here - the method is redefined from Object." |
|
792 |
||
793 |
^ true |
|
6650 | 794 |
! |
701 | 795 |
|
4658
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
796 |
isLiteral |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
797 |
"return true, if the receiver can be used as a literal constant in ST syntax |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
798 |
(i.e. can be used in constant arrays)" |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
799 |
|
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
800 |
^ true |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
801 |
|
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
802 |
! |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
803 |
|
701 | 804 |
negative |
805 |
"return true if the receiver is negative" |
|
806 |
||
807 |
(numerator < 0) ifTrue:[ |
|
5364 | 808 |
^ (denominator < 0) not |
701 | 809 |
]. |
810 |
^ (denominator < 0) |
|
811 |
! ! |
|
812 |
||
5552 | 813 |
!Fraction methodsFor:'truncation & rounding'! |
701 | 814 |
|
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
815 |
fractionPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
816 |
"extract the after-decimal fraction part." |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
817 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
818 |
numerator abs < denominator abs ifTrue:[ |
5364 | 819 |
^ self |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
820 |
]. |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
821 |
^ super fractionPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
822 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
823 |
" |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
824 |
(3/2) fractionPart |
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
825 |
(-3/2) fractionPart |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
826 |
(2/3) fractionPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
827 |
((3/2)*(15/4)) fractionPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
828 |
((2/3)*(4/15)) fractionPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
829 |
" |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
830 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
831 |
"Modified: / 5.11.2001 / 17:55:25 / cg" |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
832 |
! |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
833 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
834 |
integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
835 |
"extract the pre-decimal integer part." |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
836 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
837 |
numerator abs < denominator abs ifTrue:[ |
5364 | 838 |
^ 0 |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
839 |
]. |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
840 |
^ super integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
841 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
842 |
" |
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
843 |
(3/2) integerPart |
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
844 |
(-3/2) integerPart |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
845 |
(2/3) integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
846 |
((3/2)*(15/4)) integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
847 |
((2/3)*(4/15)) integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
848 |
" |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
849 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
850 |
"Modified: / 5.11.2001 / 17:55:01 / cg" |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
851 |
! |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
852 |
|
701 | 853 |
rounded |
854 |
"return the receiver rounded to the nearest integer as integer" |
|
855 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
856 |
"/ mhmh - what about -(1/2) |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
857 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
858 |
|t| |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
859 |
|
701 | 860 |
self negative ifTrue:[ |
5364 | 861 |
t := self - (1/2) |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
862 |
] ifFalse:[ |
5364 | 863 |
t := self + (1/2) |
701 | 864 |
]. |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
865 |
^ t truncated. |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
866 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
867 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
868 |
(1/3) rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
869 |
(1/3) negated rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
870 |
(1/2) rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
871 |
(1/2) negated rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
872 |
0.5 rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
873 |
-0.5 rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
874 |
(2/3) rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
875 |
(2/3) negated rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
876 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
877 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
878 |
"Modified: 5.11.1996 / 11:32:32 / cg" |
701 | 879 |
! |
880 |
||
881 |
truncated |
|
882 |
"return the receiver truncated towards zero as Integer" |
|
883 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
884 |
^ numerator quo: denominator |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
885 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
886 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
887 |
(3/2) truncated |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
888 |
(3/2) negated truncated |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
889 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
890 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
891 |
"Modified: 5.11.1996 / 12:18:46 / cg" |
701 | 892 |
! ! |
893 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
894 |
!Fraction class methodsFor:'documentation'! |
701 | 895 |
|
896 |
version |
|
6675 | 897 |
^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.59 2002-07-31 10:24:32 cg Exp $' |
701 | 898 |
! ! |
899 |
Fraction initialize! |