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