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