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