author | Stefan Vogel <sv@exept.de> |
Mon, 08 Apr 2002 12:22:13 +0200 | |
changeset 6486 | 38ae7f67ee12 |
parent 6153 | e92766db42c6 |
child 6571 | 101905a14870 |
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 |
||
416 |
^ (numerator asFloat) / (denominator asFloat) |
|
417 |
! |
|
1 | 418 |
|
701 | 419 |
asFraction |
420 |
"return the receiver as fraction - thats itself" |
|
421 |
||
422 |
^ self |
|
423 |
! |
|
424 |
||
425 |
asInteger |
|
426 |
"return an integer with my value - will usually truncate" |
|
1 | 427 |
|
428 |
^ numerator // denominator |
|
429 |
! |
|
430 |
||
701 | 431 |
asLargeInteger |
432 |
"return an integer with my value - will usually truncate" |
|
433 |
||
434 |
^ self asInteger asLargeInteger |
|
435 |
! |
|
436 |
||
1200 | 437 |
asShortFloat |
438 |
"return a shortFloat with (approximately) my value" |
|
439 |
||
440 |
^ (numerator asShortFloat) / (denominator asShortFloat) |
|
441 |
||
442 |
"Created: 17.4.1996 / 12:21:08 / cg" |
|
443 |
! |
|
444 |
||
701 | 445 |
coerce:aNumber |
446 |
"return aNumber converted into receivers type" |
|
447 |
||
448 |
^ aNumber asFraction |
|
449 |
! |
|
450 |
||
451 |
generality |
|
452 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
453 |
||
454 |
^ 60 |
|
455 |
! ! |
|
456 |
||
457 |
!Fraction methodsFor:'comparing'! |
|
458 |
||
459 |
< aNumber |
|
460 |
"return true if the receiver is less |
|
461 |
than aNumber, false otherwise." |
|
1 | 462 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
463 |
|d n| |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
464 |
|
701 | 465 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
5364 | 466 |
^ numerator < (denominator * aNumber) |
701 | 467 |
]. |
468 |
aNumber isFraction ifTrue:[ |
|
5364 | 469 |
d := aNumber denominator. |
470 |
n := aNumber numerator. |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
471 |
|
5364 | 472 |
"/ save a multiplication if possible |
473 |
d == denominator ifTrue:[ |
|
474 |
^ numerator < n |
|
475 |
]. |
|
476 |
^ (numerator * d) < (denominator * n) |
|
701 | 477 |
]. |
478 |
^ aNumber lessFromFraction:self |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
479 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
480 |
"Modified: 5.11.1996 / 10:30:52 / cg" |
701 | 481 |
! |
482 |
||
483 |
= aNumber |
|
484 |
"return true, if the argument represents the same numeric value |
|
485 |
as the receiver, false otherwise" |
|
486 |
||
487 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
|
5364 | 488 |
(denominator = 1) ifFalse:[^ false]. |
489 |
^ numerator = aNumber |
|
1 | 490 |
]. |
701 | 491 |
aNumber isFraction ifTrue:[ |
5364 | 492 |
denominator = aNumber denominator ifTrue:[ |
493 |
^ numerator = aNumber numerator |
|
494 |
]. |
|
495 |
^ self sameFractionValueAs:aNumber |
|
3631
26a44b284912
handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
496 |
]. |
26a44b284912
handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
497 |
(aNumber isInteger) ifTrue:[ |
5364 | 498 |
(denominator = 1) ifFalse:[^ false]. |
499 |
^ numerator = aNumber |
|
701 | 500 |
]. |
501 |
^ self retry:#= coercing:aNumber |
|
3631
26a44b284912
handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
502 |
|
26a44b284912
handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
503 |
"Modified: / 7.7.1998 / 17:17:07 / cg" |
701 | 504 |
! |
505 |
||
506 |
> aNumber |
|
507 |
"return true if the receiver is greater |
|
508 |
than aNumber, false otherwise." |
|
509 |
"optional - could use inherited method ..." |
|
510 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
511 |
|d n| |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
512 |
|
701 | 513 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
5364 | 514 |
^ numerator > (denominator * aNumber) |
701 | 515 |
]. |
516 |
aNumber isFraction ifTrue:[ |
|
5364 | 517 |
d := aNumber denominator. |
518 |
n := aNumber numerator. |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
519 |
|
5364 | 520 |
"/ save a multiplication if possible |
521 |
d == denominator ifTrue:[ |
|
522 |
^ numerator > n |
|
523 |
]. |
|
524 |
^ (numerator * d) > (denominator * n) |
|
701 | 525 |
]. |
526 |
^ self retry:#> coercing:aNumber |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
527 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
528 |
"Modified: 5.11.1996 / 10:31:28 / cg" |
4593 | 529 |
! |
530 |
||
531 |
hash |
|
532 |
"return a number for hashing; redefined, since fractions compare |
|
533 |
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
|
534 |
as 3 hash." |
4593 | 535 |
|
536 |
(denominator = 1) ifTrue:[^ numerator hash]. |
|
537 |
||
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
538 |
^ self asFloat hash |
4593 | 539 |
|
540 |
" |
|
541 |
3 hash |
|
542 |
(9/3) hash |
|
543 |
3.0 hash |
|
544 |
(1/2) hash |
|
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
545 |
(1/4) hash |
4593 | 546 |
0.0 hash |
547 |
0.5 hash |
|
548 |
0.25 hash |
|
549 |
0.4 hash |
|
550 |
" |
|
5364 | 551 |
! |
552 |
||
553 |
sameFractionValueAs:aNumber |
|
554 |
"return true, if the argument represents the same numeric value |
|
555 |
as the receiver, false otherwise" |
|
556 |
||
557 |
|rSelf rNum| |
|
558 |
||
559 |
rSelf := self reduced. |
|
560 |
rNum := aNumber reduced. |
|
561 |
rSelf denominator = rNum denominator ifTrue:[ |
|
562 |
^ rSelf numerator = rNum numerator |
|
563 |
]. |
|
564 |
^ false |
|
1 | 565 |
! ! |
566 |
||
567 |
!Fraction methodsFor:'double dispatching'! |
|
568 |
||
701 | 569 |
differenceFromFloat:aFloat |
570 |
"sent when a float does not know how to subtract the receiver, a fraction" |
|
1 | 571 |
|
701 | 572 |
^ (aFloat * denominator - numerator) / denominator |
1 | 573 |
! |
574 |
||
575 |
differenceFromInteger:anInteger |
|
213 | 576 |
"sent when an integer does not know how to subtract the receiver, a fraction" |
1 | 577 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
578 |
^ (self class |
5364 | 579 |
numerator:((anInteger * denominator) - numerator) |
580 |
denominator:denominator) |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
581 |
|
2792 | 582 |
"Modified: 28.7.1997 / 19:08:53 / cg" |
1 | 583 |
! |
584 |
||
701 | 585 |
lessFromInteger:anInteger |
586 |
"sent when an integer does not know how to compare to the receiver, a fraction" |
|
587 |
||
588 |
^ (denominator * anInteger) < numerator |
|
589 |
! |
|
590 |
||
591 |
productFromFloat:aFloat |
|
592 |
"sent when a float does not know how to multiply the receiver, a fraction" |
|
593 |
||
594 |
^ aFloat * numerator / denominator |
|
595 |
! |
|
596 |
||
1 | 597 |
productFromInteger:anInteger |
213 | 598 |
"sent when an integer does not know how to multiply the receiver, a fraction" |
1 | 599 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
600 |
^ (self class |
5364 | 601 |
numerator:(anInteger * numerator) |
602 |
denominator:denominator) |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
603 |
|
2792 | 604 |
"Modified: 28.7.1997 / 19:06:22 / cg" |
1 | 605 |
! |
606 |
||
701 | 607 |
quotientFromFloat:aFloat |
608 |
"sent when a float does not know how to divide by the receiver, a fraction" |
|
609 |
||
610 |
^ (aFloat * denominator) / numerator |
|
611 |
! |
|
612 |
||
324 | 613 |
quotientFromInteger:anInteger |
614 |
"sent when an integer does not know how to divide by the receiver, a fraction" |
|
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 * denominator) |
618 |
denominator:numerator) |
|
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:08:46 / cg" |
324 | 621 |
! |
622 |
||
1 | 623 |
sumFromFloat:aFloat |
213 | 624 |
"sent when a float does not know how to add the receiver, a fraction" |
1 | 625 |
|
626 |
^ (aFloat * denominator + numerator) / denominator |
|
627 |
! |
|
628 |
||
701 | 629 |
sumFromInteger:anInteger |
630 |
"sent when an integer does not know how to add the receiver, a fraction" |
|
1 | 631 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
632 |
^ (self class |
5364 | 633 |
numerator:(numerator + (anInteger * denominator)) |
634 |
denominator:denominator) |
|
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:40 / cg" |
1 | 637 |
! ! |
638 |
||
6486 | 639 |
!Fraction methodsFor:'encoding'! |
640 |
||
641 |
encodeOn:anEncoder with:aParameter |
|
642 |
||
643 |
anEncoder encodeFraction:self with:aParameter |
|
644 |
! ! |
|
645 |
||
92 | 646 |
!Fraction methodsFor:'printing & storing'! |
1 | 647 |
|
54 | 648 |
printOn:aStream |
649 |
"append a printed representation of the receiver to the |
|
650 |
argument, aStream" |
|
651 |
||
652 |
aStream nextPut:$(. |
|
653 |
numerator printOn:aStream. |
|
654 |
aStream nextPut:$/. |
|
655 |
denominator printOn:aStream. |
|
656 |
aStream nextPut:$) |
|
1 | 657 |
! ! |
701 | 658 |
|
659 |
!Fraction methodsFor:'private'! |
|
660 |
||
661 |
reduced |
|
662 |
"reduce the receiver" |
|
663 |
||
2790 | 664 |
|gcd den| |
701 | 665 |
|
2790 | 666 |
den := denominator. |
667 |
den < 0 ifTrue:[ |
|
5364 | 668 |
numerator := numerator negated. |
669 |
den := denominator := den negated. |
|
2789
e3e8707d26b4
make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents:
2140
diff
changeset
|
670 |
]. |
e3e8707d26b4
make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents:
2140
diff
changeset
|
671 |
|
2790 | 672 |
den == 1 ifTrue:[^ numerator]. |
701 | 673 |
numerator == 1 ifTrue:[^ self]. |
674 |
numerator == 0 ifTrue:[^ 0]. |
|
675 |
||
2790 | 676 |
gcd := numerator gcd:den. |
677 |
(gcd ~~ 1) ifTrue:[ |
|
5364 | 678 |
numerator := numerator // gcd. |
679 |
denominator := den := den // gcd. |
|
680 |
den < 0 ifTrue:[ |
|
681 |
numerator := numerator negated. |
|
682 |
den := denominator := den negated. |
|
683 |
]. |
|
684 |
(den == 1) ifTrue:[^ numerator]. |
|
701 | 685 |
]. |
686 |
^ self |
|
687 |
! |
|
688 |
||
689 |
setNumerator:num denominator:den |
|
690 |
"set both numerator and denominator" |
|
691 |
||
692 |
numerator := num. |
|
693 |
denominator := den |
|
694 |
! ! |
|
695 |
||
696 |
!Fraction methodsFor:'queries'! |
|
697 |
||
698 |
isFraction |
|
699 |
"return true, if the receiver is some kind of fraction; |
|
700 |
true is returned here - the method is redefined from Object." |
|
701 |
||
702 |
^ true |
|
703 |
! ! |
|
704 |
||
705 |
!Fraction methodsFor:'testing'! |
|
706 |
||
4658
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
707 |
isLiteral |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
708 |
"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
|
709 |
(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
|
710 |
|
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
711 |
^ true |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
712 |
|
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
713 |
! |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
714 |
|
701 | 715 |
negative |
716 |
"return true if the receiver is negative" |
|
717 |
||
718 |
(numerator < 0) ifTrue:[ |
|
5364 | 719 |
^ (denominator < 0) not |
701 | 720 |
]. |
721 |
^ (denominator < 0) |
|
722 |
! ! |
|
723 |
||
5552 | 724 |
!Fraction methodsFor:'truncation & rounding'! |
701 | 725 |
|
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
726 |
fractionPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
727 |
"extract the after-decimal fraction part." |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
728 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
729 |
numerator abs < denominator abs ifTrue:[ |
5364 | 730 |
^ self |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
731 |
]. |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
732 |
^ super fractionPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
733 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
734 |
" |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
735 |
(3/2) fractionPart |
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
736 |
(-3/2) fractionPart |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
737 |
(2/3) fractionPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
738 |
((3/2)*(15/4)) fractionPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
739 |
((2/3)*(4/15)) fractionPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
740 |
" |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
741 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
742 |
"Modified: / 5.11.2001 / 17:55:25 / cg" |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
743 |
! |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
744 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
745 |
integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
746 |
"extract the pre-decimal integer part." |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
747 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
748 |
numerator abs < denominator abs ifTrue:[ |
5364 | 749 |
^ 0 |
3899
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 |
^ super integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
752 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
753 |
" |
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
754 |
(3/2) integerPart |
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
755 |
(-3/2) integerPart |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
756 |
(2/3) integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
757 |
((3/2)*(15/4)) integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
758 |
((2/3)*(4/15)) integerPart |
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 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
761 |
"Modified: / 5.11.2001 / 17:55:01 / cg" |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
762 |
! |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
763 |
|
701 | 764 |
rounded |
765 |
"return the receiver rounded to the nearest integer as integer" |
|
766 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
767 |
"/ mhmh - what about -(1/2) |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
768 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
769 |
|t| |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
770 |
|
701 | 771 |
self negative ifTrue:[ |
5364 | 772 |
t := self - (1/2) |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
773 |
] ifFalse:[ |
5364 | 774 |
t := self + (1/2) |
701 | 775 |
]. |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
776 |
^ t truncated. |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
777 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
778 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
779 |
(1/3) rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
780 |
(1/3) negated rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
781 |
(1/2) rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
782 |
(1/2) negated rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
783 |
0.5 rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
784 |
-0.5 rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
785 |
(2/3) rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
786 |
(2/3) negated rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
787 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
788 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
789 |
"Modified: 5.11.1996 / 11:32:32 / cg" |
701 | 790 |
! |
791 |
||
792 |
truncated |
|
793 |
"return the receiver truncated towards zero as Integer" |
|
794 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
795 |
^ numerator quo: denominator |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
796 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
797 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
798 |
(3/2) truncated |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
799 |
(3/2) negated truncated |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
800 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
801 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
802 |
"Modified: 5.11.1996 / 12:18:46 / cg" |
701 | 803 |
! ! |
804 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
805 |
!Fraction class methodsFor:'documentation'! |
701 | 806 |
|
807 |
version |
|
6486 | 808 |
^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.54 2002-04-08 10:22:13 stefan Exp $' |
701 | 809 |
! ! |
810 |
Fraction initialize! |