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