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