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