author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 31 Oct 2011 22:19:21 +0000 | |
branch | jv |
changeset 17892 | d86c8bd5ece3 |
parent 17869 | 9610c6c94e71 |
child 17910 | 8d796ca8bd1d |
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 |
" |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
134 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
135 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
136 |
readFrom:aStringOrStream onError:exceptionBlock |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
137 |
|s numerator denominator| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
138 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
139 |
"/ sigh - care for subclasses... |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
140 |
self == Fraction ifFalse:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
141 |
^ super readFrom:aStringOrStream onError:exceptionBlock |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
142 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
143 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
144 |
s := aStringOrStream readStream. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
145 |
s skipSeparators. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
146 |
s peek == $( ifTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
147 |
s next. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
148 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
149 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
150 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
151 |
numerator := super readFrom:s onError:[^ exceptionBlock value]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
152 |
numerator isInteger ifTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
153 |
s skipSeparators. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
154 |
(s peek == $/) ifTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
155 |
s next. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
156 |
denominator := Integer readFrom:s onError:[^ exceptionBlock value]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
157 |
^ self numerator:numerator denominator:denominator |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
158 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
159 |
^ numerator |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
160 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
161 |
^ numerator asFraction |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
162 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
163 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
164 |
Fraction readFrom:'1' |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
165 |
Fraction readFrom:'2' |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
166 |
Fraction readFrom:'1.5' |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
167 |
Fraction readFrom:'1/5' |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
168 |
" |
1 | 169 |
! ! |
170 |
||
7442 | 171 |
!Fraction class methodsFor:'class initialization'! |
172 |
||
173 |
initialize |
|
174 |
FractionZero isNil ifTrue:[ |
|
175 |
FractionZero := self numerator:0 denominator:1. |
|
176 |
FractionOne := self numerator:1 denominator:1 |
|
177 |
] |
|
178 |
! ! |
|
179 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
180 |
!Fraction class methodsFor:'constants'! |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
181 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
182 |
pi |
11671 | 183 |
"return an approximation of the constant pi as Fraction. |
184 |
The approx. returned here has an error smaller than representable by float instances" |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
185 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
186 |
^ self |
11671 | 187 |
numerator:314159265358979323846264343 |
6063 | 188 |
denominator:100000000000000000000000000 |
189 |
||
190 |
"/ ^ self |
|
191 |
"/ numerator: 314159265358979323846264338327950288419716939937510582097494459 |
|
192 |
"/ denominator:100000000000000000000000000000000000000000000000000000000000000 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
193 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
194 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
195 |
Fraction pi |
17841 | 196 |
Fraction pi asFloat - Float pi |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
197 |
Float pi |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
198 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
199 |
|
17841 | 200 |
"Modified: / 03-05-2011 / 11:08:46 / cg" |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
201 |
! |
701 | 202 |
|
11671 | 203 |
pi_approximation |
204 |
"return an approximation of the constant pi as Fraction. |
|
205 |
The approx. returned is good for 6 valid digits and has an error of less than -2.67-07. |
|
206 |
The value might be useful to avoid floating point numbers in graphic rendering code, |
|
207 |
where 6 digits of precision are usually good enough." |
|
208 |
||
209 |
^ self |
|
210 |
numerator:355 |
|
211 |
denominator:113 |
|
212 |
||
213 |
" |
|
214 |
Fraction pi |
|
215 |
Fraction pi asFloat |
|
216 |
Float pi - Fraction pi_approximation asFloat |
|
217 |
" |
|
218 |
! |
|
219 |
||
701 | 220 |
unity |
1555 | 221 |
"return the neutral element for multiplication (1 / 1)" |
701 | 222 |
|
223 |
^ FractionOne |
|
1555 | 224 |
|
225 |
"Modified: 18.7.1996 / 12:26:06 / cg" |
|
701 | 226 |
! |
227 |
||
228 |
zero |
|
1555 | 229 |
"return the neutral element for addition (0 / 1)" |
701 | 230 |
|
231 |
^ FractionZero |
|
1555 | 232 |
|
233 |
"Modified: 18.7.1996 / 12:26:12 / cg" |
|
701 | 234 |
! ! |
235 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
236 |
!Fraction class methodsFor:'queries'! |
3 | 237 |
|
238 |
isBuiltInClass |
|
1264 | 239 |
"return true if this class is known by the run-time-system. |
240 |
Here, true is returned for myself, false for subclasses." |
|
3 | 241 |
|
242 |
^ self == Fraction |
|
1264 | 243 |
|
244 |
"Modified: 23.4.1996 / 15:59:10 / cg" |
|
3 | 245 |
! ! |
246 |
||
1 | 247 |
!Fraction methodsFor:'accessing'! |
248 |
||
249 |
denominator |
|
250 |
"return the denominator" |
|
251 |
||
252 |
^ denominator |
|
253 |
! |
|
254 |
||
701 | 255 |
numerator |
256 |
"return the numerator" |
|
1 | 257 |
|
701 | 258 |
^ numerator |
1 | 259 |
! ! |
260 |
||
261 |
!Fraction methodsFor:'arithmetic'! |
|
262 |
||
701 | 263 |
* aNumber |
11734 | 264 |
"return the product of the receiver and the argument." |
701 | 265 |
|
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
266 |
"/ notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
267 |
"/ the following code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
268 |
"/ and exists as an optimization, to speed up those cases. |
7355 | 269 |
"/ also notice, that checks for those cases must be inlinable without |
270 |
"/ 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
|
271 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
272 |
"/ Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
273 |
"/ mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
274 |
"/ (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
275 |
|
7355 | 276 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
6646 | 277 |
^ self class |
278 |
numerator:(numerator * aNumber) |
|
279 |
denominator:denominator |
|
701 | 280 |
]. |
281 |
(aNumber isMemberOf:Float) ifTrue:[ |
|
5364 | 282 |
^ (numerator * aNumber) / denominator |
701 | 283 |
]. |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
284 |
|
701 | 285 |
^ aNumber productFromFraction:self |
1892 | 286 |
|
2792 | 287 |
"Modified: 28.7.1997 / 19:09:23 / cg" |
701 | 288 |
! |
289 |
||
1 | 290 |
+ aNumber |
291 |
"return the sum of the receiver and the argument, aNumber" |
|
292 |
||
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
293 |
"/ notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
294 |
"/ the following code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
295 |
"/ and exists as an optimization, to speed up those cases. |
7355 | 296 |
"/ also notice, that checks for those cases must be inlinable without |
297 |
"/ 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
|
298 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
299 |
"/ Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
300 |
"/ mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
301 |
"/ (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
302 |
|
7355 | 303 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
6646 | 304 |
^ self class |
7355 | 305 |
numerator:(numerator + (denominator * aNumber)) |
306 |
denominator:denominator |
|
1 | 307 |
]. |
16 | 308 |
(aNumber isMemberOf:Float) ifTrue:[ |
7355 | 309 |
^ (numerator asFloat / denominator asFloat) + aNumber |
16 | 310 |
]. |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
311 |
|
1 | 312 |
^ aNumber sumFromFraction:self |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
313 |
|
2792 | 314 |
"Modified: 28.7.1997 / 19:09:16 / cg" |
1 | 315 |
! |
316 |
||
317 |
- aNumber |
|
318 |
"return the difference of the receiver and the argument, aNumber" |
|
319 |
||
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
320 |
"/ notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
321 |
"/ the following code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
322 |
"/ and exists as an optimization, to speed up those cases. |
7355 | 323 |
"/ also notice, that checks for those cases must be inlinable without |
324 |
"/ 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
|
325 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
326 |
"/ Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
327 |
"/ mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
328 |
"/ (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
329 |
|
7355 | 330 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
6646 | 331 |
^ self class |
332 |
numerator:(numerator - (denominator * aNumber)) |
|
333 |
denominator:denominator |
|
1 | 334 |
]. |
16 | 335 |
(aNumber isMemberOf:Float) ifTrue:[ |
5364 | 336 |
^ (numerator asFloat / denominator asFloat) - aNumber |
16 | 337 |
]. |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
338 |
|
1 | 339 |
^ aNumber differenceFromFraction:self |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
340 |
|
1892 | 341 |
" |
342 |
(1/3) - (1/9) |
|
343 |
(1/9) - (1/3) |
|
344 |
(999/1000) - (1/1000) |
|
345 |
(999/1000) - (1/1000000) |
|
346 |
(999000/1000000) - (1/1000000) |
|
347 |
" |
|
348 |
||
2792 | 349 |
"Modified: 28.7.1997 / 19:09:11 / cg" |
1 | 350 |
! |
351 |
||
352 |
/ aNumber |
|
353 |
"return the quotient of the receiver and the argument, aNumber" |
|
354 |
||
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
355 |
"/ notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
356 |
"/ the following code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
357 |
"/ and exists as an optimization, to speed up those cases. |
7355 | 358 |
"/ also notice, that checks for those cases must be inlinable without |
359 |
"/ 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
|
360 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
361 |
"/ Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
362 |
"/ mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
363 |
"/ (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
364 |
|
7355 | 365 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
6646 | 366 |
^ self class |
367 |
numerator:numerator |
|
368 |
denominator:(denominator * aNumber) |
|
16 | 369 |
]. |
370 |
(aNumber isMemberOf:Float) ifTrue:[ |
|
5364 | 371 |
^ numerator / (denominator * aNumber) |
16 | 372 |
]. |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
373 |
|
1 | 374 |
^ aNumber quotientFromFraction:self |
1892 | 375 |
|
2792 | 376 |
"Modified: 28.7.1997 / 19:09:06 / cg" |
1 | 377 |
! |
378 |
||
379 |
// aNumber |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
380 |
"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
|
381 |
truncation towards negative infinity." |
1 | 382 |
|
383 |
^ (numerator * aNumber denominator) // (denominator * aNumber numerator) |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
384 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
385 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
386 |
0.5 // 1 |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
387 |
-0.5 // 1 |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3899
diff
changeset
|
388 |
(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
|
389 |
(-1/2) // 1 = -1 ifFalse:[self halt]. |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
390 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
391 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3899
diff
changeset
|
392 |
"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
|
393 |
"Modified: / 13.2.1998 / 09:15:35 / stefan" |
1 | 394 |
! |
395 |
||
396 |
negated |
|
397 |
"optional - could use inherited method ..." |
|
398 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
399 |
^ self class |
5364 | 400 |
numerator:(numerator negated) |
401 |
denominator:denominator |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
402 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
403 |
"Modified: 5.11.1996 / 10:29:11 / cg" |
1 | 404 |
! |
405 |
||
406 |
reciprocal |
|
407 |
"optional - could use inherited method ..." |
|
408 |
||
409 |
numerator == 1 ifTrue:[^ denominator]. |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
410 |
^ self class |
5364 | 411 |
numerator:denominator |
412 |
denominator:numerator |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
413 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
414 |
"Modified: 5.11.1996 / 10:29:22 / cg" |
1 | 415 |
! ! |
416 |
||
701 | 417 |
!Fraction methodsFor:'coercing & converting'! |
418 |
||
1892 | 419 |
asFixedPoint |
420 |
"return the receiver as fixedPoint number. |
|
421 |
Q: what should the scale be here ?" |
|
422 |
||
423 |
^ FixedPoint numerator:numerator denominator:denominator scale:2 |
|
424 |
||
425 |
" |
|
426 |
(1/2) asFixedPoint |
|
427 |
" |
|
428 |
||
429 |
"Created: 5.11.1996 / 15:15:54 / cg" |
|
430 |
! |
|
431 |
||
2140 | 432 |
asFixedPoint:scale |
433 |
"return the receiver as fixedPoint number, with the given number |
|
434 |
of post-decimal-point digits." |
|
435 |
||
436 |
^ FixedPoint numerator:numerator denominator:denominator scale:scale |
|
437 |
||
438 |
" |
|
439 |
(1/2) asFixedPoint:2 |
|
440 |
(1/3) asFixedPoint:2 |
|
441 |
(1/3) asFixedPoint:5 |
|
442 |
(2/3) asFixedPoint:2 |
|
443 |
(2/3) asFixedPoint:5 |
|
444 |
" |
|
445 |
||
446 |
"Created: 5.11.1996 / 15:15:54 / cg" |
|
447 |
"Modified: 10.1.1997 / 19:54:50 / cg" |
|
448 |
! |
|
449 |
||
701 | 450 |
asFloat |
11239 | 451 |
"return a float with (approximately) my value. |
452 |
Since floats have a limited precision, you usually loose bits when doing this." |
|
701 | 453 |
|
7442 | 454 |
|num den numShift denShift bits rslt| |
6571
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
455 |
|
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
456 |
(numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[ |
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
457 |
^ (numerator asFloat) / (denominator asFloat) |
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
458 |
]. |
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
459 |
|
6576 | 460 |
"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
|
461 |
|
6576 | 462 |
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
|
463 |
num := numerator abs. |
6576 | 464 |
numShift := bits - num highBit. "(num highBit - bits) negated" |
465 |
numShift < 0 ifTrue:[num := num bitShift:numShift] ifFalse:[numShift := 0]. |
|
466 |
||
467 |
den := denominator. |
|
468 |
denShift := bits - den highBit. "(den highBit - bits) negated" |
|
469 |
denShift < 0 ifTrue:[den := den bitShift:denShift] ifFalse:[denShift := 0]. |
|
470 |
||
7442 | 471 |
rslt := (num asFloat / den asFloat) * (2 raisedToInteger:denShift-numShift). |
472 |
numerator negative ifTrue:[ ^ rslt negated ]. |
|
473 |
^ rslt. |
|
6571
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
474 |
|
6576 | 475 |
" |
476 |
(5/9) asFloat |
|
477 |
(-5/9) asFloat |
|
478 |
(500000000000/900000000000) asFloat |
|
479 |
(-500000000000/900000000000) asFloat |
|
480 |
(500000000000/9) asFloat |
|
481 |
(5/900000000000) asFloat |
|
482 |
89012345678901234567 asFloat / 123456789123456789 asFloat |
|
483 |
(89012345678901234567 / 123456789123456789) asFloat |
|
484 |
||
485 |
( |
|
486 |
180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125 |
|
487 |
/ |
|
488 |
180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250 |
|
489 |
) asFloat |
|
490 |
||
491 |
180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125 |
|
492 |
asFloat / |
|
493 |
180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250 |
|
494 |
asFloat |
|
495 |
" |
|
701 | 496 |
! |
1 | 497 |
|
701 | 498 |
asFraction |
499 |
"return the receiver as fraction - thats itself" |
|
500 |
||
501 |
^ self |
|
502 |
! |
|
503 |
||
504 |
asInteger |
|
505 |
"return an integer with my value - will usually truncate" |
|
1 | 506 |
|
507 |
^ numerator // denominator |
|
508 |
! |
|
509 |
||
7442 | 510 |
asLargeFloat |
511 |
"return a large float with (approximately) my value" |
|
512 |
||
513 |
^ (numerator asLargeFloat) / (denominator asLargeFloat) |
|
514 |
||
515 |
" |
|
516 |
(5/9) asLargeFloat |
|
517 |
(500000000000/900000000000) asLargeFloat |
|
518 |
(500000000000/9) asLargeFloat |
|
519 |
" |
|
520 |
! |
|
521 |
||
701 | 522 |
asLargeInteger |
523 |
"return an integer with my value - will usually truncate" |
|
524 |
||
525 |
^ self asInteger asLargeInteger |
|
526 |
! |
|
527 |
||
7355 | 528 |
asLongFloat |
529 |
"return a long float with (approximately) my value" |
|
530 |
||
7442 | 531 |
|num den numShift denShift numBits rslt| |
7355 | 532 |
|
533 |
(numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[ |
|
534 |
^ (numerator asLongFloat) / (denominator asLongFloat) |
|
535 |
]. |
|
536 |
||
537 |
"Do it the hard way: reduce magnitude and undo reduction on the quotient" |
|
538 |
||
7442 | 539 |
numBits := LongFloat precision * 2. "number of bits to preserve (conservative)" |
7355 | 540 |
num := numerator abs. |
7442 | 541 |
numShift := numBits - num highBit. "(num highBit - bits) negated" |
542 |
numShift < 0 ifTrue:[ |
|
543 |
num := num bitShift:numShift |
|
544 |
] ifFalse:[ |
|
545 |
numShift := 0 |
|
546 |
]. |
|
7355 | 547 |
|
548 |
den := denominator. |
|
7442 | 549 |
denShift := numBits - den highBit. "(den highBit - bits) negated" |
550 |
denShift < 0 ifTrue:[ |
|
551 |
den := den bitShift:denShift |
|
552 |
] ifFalse:[ |
|
553 |
denShift := 0 |
|
554 |
]. |
|
7355 | 555 |
|
7730
dda82553acc7
make #asLongFloat return a LongFloat (returned Float)
Stefan Vogel <sv@exept.de>
parents:
7571
diff
changeset
|
556 |
rslt := (num asLongFloat / den asLongFloat) * (2 raisedToInteger:denShift-numShift). |
7442 | 557 |
numerator negative ifTrue:[ ^ rslt negated ]. |
558 |
^ rslt. |
|
7355 | 559 |
|
560 |
" |
|
7442 | 561 |
(5/9) asLongFloat |
562 |
(-5/9) asLongFloat |
|
563 |
(Fraction basicNew setNumerator:500000000000 denominator:900000000000) asLongFloat = (5/9) asLongFloat |
|
564 |
(Fraction basicNew setNumerator:500000000001 denominator:900000000000) asLongFloat = (5/9) asLongFloat |
|
565 |
(500000000001/900000000000) asLongFloat |
|
566 |
(-500000000001/900000000000) asLongFloat |
|
567 |
(500000000001/900000000000) asLongFloat = (5/9) asLongFloat |
|
568 |
||
569 |
(500000000000/9) asLongFloat |
|
570 |
(5/900000000000) asLongFloat |
|
571 |
89012345678901234567 asFloat / 123456789123456789 asLongFloat |
|
572 |
(89012345678901234567 / 123456789123456789) asLongFloat |
|
573 |
(-89012345678901234567 / 123456789123456789) asLongFloat |
|
7355 | 574 |
|
575 |
( |
|
576 |
180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125 |
|
577 |
/ |
|
578 |
180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250 |
|
7442 | 579 |
) asLongFloat |
7355 | 580 |
|
581 |
180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125 |
|
582 |
asLongFloat / |
|
583 |
180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250 |
|
584 |
asLongFloat |
|
585 |
" |
|
586 |
! |
|
587 |
||
1200 | 588 |
asShortFloat |
6576 | 589 |
"return a short float with (approximately) my value" |
590 |
||
591 |
(numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[ |
|
592 |
^ (numerator asShortFloat) / (denominator asShortFloat) |
|
593 |
]. |
|
1200 | 594 |
|
6576 | 595 |
^ self asFloat asShortFloat |
1200 | 596 |
|
6576 | 597 |
" |
598 |
(5/9) asShortFloat |
|
599 |
(500000000000/900000000000) asShortFloat |
|
600 |
(500000000000/9) asShortFloat |
|
601 |
" |
|
1200 | 602 |
! |
603 |
||
701 | 604 |
coerce:aNumber |
11722 | 605 |
"convert the argument aNumber into an instance of the receivers class and return it." |
701 | 606 |
|
607 |
^ aNumber asFraction |
|
608 |
! |
|
609 |
||
610 |
generality |
|
611 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
612 |
||
613 |
^ 60 |
|
614 |
! ! |
|
615 |
||
616 |
!Fraction methodsFor:'comparing'! |
|
617 |
||
618 |
< aNumber |
|
619 |
"return true if the receiver is less |
|
620 |
than aNumber, false otherwise." |
|
1 | 621 |
|
701 | 622 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
5364 | 623 |
^ numerator < (denominator * aNumber) |
701 | 624 |
]. |
625 |
^ aNumber lessFromFraction:self |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
626 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
627 |
"Modified: 5.11.1996 / 10:30:52 / cg" |
701 | 628 |
! |
629 |
||
630 |
= aNumber |
|
631 |
"return true, if the argument represents the same numeric value |
|
632 |
as the receiver, false otherwise" |
|
633 |
||
634 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
|
6891
212aa8576cf7
oops - did not compare correctly against unnormalized numbers
Claus Gittinger <cg@exept.de>
parents:
6675
diff
changeset
|
635 |
(denominator == 1) ifFalse:[ |
212aa8576cf7
oops - did not compare correctly against unnormalized numbers
Claus Gittinger <cg@exept.de>
parents:
6675
diff
changeset
|
636 |
^ numerator = (aNumber * denominator) |
212aa8576cf7
oops - did not compare correctly against unnormalized numbers
Claus Gittinger <cg@exept.de>
parents:
6675
diff
changeset
|
637 |
]. |
5364 | 638 |
^ numerator = aNumber |
1 | 639 |
]. |
7355 | 640 |
^ aNumber equalFromFraction:self |
3631
26a44b284912
handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
641 |
|
26a44b284912
handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
642 |
"Modified: / 7.7.1998 / 17:17:07 / cg" |
701 | 643 |
! |
644 |
||
645 |
> aNumber |
|
646 |
"return true if the receiver is greater |
|
647 |
than aNumber, false otherwise." |
|
648 |
"optional - could use inherited method ..." |
|
649 |
||
650 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
|
5364 | 651 |
^ numerator > (denominator * aNumber) |
701 | 652 |
]. |
7355 | 653 |
^ aNumber < self |
4593 | 654 |
! |
655 |
||
656 |
hash |
|
657 |
"return a number for hashing; redefined, since fractions compare |
|
658 |
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
|
659 |
as 3 hash." |
4593 | 660 |
|
661 |
(denominator = 1) ifTrue:[^ numerator hash]. |
|
662 |
||
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
663 |
^ self asFloat hash |
4593 | 664 |
|
665 |
" |
|
666 |
3 hash |
|
667 |
(9/3) hash |
|
668 |
3.0 hash |
|
669 |
(1/2) hash |
|
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
670 |
(1/4) hash |
4593 | 671 |
0.0 hash |
672 |
0.5 hash |
|
673 |
0.25 hash |
|
674 |
0.4 hash |
|
675 |
" |
|
5364 | 676 |
! |
677 |
||
678 |
sameFractionValueAs:aNumber |
|
679 |
"return true, if the argument represents the same numeric value |
|
680 |
as the receiver, false otherwise" |
|
681 |
||
682 |
|rSelf rNum| |
|
683 |
||
684 |
rSelf := self reduced. |
|
685 |
rNum := aNumber reduced. |
|
686 |
rSelf denominator = rNum denominator ifTrue:[ |
|
687 |
^ rSelf numerator = rNum numerator |
|
688 |
]. |
|
689 |
^ false |
|
1 | 690 |
! ! |
691 |
||
692 |
!Fraction methodsFor:'double dispatching'! |
|
693 |
||
7355 | 694 |
differenceFromFixedPoint:aFixedPoint |
695 |
|n d otherDenominator otherNumerator| |
|
696 |
||
697 |
otherDenominator := aFixedPoint denominator. |
|
698 |
otherNumerator := aFixedPoint numerator. |
|
699 |
||
700 |
"save a multiplication if possible" |
|
701 |
otherDenominator == denominator ifTrue:[ |
|
702 |
n := otherNumerator - numerator. |
|
703 |
d := otherDenominator. |
|
704 |
] ifFalse:[ |
|
705 |
n := (otherNumerator * denominator) - (numerator * otherDenominator). |
|
706 |
d := otherDenominator * denominator. |
|
707 |
]. |
|
708 |
^ aFixedPoint class |
|
709 |
numerator:n |
|
710 |
denominator:d |
|
711 |
scale:(aFixedPoint scale) |
|
712 |
||
713 |
" |
|
714 |
((1/3) asFixedPoint:2) - (1/3) |
|
715 |
((1/3) asFixedPoint:2) - (2/3) |
|
716 |
" |
|
717 |
! |
|
718 |
||
701 | 719 |
differenceFromFloat:aFloat |
720 |
"sent when a float does not know how to subtract the receiver, a fraction" |
|
1 | 721 |
|
701 | 722 |
^ (aFloat * denominator - numerator) / denominator |
1 | 723 |
! |
724 |
||
7355 | 725 |
differenceFromFraction:aFraction |
726 |
|n d otherDenominator otherNumerator| |
|
727 |
||
728 |
otherDenominator := aFraction denominator. |
|
729 |
otherNumerator := aFraction numerator. |
|
730 |
||
731 |
"save a multiplication if possible" |
|
732 |
otherDenominator == denominator ifTrue:[ |
|
733 |
n := otherNumerator - numerator. |
|
734 |
d := otherDenominator. |
|
735 |
] ifFalse:[ |
|
736 |
n := (otherNumerator * denominator) - (numerator * otherDenominator). |
|
737 |
d := otherDenominator * denominator. |
|
738 |
]. |
|
739 |
^ aFraction class |
|
740 |
numerator:n |
|
741 |
denominator:d |
|
742 |
||
743 |
" |
|
744 |
((1/3) asFixedPoint:2) - (1/3) |
|
745 |
((1/3) asFixedPoint:2) - (2/3) |
|
746 |
" |
|
747 |
! |
|
748 |
||
1 | 749 |
differenceFromInteger:anInteger |
213 | 750 |
"sent when an integer does not know how to subtract the receiver, a fraction" |
1 | 751 |
|
7355 | 752 |
^ self class |
5364 | 753 |
numerator:((anInteger * denominator) - numerator) |
7355 | 754 |
denominator:denominator |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
755 |
|
2792 | 756 |
"Modified: 28.7.1997 / 19:08:53 / cg" |
1 | 757 |
! |
758 |
||
7355 | 759 |
equalFromFraction:aFraction |
7571
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
760 |
denominator = aFraction denominator ifFalse:[ |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
761 |
^ false " must always be reduced " |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
762 |
"/ ^ (numerator * aFraction denominator) = (aFraction numerator * denominator) |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
763 |
]. |
7355 | 764 |
^ numerator = aFraction numerator |
765 |
! |
|
766 |
||
767 |
equalFromInteger:anInteger |
|
768 |
"sent when an integer does not know how to compare to the receiver, a fraction" |
|
769 |
||
770 |
"as I am always reduced, this test should not be required. |
|
771 |
However, it is here for subclasses and to allow comparing unnormalized fractions, |
|
772 |
which might be encountered internally" |
|
773 |
||
7571
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
774 |
denominator = 1 ifFalse:[ |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
775 |
^ numerator = (anInteger * denominator) |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
776 |
]. |
7355 | 777 |
^ numerator = anInteger |
7571
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
778 |
|
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
779 |
" |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
780 |
1 = (1 asFixedPoint:1) |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
781 |
(1 asFixedPoint:1) = 1 |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
782 |
" |
7355 | 783 |
! |
784 |
||
10486 | 785 |
lessEqFromInteger:anInteger |
786 |
"sent when an integer does not know how to compare to the receiver, a fraction" |
|
787 |
||
788 |
^ (denominator * anInteger) <= numerator |
|
789 |
! |
|
790 |
||
7355 | 791 |
lessFromFraction:aFraction |
792 |
"sent when a fraction does not know how to compare to the receiver" |
|
793 |
||
794 |
|n d| |
|
795 |
||
796 |
d := aFraction denominator. |
|
797 |
n := aFraction numerator. |
|
798 |
||
799 |
"/ save a multiplication if possible |
|
800 |
d == denominator ifTrue:[ |
|
801 |
^ n < numerator |
|
802 |
]. |
|
803 |
^ (denominator * n) < (numerator * d) |
|
804 |
! |
|
805 |
||
701 | 806 |
lessFromInteger:anInteger |
807 |
"sent when an integer does not know how to compare to the receiver, a fraction" |
|
808 |
||
809 |
^ (denominator * anInteger) < numerator |
|
810 |
! |
|
811 |
||
7355 | 812 |
productFromFixedPoint:aFixedPoint |
813 |
^ aFixedPoint class |
|
814 |
numerator:(aFixedPoint numerator * numerator) |
|
815 |
denominator:(aFixedPoint denominator * denominator) |
|
816 |
scale:(aFixedPoint scale) |
|
817 |
||
818 |
" |
|
819 |
((1/3) asFixedPoint:2) * 2 |
|
820 |
((1/3) asFixedPoint:2) * (1/2) |
|
821 |
((1/3) asFixedPoint:2) * (3/2) |
|
822 |
" |
|
823 |
! |
|
824 |
||
701 | 825 |
productFromFloat:aFloat |
826 |
"sent when a float does not know how to multiply the receiver, a fraction" |
|
827 |
||
828 |
^ aFloat * numerator / denominator |
|
829 |
! |
|
830 |
||
7355 | 831 |
productFromFraction:aFraction |
832 |
^ aFraction class |
|
833 |
numerator:(aFraction numerator * numerator) |
|
834 |
denominator:(aFraction denominator * denominator) |
|
835 |
||
836 |
" |
|
837 |
((1/3) asFixedPoint:2) * 2 |
|
838 |
((1/3) asFixedPoint:2) * (1/2) |
|
839 |
((1/3) asFixedPoint:2) * (3/2) |
|
840 |
" |
|
841 |
! |
|
842 |
||
1 | 843 |
productFromInteger:anInteger |
213 | 844 |
"sent when an integer does not know how to multiply the receiver, a fraction" |
1 | 845 |
|
7355 | 846 |
^ self class |
5364 | 847 |
numerator:(anInteger * numerator) |
7355 | 848 |
denominator:denominator |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
849 |
|
2792 | 850 |
"Modified: 28.7.1997 / 19:06:22 / cg" |
1 | 851 |
! |
852 |
||
7355 | 853 |
quotientFromFixedPoint:aFixedPoint |
7442 | 854 |
"Return the quotient of the argument, aFixedPoint and the receiver. |
855 |
Sent when aFixedPoint does not know how to divide by the receiver." |
|
856 |
||
7355 | 857 |
^ aFixedPoint class |
858 |
numerator:(aFixedPoint numerator * denominator) |
|
859 |
denominator:(aFixedPoint denominator * numerator) |
|
860 |
scale:(aFixedPoint scale) |
|
861 |
||
862 |
" |
|
863 |
((1/3) asFixedPoint:2) / 2 |
|
864 |
((1/3) asFixedPoint:2) / (1/2) |
|
865 |
" |
|
866 |
! |
|
867 |
||
701 | 868 |
quotientFromFloat:aFloat |
7442 | 869 |
"Return the quotient of the argument, aFloat and the receiver. |
870 |
Sent when aFloat does not know how to divide by the receiver." |
|
701 | 871 |
|
872 |
^ (aFloat * denominator) / numerator |
|
873 |
! |
|
874 |
||
7355 | 875 |
quotientFromFraction:aFraction |
7442 | 876 |
"Return the quotient of the argument, aFraction and the receiver. |
877 |
Sent when aFraction does not know how to divide by the receiver." |
|
878 |
||
7355 | 879 |
^ aFraction class |
880 |
numerator:(aFraction numerator * denominator) |
|
881 |
denominator:(aFraction denominator * numerator) |
|
882 |
||
883 |
" |
|
884 |
(1/3) / (1/2) |
|
885 |
(1/3) / (3/2) |
|
886 |
" |
|
887 |
! |
|
888 |
||
324 | 889 |
quotientFromInteger:anInteger |
7442 | 890 |
"Return the quotient of the argument, anInteger and the receiver. |
891 |
Sent when anInteger does not know how to divide by the receiver." |
|
324 | 892 |
|
7355 | 893 |
^ self class |
5364 | 894 |
numerator:(anInteger * denominator) |
7355 | 895 |
denominator:numerator |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
896 |
|
2792 | 897 |
"Modified: 28.7.1997 / 19:08:46 / cg" |
324 | 898 |
! |
899 |
||
7355 | 900 |
sumFromFixedPoint:aFixedPoint |
901 |
|n d otherDenominator otherNumerator| |
|
902 |
||
903 |
otherDenominator := aFixedPoint denominator. |
|
904 |
otherNumerator := aFixedPoint numerator. |
|
905 |
||
906 |
"save a multiplication if possible" |
|
907 |
otherDenominator == denominator ifTrue:[ |
|
908 |
n := otherNumerator + numerator. |
|
909 |
d := otherDenominator. |
|
910 |
] ifFalse:[ |
|
911 |
n := (otherNumerator * denominator) + (numerator * otherDenominator). |
|
912 |
d := otherDenominator * denominator. |
|
913 |
]. |
|
914 |
^ aFixedPoint class |
|
915 |
numerator:n |
|
916 |
denominator:d |
|
917 |
scale:(aFixedPoint scale) |
|
918 |
||
919 |
" |
|
920 |
((1/3) asFixedPoint:2) + (1/3) |
|
921 |
((1/3) asFixedPoint:2) + (2/3) |
|
922 |
" |
|
923 |
! |
|
924 |
||
1 | 925 |
sumFromFloat:aFloat |
213 | 926 |
"sent when a float does not know how to add the receiver, a fraction" |
1 | 927 |
|
928 |
^ (aFloat * denominator + numerator) / denominator |
|
929 |
! |
|
930 |
||
7355 | 931 |
sumFromFraction:aFraction |
932 |
|n d otherDenominator otherNumerator| |
|
933 |
||
934 |
otherDenominator := aFraction denominator. |
|
935 |
otherNumerator := aFraction numerator. |
|
936 |
||
937 |
"save a multiplication if possible" |
|
938 |
otherDenominator == denominator ifTrue:[ |
|
939 |
n := otherNumerator + numerator. |
|
940 |
d := otherDenominator. |
|
941 |
] ifFalse:[ |
|
942 |
n := (otherNumerator * denominator) + (numerator * otherDenominator). |
|
943 |
d := otherDenominator * denominator. |
|
944 |
]. |
|
945 |
^ aFraction class |
|
946 |
numerator:n |
|
947 |
denominator:d |
|
948 |
||
949 |
" |
|
950 |
(1/3) + (1/3) |
|
951 |
(1/3) + (2/3) |
|
952 |
" |
|
953 |
! |
|
954 |
||
701 | 955 |
sumFromInteger:anInteger |
956 |
"sent when an integer does not know how to add the receiver, a fraction" |
|
1 | 957 |
|
7355 | 958 |
^ self class |
5364 | 959 |
numerator:(numerator + (anInteger * denominator)) |
7355 | 960 |
denominator:denominator |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
961 |
|
2792 | 962 |
"Modified: 28.7.1997 / 19:08:40 / cg" |
1 | 963 |
! ! |
964 |
||
92 | 965 |
!Fraction methodsFor:'printing & storing'! |
1 | 966 |
|
54 | 967 |
printOn:aStream |
968 |
"append a printed representation of the receiver to the |
|
969 |
argument, aStream" |
|
970 |
||
6675 | 971 |
|t| |
972 |
||
973 |
PrintWholeNumbers == true ifTrue:[ |
|
974 |
"/ experimental: print fractions which are greater than 1 as a sum of |
|
975 |
"/ an integral and the fractional part. They are easier to read this way. |
|
976 |
numerator > denominator ifTrue:[ |
|
977 |
aStream nextPut:$(. |
|
978 |
t := numerator // denominator. |
|
979 |
t printOn:aStream. |
|
980 |
aStream nextPutAll:'+('. |
|
981 |
(numerator - (t*denominator)) printOn:aStream. |
|
982 |
aStream nextPut:$/. |
|
983 |
denominator printOn:aStream. |
|
984 |
aStream nextPutAll:'))'. |
|
985 |
^ self |
|
986 |
]. |
|
987 |
]. |
|
988 |
||
54 | 989 |
aStream nextPut:$(. |
990 |
numerator printOn:aStream. |
|
991 |
aStream nextPut:$/. |
|
992 |
denominator printOn:aStream. |
|
993 |
aStream nextPut:$) |
|
6675 | 994 |
|
995 |
"Modified: / 31.7.2002 / 09:56:41 / cg" |
|
1 | 996 |
! ! |
701 | 997 |
|
998 |
!Fraction methodsFor:'private'! |
|
999 |
||
1000 |
reduced |
|
7139 | 1001 |
"reduce the receiver; divide the numerator and denominator by their |
1002 |
greatest common divisor; if the result is integral, return an Integer. |
|
1003 |
Otherwise, return the normalized receiver. |
|
11250 | 1004 |
CAVEAT: bad name; should be called reduce, as it has a side effect |
1005 |
(i.e. this is destructive wrt. the instance values)." |
|
701 | 1006 |
|
2790 | 1007 |
|gcd den| |
701 | 1008 |
|
2790 | 1009 |
den := denominator. |
1010 |
den < 0 ifTrue:[ |
|
5364 | 1011 |
numerator := numerator negated. |
1012 |
den := denominator := den negated. |
|
2789
e3e8707d26b4
make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents:
2140
diff
changeset
|
1013 |
]. |
e3e8707d26b4
make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents:
2140
diff
changeset
|
1014 |
|
2790 | 1015 |
den == 1 ifTrue:[^ numerator]. |
701 | 1016 |
numerator == 1 ifTrue:[^ self]. |
1017 |
numerator == 0 ifTrue:[^ 0]. |
|
1018 |
||
2790 | 1019 |
gcd := numerator gcd:den. |
1020 |
(gcd ~~ 1) ifTrue:[ |
|
6576 | 1021 |
gcd < 0 ifTrue:[ |
1022 |
gcd := gcd negated. |
|
1023 |
]. |
|
5364 | 1024 |
numerator := numerator // gcd. |
1025 |
denominator := den := den // gcd. |
|
1026 |
(den == 1) ifTrue:[^ numerator]. |
|
701 | 1027 |
]. |
1028 |
^ self |
|
1029 |
! |
|
1030 |
||
1031 |
setNumerator:num denominator:den |
|
1032 |
"set both numerator and denominator" |
|
1033 |
||
1034 |
numerator := num. |
|
1035 |
denominator := den |
|
1036 |
! ! |
|
1037 |
||
6650 | 1038 |
!Fraction methodsFor:'testing'! |
701 | 1039 |
|
1040 |
isFraction |
|
1041 |
"return true, if the receiver is some kind of fraction; |
|
1042 |
true is returned here - the method is redefined from Object." |
|
1043 |
||
1044 |
^ true |
|
6650 | 1045 |
! |
701 | 1046 |
|
4658
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1047 |
isLiteral |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1048 |
"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
|
1049 |
(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
|
1050 |
|
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1051 |
^ true |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1052 |
|
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1053 |
! |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1054 |
|
701 | 1055 |
negative |
1056 |
"return true if the receiver is negative" |
|
1057 |
||
1058 |
(numerator < 0) ifTrue:[ |
|
5364 | 1059 |
^ (denominator < 0) not |
701 | 1060 |
]. |
1061 |
^ (denominator < 0) |
|
1062 |
! ! |
|
1063 |
||
5552 | 1064 |
!Fraction methodsFor:'truncation & rounding'! |
701 | 1065 |
|
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1066 |
fractionPart |
7355 | 1067 |
"extract the after-decimal fraction part, |
1068 |
such that (self truncated + self fractionPart) = self" |
|
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1069 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1070 |
numerator abs < denominator abs ifTrue:[ |
5364 | 1071 |
^ self |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1072 |
]. |
7355 | 1073 |
^ (numerator rem: denominator) / denominator |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1074 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1075 |
" |
7355 | 1076 |
(3/2) fractionPart + (3/2) truncated |
1077 |
(-3/2) fractionPart + (-3/2) truncated |
|
1078 |
||
1079 |
(3/2) fractionPart |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1080 |
(-3/2) fractionPart |
7355 | 1081 |
(3/2) asFloat fractionPart |
1082 |
(-3/2) asFloat fractionPart |
|
1083 |
(2/3) fractionPart |
|
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1084 |
((3/2)*(15/4)) fractionPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1085 |
((2/3)*(4/15)) fractionPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1086 |
" |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1087 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1088 |
"Modified: / 5.11.2001 / 17:55:25 / cg" |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1089 |
! |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1090 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1091 |
integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1092 |
"extract the pre-decimal integer part." |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1093 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1094 |
numerator abs < denominator abs ifTrue:[ |
5364 | 1095 |
^ 0 |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1096 |
]. |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1097 |
^ super integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1098 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1099 |
" |
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1100 |
(3/2) integerPart |
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1101 |
(-3/2) integerPart |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1102 |
(2/3) integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1103 |
((3/2)*(15/4)) integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1104 |
((2/3)*(4/15)) integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1105 |
" |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1106 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1107 |
"Modified: / 5.11.2001 / 17:55:01 / cg" |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1108 |
! |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1109 |
|
701 | 1110 |
rounded |
1111 |
"return the receiver rounded to the nearest integer as integer" |
|
1112 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1113 |
"/ mhmh - what about -(1/2) |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1114 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1115 |
|t| |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1116 |
|
701 | 1117 |
self negative ifTrue:[ |
5364 | 1118 |
t := self - (1/2) |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1119 |
] ifFalse:[ |
5364 | 1120 |
t := self + (1/2) |
701 | 1121 |
]. |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1122 |
^ t truncated. |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1123 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1124 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1125 |
(1/3) rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1126 |
(1/3) negated rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1127 |
(1/2) rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1128 |
(1/2) negated rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1129 |
0.5 rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1130 |
-0.5 rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1131 |
(2/3) rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1132 |
(2/3) negated rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1133 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1134 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1135 |
"Modified: 5.11.1996 / 11:32:32 / cg" |
701 | 1136 |
! |
1137 |
||
1138 |
truncated |
|
1139 |
"return the receiver truncated towards zero as Integer" |
|
1140 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1141 |
^ numerator quo: denominator |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1142 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1143 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1144 |
(3/2) truncated |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1145 |
(3/2) negated truncated |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1146 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1147 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1148 |
"Modified: 5.11.1996 / 12:18:46 / cg" |
701 | 1149 |
! ! |
1150 |
||
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1151 |
!Fraction methodsFor:'visiting'! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1152 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1153 |
acceptVisitor:aVisitor with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1154 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1155 |
^ aVisitor visitFraction:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1156 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1157 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1158 |
!Fraction class methodsFor:'documentation'! |
701 | 1159 |
|
1160 |
version |
|
17892 | 1161 |
^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.81 2011/05/03 09:08:52 cg Exp $' |
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
1162 |
! |
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
1163 |
|
17841 | 1164 |
version_CVS |
17845 | 1165 |
^ '§Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.81 2011/05/03 09:08:52 cg Exp §' |
17865 | 1166 |
! |
1167 |
||
1168 |
version_SVN |
|
17892 | 1169 |
^ '$Id: Fraction.st 10729 2011-10-31 22:19:21Z vranyj1 $' |
701 | 1170 |
! ! |
6891
212aa8576cf7
oops - did not compare correctly against unnormalized numbers
Claus Gittinger <cg@exept.de>
parents:
6675
diff
changeset
|
1171 |
|
17846 | 1172 |
Fraction initialize! |
17892 | 1173 |