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