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