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