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