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