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