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