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