author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Fri, 31 May 2013 00:35:21 +0100 | |
branch | jv |
changeset 18062 | 014678b4657a |
parent 18011 | deb0c3355881 |
parent 15278 | ddf05cd996da |
child 18120 | e3a375d5f6a8 |
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 |
" |
11899 | 134 |
! |
135 |
||
136 |
readFrom:aStringOrStream onError:exceptionBlock |
|
137 |
|s numerator denominator| |
|
138 |
||
11901 | 139 |
"/ sigh - care for subclasses... |
140 |
self == Fraction ifFalse:[ |
|
141 |
^ super readFrom:aStringOrStream onError:exceptionBlock |
|
142 |
]. |
|
143 |
||
11899 | 144 |
s := aStringOrStream readStream. |
145 |
s skipSeparators. |
|
146 |
s peek == $( ifTrue:[ |
|
147 |
s next. |
|
148 |
||
149 |
]. |
|
150 |
||
151 |
numerator := super readFrom:s onError:[^ exceptionBlock value]. |
|
152 |
numerator isInteger ifTrue:[ |
|
153 |
s skipSeparators. |
|
154 |
(s peek == $/) ifTrue:[ |
|
155 |
s next. |
|
156 |
denominator := Integer readFrom:s onError:[^ exceptionBlock value]. |
|
157 |
^ self numerator:numerator denominator:denominator |
|
158 |
]. |
|
159 |
^ numerator |
|
160 |
]. |
|
161 |
^ numerator asFraction |
|
162 |
||
163 |
" |
|
164 |
Fraction readFrom:'1' |
|
165 |
Fraction readFrom:'2' |
|
166 |
Fraction readFrom:'1.5' |
|
167 |
Fraction readFrom:'1/5' |
|
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 |
13357 | 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 |
|
13357 | 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 |
15278 | 529 |
"return a long float with (approximately) my value. |
530 |
Since floats have a limited precision, you usually loose bits when doing this." |
|
7355 | 531 |
|
7442 | 532 |
|num den numShift denShift numBits rslt| |
7355 | 533 |
|
534 |
(numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[ |
|
535 |
^ (numerator asLongFloat) / (denominator asLongFloat) |
|
536 |
]. |
|
537 |
||
538 |
"Do it the hard way: reduce magnitude and undo reduction on the quotient" |
|
539 |
||
7442 | 540 |
numBits := LongFloat precision * 2. "number of bits to preserve (conservative)" |
7355 | 541 |
num := numerator abs. |
7442 | 542 |
numShift := numBits - num highBit. "(num highBit - bits) negated" |
15278 | 543 |
numShift < 0 ifTrue:[num := num bitShift:numShift] ifFalse:[ numShift := 0]. |
7355 | 544 |
|
545 |
den := denominator. |
|
7442 | 546 |
denShift := numBits - den highBit. "(den highBit - bits) negated" |
15278 | 547 |
denShift < 0 ifTrue:[den := den bitShift:denShift] ifFalse:[denShift := 0]. |
7355 | 548 |
|
7730
dda82553acc7
make #asLongFloat return a LongFloat (returned Float)
Stefan Vogel <sv@exept.de>
parents:
7571
diff
changeset
|
549 |
rslt := (num asLongFloat / den asLongFloat) * (2 raisedToInteger:denShift-numShift). |
7442 | 550 |
numerator negative ifTrue:[ ^ rslt negated ]. |
551 |
^ rslt. |
|
7355 | 552 |
|
553 |
" |
|
7442 | 554 |
(5/9) asLongFloat |
555 |
(-5/9) asLongFloat |
|
556 |
(Fraction basicNew setNumerator:500000000000 denominator:900000000000) asLongFloat = (5/9) asLongFloat |
|
557 |
(Fraction basicNew setNumerator:500000000001 denominator:900000000000) asLongFloat = (5/9) asLongFloat |
|
558 |
(500000000001/900000000000) asLongFloat |
|
559 |
(-500000000001/900000000000) asLongFloat |
|
560 |
(500000000001/900000000000) asLongFloat = (5/9) asLongFloat |
|
561 |
||
562 |
(500000000000/9) asLongFloat |
|
563 |
(5/900000000000) asLongFloat |
|
564 |
89012345678901234567 asFloat / 123456789123456789 asLongFloat |
|
565 |
(89012345678901234567 / 123456789123456789) asLongFloat |
|
566 |
(-89012345678901234567 / 123456789123456789) asLongFloat |
|
7355 | 567 |
|
568 |
( |
|
569 |
180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125 |
|
570 |
/ |
|
571 |
180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250 |
|
7442 | 572 |
) asLongFloat |
7355 | 573 |
|
574 |
180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125 |
|
575 |
asLongFloat / |
|
576 |
180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250 |
|
577 |
asLongFloat |
|
578 |
" |
|
579 |
! |
|
580 |
||
1200 | 581 |
asShortFloat |
6576 | 582 |
"return a short float with (approximately) my value" |
583 |
||
584 |
(numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[ |
|
585 |
^ (numerator asShortFloat) / (denominator asShortFloat) |
|
586 |
]. |
|
1200 | 587 |
|
6576 | 588 |
^ self asFloat asShortFloat |
1200 | 589 |
|
6576 | 590 |
" |
591 |
(5/9) asShortFloat |
|
592 |
(500000000000/900000000000) asShortFloat |
|
593 |
(500000000000/9) asShortFloat |
|
594 |
" |
|
1200 | 595 |
! |
596 |
||
701 | 597 |
coerce:aNumber |
11722 | 598 |
"convert the argument aNumber into an instance of the receivers class and return it." |
701 | 599 |
|
600 |
^ aNumber asFraction |
|
601 |
! |
|
602 |
||
603 |
generality |
|
604 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
605 |
||
606 |
^ 60 |
|
607 |
! ! |
|
608 |
||
609 |
!Fraction methodsFor:'comparing'! |
|
610 |
||
611 |
< aNumber |
|
612 |
"return true if the receiver is less |
|
613 |
than aNumber, false otherwise." |
|
1 | 614 |
|
701 | 615 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
5364 | 616 |
^ numerator < (denominator * aNumber) |
701 | 617 |
]. |
618 |
^ aNumber lessFromFraction:self |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
619 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
620 |
"Modified: 5.11.1996 / 10:30:52 / cg" |
701 | 621 |
! |
622 |
||
623 |
= aNumber |
|
624 |
"return true, if the argument represents the same numeric value |
|
625 |
as the receiver, false otherwise" |
|
626 |
||
627 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
|
6891
212aa8576cf7
oops - did not compare correctly against unnormalized numbers
Claus Gittinger <cg@exept.de>
parents:
6675
diff
changeset
|
628 |
(denominator == 1) ifFalse:[ |
212aa8576cf7
oops - did not compare correctly against unnormalized numbers
Claus Gittinger <cg@exept.de>
parents:
6675
diff
changeset
|
629 |
^ numerator = (aNumber * denominator) |
212aa8576cf7
oops - did not compare correctly against unnormalized numbers
Claus Gittinger <cg@exept.de>
parents:
6675
diff
changeset
|
630 |
]. |
5364 | 631 |
^ numerator = aNumber |
1 | 632 |
]. |
7355 | 633 |
^ aNumber equalFromFraction:self |
3631
26a44b284912
handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
634 |
|
26a44b284912
handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
635 |
"Modified: / 7.7.1998 / 17:17:07 / cg" |
701 | 636 |
! |
637 |
||
638 |
> aNumber |
|
639 |
"return true if the receiver is greater |
|
640 |
than aNumber, false otherwise." |
|
641 |
"optional - could use inherited method ..." |
|
642 |
||
643 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
|
5364 | 644 |
^ numerator > (denominator * aNumber) |
701 | 645 |
]. |
7355 | 646 |
^ aNumber < self |
4593 | 647 |
! |
648 |
||
649 |
hash |
|
650 |
"return a number for hashing; redefined, since fractions compare |
|
651 |
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
|
652 |
as 3 hash." |
4593 | 653 |
|
654 |
(denominator = 1) ifTrue:[^ numerator hash]. |
|
655 |
||
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
656 |
^ self asFloat hash |
4593 | 657 |
|
658 |
" |
|
659 |
3 hash |
|
660 |
(9/3) hash |
|
661 |
3.0 hash |
|
662 |
(1/2) hash |
|
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
663 |
(1/4) hash |
4593 | 664 |
0.0 hash |
665 |
0.5 hash |
|
666 |
0.25 hash |
|
667 |
0.4 hash |
|
668 |
" |
|
5364 | 669 |
! |
670 |
||
671 |
sameFractionValueAs:aNumber |
|
672 |
"return true, if the argument represents the same numeric value |
|
673 |
as the receiver, false otherwise" |
|
674 |
||
675 |
|rSelf rNum| |
|
676 |
||
677 |
rSelf := self reduced. |
|
678 |
rNum := aNumber reduced. |
|
679 |
rSelf denominator = rNum denominator ifTrue:[ |
|
680 |
^ rSelf numerator = rNum numerator |
|
681 |
]. |
|
682 |
^ false |
|
1 | 683 |
! ! |
684 |
||
685 |
!Fraction methodsFor:'double dispatching'! |
|
686 |
||
7355 | 687 |
differenceFromFixedPoint:aFixedPoint |
688 |
|n d otherDenominator otherNumerator| |
|
689 |
||
690 |
otherDenominator := aFixedPoint denominator. |
|
691 |
otherNumerator := aFixedPoint numerator. |
|
692 |
||
693 |
"save a multiplication if possible" |
|
694 |
otherDenominator == denominator ifTrue:[ |
|
695 |
n := otherNumerator - numerator. |
|
696 |
d := otherDenominator. |
|
697 |
] ifFalse:[ |
|
698 |
n := (otherNumerator * denominator) - (numerator * otherDenominator). |
|
699 |
d := otherDenominator * denominator. |
|
700 |
]. |
|
701 |
^ aFixedPoint class |
|
702 |
numerator:n |
|
703 |
denominator:d |
|
704 |
scale:(aFixedPoint scale) |
|
705 |
||
706 |
" |
|
707 |
((1/3) asFixedPoint:2) - (1/3) |
|
708 |
((1/3) asFixedPoint:2) - (2/3) |
|
709 |
" |
|
710 |
! |
|
711 |
||
701 | 712 |
differenceFromFloat:aFloat |
713 |
"sent when a float does not know how to subtract the receiver, a fraction" |
|
1 | 714 |
|
701 | 715 |
^ (aFloat * denominator - numerator) / denominator |
1 | 716 |
! |
717 |
||
7355 | 718 |
differenceFromFraction:aFraction |
719 |
|n d otherDenominator otherNumerator| |
|
720 |
||
721 |
otherDenominator := aFraction denominator. |
|
722 |
otherNumerator := aFraction numerator. |
|
723 |
||
724 |
"save a multiplication if possible" |
|
725 |
otherDenominator == denominator ifTrue:[ |
|
726 |
n := otherNumerator - numerator. |
|
727 |
d := otherDenominator. |
|
728 |
] ifFalse:[ |
|
729 |
n := (otherNumerator * denominator) - (numerator * otherDenominator). |
|
730 |
d := otherDenominator * denominator. |
|
731 |
]. |
|
732 |
^ aFraction class |
|
733 |
numerator:n |
|
734 |
denominator:d |
|
735 |
||
736 |
" |
|
737 |
((1/3) asFixedPoint:2) - (1/3) |
|
738 |
((1/3) asFixedPoint:2) - (2/3) |
|
739 |
" |
|
740 |
! |
|
741 |
||
1 | 742 |
differenceFromInteger:anInteger |
213 | 743 |
"sent when an integer does not know how to subtract the receiver, a fraction" |
1 | 744 |
|
7355 | 745 |
^ self class |
5364 | 746 |
numerator:((anInteger * denominator) - numerator) |
7355 | 747 |
denominator:denominator |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
748 |
|
2792 | 749 |
"Modified: 28.7.1997 / 19:08:53 / cg" |
1 | 750 |
! |
751 |
||
7355 | 752 |
equalFromFraction:aFraction |
7571
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
753 |
denominator = aFraction denominator ifFalse:[ |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
754 |
^ false " must always be reduced " |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
755 |
"/ ^ (numerator * aFraction denominator) = (aFraction numerator * denominator) |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
756 |
]. |
7355 | 757 |
^ numerator = aFraction numerator |
758 |
! |
|
759 |
||
760 |
equalFromInteger:anInteger |
|
761 |
"sent when an integer does not know how to compare to the receiver, a fraction" |
|
762 |
||
763 |
"as I am always reduced, this test should not be required. |
|
764 |
However, it is here for subclasses and to allow comparing unnormalized fractions, |
|
765 |
which might be encountered internally" |
|
766 |
||
7571
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
767 |
denominator = 1 ifFalse:[ |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
768 |
^ numerator = (anInteger * denominator) |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
769 |
]. |
7355 | 770 |
^ numerator = anInteger |
7571
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
771 |
|
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
772 |
" |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
773 |
1 = (1 asFixedPoint:1) |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
774 |
(1 asFixedPoint:1) = 1 |
57d86ad2aded
fixed comparing agains integer for subclasses
Claus Gittinger <cg@exept.de>
parents:
7442
diff
changeset
|
775 |
" |
7355 | 776 |
! |
777 |
||
10486 | 778 |
lessEqFromInteger:anInteger |
779 |
"sent when an integer does not know how to compare to the receiver, a fraction" |
|
780 |
||
781 |
^ (denominator * anInteger) <= numerator |
|
782 |
! |
|
783 |
||
7355 | 784 |
lessFromFraction:aFraction |
785 |
"sent when a fraction does not know how to compare to the receiver" |
|
786 |
||
787 |
|n d| |
|
788 |
||
789 |
d := aFraction denominator. |
|
790 |
n := aFraction numerator. |
|
791 |
||
792 |
"/ save a multiplication if possible |
|
793 |
d == denominator ifTrue:[ |
|
794 |
^ n < numerator |
|
795 |
]. |
|
796 |
^ (denominator * n) < (numerator * d) |
|
797 |
! |
|
798 |
||
701 | 799 |
lessFromInteger:anInteger |
800 |
"sent when an integer does not know how to compare to the receiver, a fraction" |
|
801 |
||
802 |
^ (denominator * anInteger) < numerator |
|
803 |
! |
|
804 |
||
7355 | 805 |
productFromFixedPoint:aFixedPoint |
806 |
^ aFixedPoint class |
|
807 |
numerator:(aFixedPoint numerator * numerator) |
|
808 |
denominator:(aFixedPoint denominator * denominator) |
|
809 |
scale:(aFixedPoint scale) |
|
810 |
||
811 |
" |
|
812 |
((1/3) asFixedPoint:2) * 2 |
|
813 |
((1/3) asFixedPoint:2) * (1/2) |
|
814 |
((1/3) asFixedPoint:2) * (3/2) |
|
815 |
" |
|
816 |
! |
|
817 |
||
701 | 818 |
productFromFloat:aFloat |
819 |
"sent when a float does not know how to multiply the receiver, a fraction" |
|
820 |
||
821 |
^ aFloat * numerator / denominator |
|
822 |
! |
|
823 |
||
7355 | 824 |
productFromFraction:aFraction |
825 |
^ aFraction class |
|
826 |
numerator:(aFraction numerator * numerator) |
|
827 |
denominator:(aFraction denominator * denominator) |
|
828 |
||
829 |
" |
|
830 |
((1/3) asFixedPoint:2) * 2 |
|
831 |
((1/3) asFixedPoint:2) * (1/2) |
|
832 |
((1/3) asFixedPoint:2) * (3/2) |
|
833 |
" |
|
834 |
! |
|
835 |
||
1 | 836 |
productFromInteger:anInteger |
213 | 837 |
"sent when an integer does not know how to multiply the receiver, a fraction" |
1 | 838 |
|
7355 | 839 |
^ self class |
5364 | 840 |
numerator:(anInteger * numerator) |
7355 | 841 |
denominator:denominator |
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:06:22 / cg" |
1 | 844 |
! |
845 |
||
7355 | 846 |
quotientFromFixedPoint:aFixedPoint |
7442 | 847 |
"Return the quotient of the argument, aFixedPoint and the receiver. |
848 |
Sent when aFixedPoint does not know how to divide by the receiver." |
|
849 |
||
7355 | 850 |
^ aFixedPoint class |
851 |
numerator:(aFixedPoint numerator * denominator) |
|
852 |
denominator:(aFixedPoint denominator * numerator) |
|
853 |
scale:(aFixedPoint scale) |
|
854 |
||
855 |
" |
|
856 |
((1/3) asFixedPoint:2) / 2 |
|
857 |
((1/3) asFixedPoint:2) / (1/2) |
|
858 |
" |
|
859 |
! |
|
860 |
||
701 | 861 |
quotientFromFloat:aFloat |
7442 | 862 |
"Return the quotient of the argument, aFloat and the receiver. |
863 |
Sent when aFloat does not know how to divide by the receiver." |
|
701 | 864 |
|
865 |
^ (aFloat * denominator) / numerator |
|
866 |
! |
|
867 |
||
7355 | 868 |
quotientFromFraction:aFraction |
7442 | 869 |
"Return the quotient of the argument, aFraction and the receiver. |
870 |
Sent when aFraction does not know how to divide by the receiver." |
|
871 |
||
7355 | 872 |
^ aFraction class |
873 |
numerator:(aFraction numerator * denominator) |
|
874 |
denominator:(aFraction denominator * numerator) |
|
875 |
||
876 |
" |
|
877 |
(1/3) / (1/2) |
|
878 |
(1/3) / (3/2) |
|
879 |
" |
|
880 |
! |
|
881 |
||
324 | 882 |
quotientFromInteger:anInteger |
7442 | 883 |
"Return the quotient of the argument, anInteger and the receiver. |
884 |
Sent when anInteger does not know how to divide by the receiver." |
|
324 | 885 |
|
7355 | 886 |
^ self class |
5364 | 887 |
numerator:(anInteger * denominator) |
7355 | 888 |
denominator:numerator |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
889 |
|
2792 | 890 |
"Modified: 28.7.1997 / 19:08:46 / cg" |
324 | 891 |
! |
892 |
||
7355 | 893 |
sumFromFixedPoint:aFixedPoint |
894 |
|n d otherDenominator otherNumerator| |
|
895 |
||
896 |
otherDenominator := aFixedPoint denominator. |
|
897 |
otherNumerator := aFixedPoint numerator. |
|
898 |
||
899 |
"save a multiplication if possible" |
|
900 |
otherDenominator == denominator ifTrue:[ |
|
901 |
n := otherNumerator + numerator. |
|
902 |
d := otherDenominator. |
|
903 |
] ifFalse:[ |
|
904 |
n := (otherNumerator * denominator) + (numerator * otherDenominator). |
|
905 |
d := otherDenominator * denominator. |
|
906 |
]. |
|
907 |
^ aFixedPoint class |
|
908 |
numerator:n |
|
909 |
denominator:d |
|
910 |
scale:(aFixedPoint scale) |
|
911 |
||
912 |
" |
|
913 |
((1/3) asFixedPoint:2) + (1/3) |
|
914 |
((1/3) asFixedPoint:2) + (2/3) |
|
915 |
" |
|
916 |
! |
|
917 |
||
1 | 918 |
sumFromFloat:aFloat |
213 | 919 |
"sent when a float does not know how to add the receiver, a fraction" |
1 | 920 |
|
921 |
^ (aFloat * denominator + numerator) / denominator |
|
922 |
! |
|
923 |
||
7355 | 924 |
sumFromFraction:aFraction |
925 |
|n d otherDenominator otherNumerator| |
|
926 |
||
927 |
otherDenominator := aFraction denominator. |
|
928 |
otherNumerator := aFraction numerator. |
|
929 |
||
930 |
"save a multiplication if possible" |
|
931 |
otherDenominator == denominator ifTrue:[ |
|
932 |
n := otherNumerator + numerator. |
|
933 |
d := otherDenominator. |
|
934 |
] ifFalse:[ |
|
935 |
n := (otherNumerator * denominator) + (numerator * otherDenominator). |
|
936 |
d := otherDenominator * denominator. |
|
937 |
]. |
|
938 |
^ aFraction class |
|
939 |
numerator:n |
|
940 |
denominator:d |
|
941 |
||
942 |
" |
|
943 |
(1/3) + (1/3) |
|
944 |
(1/3) + (2/3) |
|
945 |
" |
|
946 |
! |
|
947 |
||
701 | 948 |
sumFromInteger:anInteger |
949 |
"sent when an integer does not know how to add the receiver, a fraction" |
|
1 | 950 |
|
7355 | 951 |
^ self class |
5364 | 952 |
numerator:(numerator + (anInteger * denominator)) |
7355 | 953 |
denominator:denominator |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
954 |
|
2792 | 955 |
"Modified: 28.7.1997 / 19:08:40 / cg" |
1 | 956 |
! ! |
957 |
||
92 | 958 |
!Fraction methodsFor:'printing & storing'! |
1 | 959 |
|
54 | 960 |
printOn:aStream |
961 |
"append a printed representation of the receiver to the |
|
962 |
argument, aStream" |
|
963 |
||
6675 | 964 |
|t| |
965 |
||
966 |
PrintWholeNumbers == true ifTrue:[ |
|
967 |
"/ experimental: print fractions which are greater than 1 as a sum of |
|
968 |
"/ an integral and the fractional part. They are easier to read this way. |
|
969 |
numerator > denominator ifTrue:[ |
|
970 |
aStream nextPut:$(. |
|
971 |
t := numerator // denominator. |
|
972 |
t printOn:aStream. |
|
973 |
aStream nextPutAll:'+('. |
|
974 |
(numerator - (t*denominator)) printOn:aStream. |
|
975 |
aStream nextPut:$/. |
|
976 |
denominator printOn:aStream. |
|
977 |
aStream nextPutAll:'))'. |
|
978 |
^ self |
|
979 |
]. |
|
980 |
]. |
|
981 |
||
54 | 982 |
aStream nextPut:$(. |
983 |
numerator printOn:aStream. |
|
984 |
aStream nextPut:$/. |
|
985 |
denominator printOn:aStream. |
|
986 |
aStream nextPut:$) |
|
6675 | 987 |
|
988 |
"Modified: / 31.7.2002 / 09:56:41 / cg" |
|
1 | 989 |
! ! |
701 | 990 |
|
991 |
!Fraction methodsFor:'private'! |
|
992 |
||
993 |
reduced |
|
7139 | 994 |
"reduce the receiver; divide the numerator and denominator by their |
995 |
greatest common divisor; if the result is integral, return an Integer. |
|
996 |
Otherwise, return the normalized receiver. |
|
11250 | 997 |
CAVEAT: bad name; should be called reduce, as it has a side effect |
998 |
(i.e. this is destructive wrt. the instance values)." |
|
701 | 999 |
|
2790 | 1000 |
|gcd den| |
701 | 1001 |
|
2790 | 1002 |
den := denominator. |
1003 |
den < 0 ifTrue:[ |
|
5364 | 1004 |
numerator := numerator negated. |
1005 |
den := denominator := den negated. |
|
2789
e3e8707d26b4
make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents:
2140
diff
changeset
|
1006 |
]. |
e3e8707d26b4
make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents:
2140
diff
changeset
|
1007 |
|
2790 | 1008 |
den == 1 ifTrue:[^ numerator]. |
701 | 1009 |
numerator == 1 ifTrue:[^ self]. |
1010 |
numerator == 0 ifTrue:[^ 0]. |
|
1011 |
||
2790 | 1012 |
gcd := numerator gcd:den. |
1013 |
(gcd ~~ 1) ifTrue:[ |
|
6576 | 1014 |
gcd < 0 ifTrue:[ |
1015 |
gcd := gcd negated. |
|
1016 |
]. |
|
5364 | 1017 |
numerator := numerator // gcd. |
1018 |
denominator := den := den // gcd. |
|
1019 |
(den == 1) ifTrue:[^ numerator]. |
|
701 | 1020 |
]. |
1021 |
^ self |
|
1022 |
! |
|
1023 |
||
1024 |
setNumerator:num denominator:den |
|
1025 |
"set both numerator and denominator" |
|
1026 |
||
1027 |
numerator := num. |
|
1028 |
denominator := den |
|
1029 |
! ! |
|
1030 |
||
6650 | 1031 |
!Fraction methodsFor:'testing'! |
701 | 1032 |
|
1033 |
isFraction |
|
1034 |
"return true, if the receiver is some kind of fraction; |
|
1035 |
true is returned here - the method is redefined from Object." |
|
1036 |
||
1037 |
^ true |
|
6650 | 1038 |
! |
701 | 1039 |
|
4658
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1040 |
isLiteral |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1041 |
"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
|
1042 |
(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
|
1043 |
|
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1044 |
^ true |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1045 |
|
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1046 |
! |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1047 |
|
701 | 1048 |
negative |
1049 |
"return true if the receiver is negative" |
|
1050 |
||
1051 |
(numerator < 0) ifTrue:[ |
|
5364 | 1052 |
^ (denominator < 0) not |
701 | 1053 |
]. |
1054 |
^ (denominator < 0) |
|
1055 |
! ! |
|
1056 |
||
5552 | 1057 |
!Fraction methodsFor:'truncation & rounding'! |
701 | 1058 |
|
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1059 |
fractionPart |
7355 | 1060 |
"extract the after-decimal fraction part, |
1061 |
such that (self truncated + self fractionPart) = self" |
|
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1062 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1063 |
numerator abs < denominator abs ifTrue:[ |
5364 | 1064 |
^ self |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1065 |
]. |
7355 | 1066 |
^ (numerator rem: denominator) / denominator |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1067 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1068 |
" |
7355 | 1069 |
(3/2) fractionPart + (3/2) truncated |
1070 |
(-3/2) fractionPart + (-3/2) truncated |
|
1071 |
||
1072 |
(3/2) fractionPart |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1073 |
(-3/2) fractionPart |
7355 | 1074 |
(3/2) asFloat fractionPart |
1075 |
(-3/2) asFloat fractionPart |
|
1076 |
(2/3) fractionPart |
|
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1077 |
((3/2)*(15/4)) fractionPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1078 |
((2/3)*(4/15)) fractionPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1079 |
" |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1080 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1081 |
"Modified: / 5.11.2001 / 17:55:25 / cg" |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1082 |
! |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1083 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1084 |
integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1085 |
"extract the pre-decimal integer part." |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1086 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1087 |
numerator abs < denominator abs ifTrue:[ |
5364 | 1088 |
^ 0 |
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 |
^ super integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1091 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1092 |
" |
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1093 |
(3/2) integerPart |
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
1094 |
(-3/2) integerPart |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1095 |
(2/3) integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1096 |
((3/2)*(15/4)) integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1097 |
((2/3)*(4/15)) 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 |
"Modified: / 5.11.2001 / 17:55:01 / cg" |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1101 |
! |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
1102 |
|
701 | 1103 |
rounded |
1104 |
"return the receiver rounded to the nearest integer as integer" |
|
1105 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1106 |
"/ mhmh - what about -(1/2) |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1107 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1108 |
|t| |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1109 |
|
701 | 1110 |
self negative ifTrue:[ |
5364 | 1111 |
t := self - (1/2) |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1112 |
] ifFalse:[ |
5364 | 1113 |
t := self + (1/2) |
701 | 1114 |
]. |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1115 |
^ t truncated. |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1116 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1117 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1118 |
(1/3) rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1119 |
(1/3) negated rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1120 |
(1/2) rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1121 |
(1/2) negated rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1122 |
0.5 rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1123 |
-0.5 rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1124 |
(2/3) rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1125 |
(2/3) negated rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1126 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1127 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1128 |
"Modified: 5.11.1996 / 11:32:32 / cg" |
701 | 1129 |
! |
1130 |
||
1131 |
truncated |
|
1132 |
"return the receiver truncated towards zero as Integer" |
|
1133 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1134 |
^ numerator quo: denominator |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1135 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1136 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1137 |
(3/2) truncated |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1138 |
(3/2) negated truncated |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1139 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1140 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1141 |
"Modified: 5.11.1996 / 12:18:46 / cg" |
701 | 1142 |
! ! |
1143 |
||
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1144 |
!Fraction methodsFor:'visiting'! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1145 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1146 |
acceptVisitor:aVisitor with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1147 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1148 |
^ aVisitor visitFraction:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1149 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
7859
diff
changeset
|
1150 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1151 |
!Fraction class methodsFor:'documentation'! |
701 | 1152 |
|
1153 |
version |
|
15278 | 1154 |
^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.82 2013-05-27 08:14:32 cg Exp $' |
13357 | 1155 |
! |
1156 |
||
1157 |
version_CVS |
|
15278 | 1158 |
^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.82 2013-05-27 08:14:32 cg Exp $' |
701 | 1159 |
! ! |
6891
212aa8576cf7
oops - did not compare correctly against unnormalized numbers
Claus Gittinger <cg@exept.de>
parents:
6675
diff
changeset
|
1160 |
|
15278 | 1161 |
|
701 | 1162 |
Fraction initialize! |