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