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