author | Claus Gittinger <cg@exept.de> |
Wed, 24 Jul 2002 10:32:32 +0200 | |
changeset 6657 | a0fa5a821176 |
parent 6650 | 35de1d8400b2 |
child 6675 | c7e2757d5e80 |
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' |
17 |
classVariableNames:'FractionOne FractionZero' |
|
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 |
|
324 | 42 |
integers. Fractions are usually created by dividing Integers using / |
1295 | 43 |
(for exact division). |
44 |
||
1892 | 45 |
Mixed mode arithmetic: |
5364 | 46 |
fraction op fraction -> fraction |
47 |
fraction op fix -> fix; scale is fix's scale |
|
48 |
fraction op integer -> fraction |
|
49 |
fraction op float -> float |
|
1892 | 50 |
|
1295 | 51 |
[author:] |
5364 | 52 |
Claus Gittinger |
1556 | 53 |
|
54 |
[see also:] |
|
5364 | 55 |
Number |
56 |
Float Integer FixedPoint |
|
1295 | 57 |
" |
88 | 58 |
! ! |
1 | 59 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
60 |
!Fraction class methodsFor:'initialization'! |
1 | 61 |
|
62 |
initialize |
|
302 | 63 |
FractionZero isNil ifTrue:[ |
5364 | 64 |
FractionZero := self numerator:0 denominator:1. |
65 |
FractionOne := self numerator:1 denominator:1 |
|
302 | 66 |
] |
1 | 67 |
! ! |
68 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
69 |
!Fraction class methodsFor:'instance creation'! |
1 | 70 |
|
71 |
new |
|
72 |
"create and return a new fraction with value 0" |
|
73 |
||
54 | 74 |
^ self numerator:0 denominator:1 |
1 | 75 |
! |
76 |
||
77 |
numerator:num denominator:den |
|
78 |
"create and return a new fraction with numerator num and denominator den" |
|
79 |
||
2792 | 80 |
|newFraction| |
81 |
||
1 | 82 |
%{ /* NOCONTEXT */ |
369 | 83 |
|
13 | 84 |
/* this check allows subclassing .. */ |
1 | 85 |
if (self == Fraction) { |
5364 | 86 |
if (__bothSmallInteger(num, den)) { |
87 |
if (__CanDoQuickAlignedNew(sizeof(struct __Fraction))) { /* OBJECT ALLOCATION */ |
|
88 |
OBJ newFraction; |
|
89 |
int spc; |
|
1 | 90 |
|
5364 | 91 |
__qCheckedAlignedNew(newFraction, sizeof(struct __Fraction)); |
92 |
__InstPtr(newFraction)->o_class = self; |
|
93 |
if (__intVal(den) < 0) { |
|
94 |
__FractionInstPtr(newFraction)->f_numerator = __MKSMALLINT(- __intVal(num)); |
|
95 |
__FractionInstPtr(newFraction)->f_denominator = __MKSMALLINT(- __intVal(den)); |
|
96 |
} else { |
|
97 |
__FractionInstPtr(newFraction)->f_numerator = num; |
|
98 |
__FractionInstPtr(newFraction)->f_denominator = den; |
|
99 |
} |
|
100 |
if (num == __MKSMALLINT(1)) { |
|
101 |
RETURN ( newFraction ); |
|
102 |
} |
|
103 |
} |
|
104 |
} |
|
1 | 105 |
} |
2790 | 106 |
%}. |
2792 | 107 |
newFraction isNil ifTrue:[ |
5364 | 108 |
newFraction := self basicNew setNumerator:num denominator:den. |
2792 | 109 |
]. |
110 |
^ newFraction reduced |
|
1 | 111 |
! ! |
112 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
113 |
!Fraction class methodsFor:'constants'! |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
114 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
115 |
pi |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
116 |
"return the constant pi as Fraction" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
117 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
118 |
^ self |
6063 | 119 |
numerator: 314159265358979323846264343 |
120 |
denominator:100000000000000000000000000 |
|
121 |
||
122 |
"/ ^ self |
|
123 |
"/ numerator: 314159265358979323846264338327950288419716939937510582097494459 |
|
124 |
"/ denominator:100000000000000000000000000000000000000000000000000000000000000 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
125 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
126 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
127 |
Fraction pi |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
128 |
Fraction pi asFloat |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
129 |
Float pi |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
130 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
131 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
132 |
"Modified: 5.11.1996 / 11:11:44 / cg" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
133 |
! |
701 | 134 |
|
135 |
unity |
|
1555 | 136 |
"return the neutral element for multiplication (1 / 1)" |
701 | 137 |
|
138 |
^ FractionOne |
|
1555 | 139 |
|
140 |
"Modified: 18.7.1996 / 12:26:06 / cg" |
|
701 | 141 |
! |
142 |
||
143 |
zero |
|
1555 | 144 |
"return the neutral element for addition (0 / 1)" |
701 | 145 |
|
146 |
^ FractionZero |
|
1555 | 147 |
|
148 |
"Modified: 18.7.1996 / 12:26:12 / cg" |
|
701 | 149 |
! ! |
150 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
151 |
!Fraction class methodsFor:'queries'! |
3 | 152 |
|
153 |
isBuiltInClass |
|
1264 | 154 |
"return true if this class is known by the run-time-system. |
155 |
Here, true is returned for myself, false for subclasses." |
|
3 | 156 |
|
157 |
^ self == Fraction |
|
1264 | 158 |
|
159 |
"Modified: 23.4.1996 / 15:59:10 / cg" |
|
3 | 160 |
! ! |
161 |
||
1 | 162 |
!Fraction methodsFor:'accessing'! |
163 |
||
164 |
denominator |
|
165 |
"return the denominator" |
|
166 |
||
167 |
^ denominator |
|
168 |
! |
|
169 |
||
701 | 170 |
numerator |
171 |
"return the numerator" |
|
1 | 172 |
|
701 | 173 |
^ numerator |
1 | 174 |
! ! |
175 |
||
176 |
!Fraction methodsFor:'arithmetic'! |
|
177 |
||
701 | 178 |
* aNumber |
179 |
"return the product of the receiver and the argument, aNumber" |
|
180 |
||
181 |
|n d| |
|
182 |
||
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
183 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
184 |
"/ notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
185 |
"/ the following code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
186 |
"/ and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
187 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
188 |
"/ Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
189 |
"/ mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
190 |
"/ (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
191 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
192 |
|
6646 | 193 |
aNumber isInteger ifTrue:[ |
194 |
^ self class |
|
195 |
numerator:(numerator * aNumber) |
|
196 |
denominator:denominator |
|
701 | 197 |
]. |
198 |
aNumber isFraction ifTrue:[ |
|
5364 | 199 |
aNumber isFixedPoint ifFalse:[ "/ the value was corrent, but the scale is lost |
200 |
n := numerator * aNumber numerator. |
|
201 |
d := denominator * aNumber denominator. |
|
6646 | 202 |
^ self class |
203 |
numerator:n |
|
204 |
denominator:d |
|
5364 | 205 |
] |
701 | 206 |
]. |
207 |
(aNumber isMemberOf:Float) ifTrue:[ |
|
5364 | 208 |
^ (numerator * aNumber) / denominator |
701 | 209 |
]. |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
210 |
|
701 | 211 |
^ aNumber productFromFraction:self |
1892 | 212 |
|
2792 | 213 |
"Modified: 28.7.1997 / 19:09:23 / cg" |
701 | 214 |
! |
215 |
||
1 | 216 |
+ aNumber |
217 |
"return the sum of the receiver and the argument, aNumber" |
|
218 |
||
219 |
|n d| |
|
220 |
||
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
221 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
222 |
"/ notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
223 |
"/ the following code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
224 |
"/ and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
225 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
226 |
"/ Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
227 |
"/ mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
228 |
"/ (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
229 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
230 |
|
6646 | 231 |
aNumber isInteger ifTrue:[ |
232 |
^ self class |
|
233 |
numerator:(numerator + (denominator * aNumber)) |
|
234 |
denominator:denominator |
|
1 | 235 |
]. |
259 | 236 |
aNumber isFraction ifTrue:[ |
6576 | 237 |
aNumber isFixedPoint ifFalse:[ "/ the value was correct, but the scale is lost |
5364 | 238 |
n := aNumber numerator. |
239 |
d := aNumber denominator. |
|
1 | 240 |
|
5364 | 241 |
"save a multiplication if possible" |
242 |
denominator == d ifTrue:[ |
|
243 |
n := numerator + n |
|
244 |
] ifFalse:[ |
|
245 |
n := (numerator * d) + (n * denominator). |
|
246 |
d := denominator * d. |
|
247 |
]. |
|
6646 | 248 |
^ self class |
249 |
numerator:n |
|
250 |
denominator:d |
|
5364 | 251 |
] |
1 | 252 |
]. |
16 | 253 |
(aNumber isMemberOf:Float) ifTrue:[ |
6576 | 254 |
^ aNumber + self asFloat |
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 |
||
265 |
|n d| |
|
266 |
||
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
267 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
268 |
"/ notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
269 |
"/ the following code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
270 |
"/ and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
271 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
272 |
"/ Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
273 |
"/ mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
274 |
"/ (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
275 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
276 |
|
6646 | 277 |
aNumber isInteger ifTrue:[ |
278 |
^ self class |
|
279 |
numerator:(numerator - (denominator * aNumber)) |
|
280 |
denominator:denominator |
|
1 | 281 |
]. |
259 | 282 |
aNumber isFraction ifTrue:[ |
5364 | 283 |
aNumber isFixedPoint ifFalse:[ "/ the value was corrent, but the scale is lost |
284 |
n := aNumber numerator. |
|
285 |
d := aNumber denominator. |
|
1 | 286 |
|
5364 | 287 |
"save a multiplication if possible" |
288 |
denominator == d ifTrue:[ |
|
289 |
n := numerator - n |
|
290 |
] ifFalse:[ |
|
291 |
n := (numerator * d) - (n * denominator). |
|
292 |
d := denominator * d |
|
293 |
]. |
|
6646 | 294 |
^ self class |
295 |
numerator:n |
|
296 |
denominator:d |
|
5364 | 297 |
] |
1 | 298 |
]. |
16 | 299 |
(aNumber isMemberOf:Float) ifTrue:[ |
5364 | 300 |
^ (numerator asFloat / denominator asFloat) - aNumber |
16 | 301 |
]. |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
302 |
|
1 | 303 |
^ aNumber differenceFromFraction:self |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
304 |
|
1892 | 305 |
" |
306 |
(1/3) - (1/9) |
|
307 |
(1/9) - (1/3) |
|
308 |
(999/1000) - (1/1000) |
|
309 |
(999/1000) - (1/1000000) |
|
310 |
(999000/1000000) - (1/1000000) |
|
311 |
" |
|
312 |
||
2792 | 313 |
"Modified: 28.7.1997 / 19:09:11 / cg" |
1 | 314 |
! |
315 |
||
316 |
/ aNumber |
|
317 |
"return the quotient of the receiver and the argument, aNumber" |
|
318 |
||
319 |
|n d| |
|
320 |
||
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
321 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
322 |
"/ notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
323 |
"/ the following code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
324 |
"/ and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
325 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
326 |
"/ Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
327 |
"/ mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
328 |
"/ (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
329 |
"/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
330 |
|
6646 | 331 |
aNumber isInteger ifTrue:[ |
332 |
^ self class |
|
333 |
numerator:numerator |
|
334 |
denominator:(denominator * aNumber) |
|
16 | 335 |
]. |
259 | 336 |
aNumber isFraction ifTrue:[ |
5364 | 337 |
aNumber isFixedPoint ifFalse:[ "/ the value was corrent, but the scale is lost |
338 |
n := numerator * aNumber denominator. |
|
339 |
d := denominator * aNumber numerator. |
|
6646 | 340 |
^ self class |
341 |
numerator:n |
|
342 |
denominator:d |
|
5364 | 343 |
] |
1 | 344 |
]. |
16 | 345 |
(aNumber isMemberOf:Float) ifTrue:[ |
5364 | 346 |
^ numerator / (denominator * aNumber) |
16 | 347 |
]. |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
348 |
|
1 | 349 |
^ aNumber quotientFromFraction:self |
1892 | 350 |
|
2792 | 351 |
"Modified: 28.7.1997 / 19:09:06 / cg" |
1 | 352 |
! |
353 |
||
354 |
// aNumber |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
355 |
"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
|
356 |
truncation towards negative infinity." |
1 | 357 |
|
358 |
^ (numerator * aNumber denominator) // (denominator * aNumber numerator) |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
359 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
360 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
361 |
0.5 // 1 |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
362 |
-0.5 // 1 |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3899
diff
changeset
|
363 |
(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
|
364 |
(-1/2) // 1 = -1 ifFalse:[self halt]. |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
365 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
366 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3899
diff
changeset
|
367 |
"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
|
368 |
"Modified: / 13.2.1998 / 09:15:35 / stefan" |
1 | 369 |
! |
370 |
||
371 |
negated |
|
372 |
"optional - could use inherited method ..." |
|
373 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
374 |
^ self class |
5364 | 375 |
numerator:(numerator negated) |
376 |
denominator:denominator |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
377 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
378 |
"Modified: 5.11.1996 / 10:29:11 / cg" |
1 | 379 |
! |
380 |
||
381 |
reciprocal |
|
382 |
"optional - could use inherited method ..." |
|
383 |
||
384 |
numerator == 1 ifTrue:[^ denominator]. |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
385 |
^ self class |
5364 | 386 |
numerator:denominator |
387 |
denominator:numerator |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
388 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
389 |
"Modified: 5.11.1996 / 10:29:22 / cg" |
1 | 390 |
! ! |
391 |
||
701 | 392 |
!Fraction methodsFor:'coercing & converting'! |
393 |
||
1892 | 394 |
asFixedPoint |
395 |
"return the receiver as fixedPoint number. |
|
396 |
Q: what should the scale be here ?" |
|
397 |
||
398 |
^ FixedPoint numerator:numerator denominator:denominator scale:2 |
|
399 |
||
400 |
" |
|
401 |
(1/2) asFixedPoint |
|
402 |
" |
|
403 |
||
404 |
"Created: 5.11.1996 / 15:15:54 / cg" |
|
405 |
! |
|
406 |
||
2140 | 407 |
asFixedPoint:scale |
408 |
"return the receiver as fixedPoint number, with the given number |
|
409 |
of post-decimal-point digits." |
|
410 |
||
411 |
^ FixedPoint numerator:numerator denominator:denominator scale:scale |
|
412 |
||
413 |
" |
|
414 |
(1/2) asFixedPoint:2 |
|
415 |
(1/3) asFixedPoint:2 |
|
416 |
(1/3) asFixedPoint:5 |
|
417 |
(2/3) asFixedPoint:2 |
|
418 |
(2/3) asFixedPoint:5 |
|
419 |
" |
|
420 |
||
421 |
"Created: 5.11.1996 / 15:15:54 / cg" |
|
422 |
"Modified: 10.1.1997 / 19:54:50 / cg" |
|
423 |
! |
|
424 |
||
701 | 425 |
asFloat |
426 |
"return a float with (approximately) my value" |
|
427 |
||
6576 | 428 |
|num den numShift denShift bits| |
6571
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
429 |
|
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
430 |
(numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[ |
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
431 |
^ (numerator asFloat) / (denominator asFloat) |
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
432 |
]. |
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
433 |
|
6576 | 434 |
"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
|
435 |
|
6576 | 436 |
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
|
437 |
num := numerator abs. |
6576 | 438 |
numShift := bits - num highBit. "(num highBit - bits) negated" |
439 |
numShift < 0 ifTrue:[num := num bitShift:numShift] ifFalse:[numShift := 0]. |
|
440 |
||
441 |
den := denominator. |
|
442 |
denShift := bits - den highBit. "(den highBit - bits) negated" |
|
443 |
denShift < 0 ifTrue:[den := den bitShift:denShift] ifFalse:[denShift := 0]. |
|
444 |
||
445 |
^ (num asFloat / den asFloat) * (2.0 raisedToInteger:denShift-numShift) * (num sign) |
|
6571
101905a14870
Fix #asFloat for non-trivial Fractions
Stefan Vogel <sv@exept.de>
parents:
6486
diff
changeset
|
446 |
|
6576 | 447 |
" |
448 |
(5/9) asFloat |
|
449 |
(-5/9) asFloat |
|
450 |
(500000000000/900000000000) asFloat |
|
451 |
(-500000000000/900000000000) asFloat |
|
452 |
(500000000000/9) asFloat |
|
453 |
(5/900000000000) asFloat |
|
454 |
89012345678901234567 asFloat / 123456789123456789 asFloat |
|
455 |
(89012345678901234567 / 123456789123456789) asFloat |
|
456 |
||
457 |
( |
|
458 |
180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125 |
|
459 |
/ |
|
460 |
180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250 |
|
461 |
) asFloat |
|
462 |
||
463 |
180338700661043257034670206806167960222709397862806840937993331366591676308781197477183367018067356365812757479444845320188679437752013593674158587947149815441890236037219685250845721864713487208757788709113534916165172927384095182655935222723385253851776639985379367854545495930551624041981995105743408203125 |
|
464 |
asFloat / |
|
465 |
180331613628627651967947866455016278082980736719853750685591387625058011528928110602436691256100991596843001549483950600930062886280582766771424470965440873615557144641435276844465734361353086032476712374317224249252177316815544331763696909434844464464323192083930469387098582956241443753242492675781250 |
|
466 |
asFloat |
|
467 |
" |
|
701 | 468 |
! |
1 | 469 |
|
701 | 470 |
asFraction |
471 |
"return the receiver as fraction - thats itself" |
|
472 |
||
473 |
^ self |
|
474 |
! |
|
475 |
||
476 |
asInteger |
|
477 |
"return an integer with my value - will usually truncate" |
|
1 | 478 |
|
479 |
^ numerator // denominator |
|
480 |
! |
|
481 |
||
701 | 482 |
asLargeInteger |
483 |
"return an integer with my value - will usually truncate" |
|
484 |
||
485 |
^ self asInteger asLargeInteger |
|
486 |
! |
|
487 |
||
1200 | 488 |
asShortFloat |
6576 | 489 |
"return a short float with (approximately) my value" |
490 |
||
491 |
(numerator class == SmallInteger and:[denominator class == SmallInteger]) ifTrue:[ |
|
492 |
^ (numerator asShortFloat) / (denominator asShortFloat) |
|
493 |
]. |
|
1200 | 494 |
|
6576 | 495 |
^ self asFloat asShortFloat |
1200 | 496 |
|
6576 | 497 |
" |
498 |
(5/9) asShortFloat |
|
499 |
(500000000000/900000000000) asShortFloat |
|
500 |
(500000000000/9) asShortFloat |
|
501 |
" |
|
1200 | 502 |
! |
503 |
||
701 | 504 |
coerce:aNumber |
505 |
"return aNumber converted into receivers type" |
|
506 |
||
507 |
^ aNumber asFraction |
|
508 |
! |
|
509 |
||
510 |
generality |
|
511 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
512 |
||
513 |
^ 60 |
|
514 |
! ! |
|
515 |
||
516 |
!Fraction methodsFor:'comparing'! |
|
517 |
||
518 |
< aNumber |
|
519 |
"return true if the receiver is less |
|
520 |
than aNumber, false otherwise." |
|
1 | 521 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
522 |
|d n| |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
523 |
|
701 | 524 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
5364 | 525 |
^ numerator < (denominator * aNumber) |
701 | 526 |
]. |
527 |
aNumber isFraction ifTrue:[ |
|
5364 | 528 |
d := aNumber denominator. |
529 |
n := aNumber numerator. |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
530 |
|
5364 | 531 |
"/ save a multiplication if possible |
532 |
d == denominator ifTrue:[ |
|
533 |
^ numerator < n |
|
534 |
]. |
|
535 |
^ (numerator * d) < (denominator * n) |
|
701 | 536 |
]. |
537 |
^ aNumber lessFromFraction:self |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
538 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
539 |
"Modified: 5.11.1996 / 10:30:52 / cg" |
701 | 540 |
! |
541 |
||
542 |
= aNumber |
|
543 |
"return true, if the argument represents the same numeric value |
|
544 |
as the receiver, false otherwise" |
|
545 |
||
546 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
|
5364 | 547 |
(denominator = 1) ifFalse:[^ false]. |
548 |
^ numerator = aNumber |
|
1 | 549 |
]. |
701 | 550 |
aNumber isFraction ifTrue:[ |
5364 | 551 |
denominator = aNumber denominator ifTrue:[ |
552 |
^ numerator = aNumber numerator |
|
553 |
]. |
|
554 |
^ self sameFractionValueAs:aNumber |
|
3631
26a44b284912
handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
555 |
]. |
26a44b284912
handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
556 |
(aNumber isInteger) ifTrue:[ |
5364 | 557 |
(denominator = 1) ifFalse:[^ false]. |
558 |
^ numerator = aNumber |
|
701 | 559 |
]. |
560 |
^ self retry:#= coercing:aNumber |
|
3631
26a44b284912
handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
561 |
|
26a44b284912
handle largeInteger case in Fraction =
Claus Gittinger <cg@exept.de>
parents:
2894
diff
changeset
|
562 |
"Modified: / 7.7.1998 / 17:17:07 / cg" |
701 | 563 |
! |
564 |
||
565 |
> aNumber |
|
566 |
"return true if the receiver is greater |
|
567 |
than aNumber, false otherwise." |
|
568 |
"optional - could use inherited method ..." |
|
569 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
570 |
|d n| |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
571 |
|
701 | 572 |
(aNumber isMemberOf:SmallInteger) ifTrue:[ |
5364 | 573 |
^ numerator > (denominator * aNumber) |
701 | 574 |
]. |
575 |
aNumber isFraction ifTrue:[ |
|
5364 | 576 |
d := aNumber denominator. |
577 |
n := aNumber numerator. |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
578 |
|
5364 | 579 |
"/ save a multiplication if possible |
580 |
d == denominator ifTrue:[ |
|
581 |
^ numerator > n |
|
582 |
]. |
|
583 |
^ (numerator * d) > (denominator * n) |
|
701 | 584 |
]. |
585 |
^ self retry:#> coercing:aNumber |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
586 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
587 |
"Modified: 5.11.1996 / 10:31:28 / cg" |
4593 | 588 |
! |
589 |
||
590 |
hash |
|
591 |
"return a number for hashing; redefined, since fractions compare |
|
592 |
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
|
593 |
as 3 hash." |
4593 | 594 |
|
595 |
(denominator = 1) ifTrue:[^ numerator hash]. |
|
596 |
||
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
597 |
^ self asFloat hash |
4593 | 598 |
|
599 |
" |
|
600 |
3 hash |
|
601 |
(9/3) hash |
|
602 |
3.0 hash |
|
603 |
(1/2) hash |
|
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
604 |
(1/4) hash |
4593 | 605 |
0.0 hash |
606 |
0.5 hash |
|
607 |
0.25 hash |
|
608 |
0.4 hash |
|
609 |
" |
|
5364 | 610 |
! |
611 |
||
612 |
sameFractionValueAs:aNumber |
|
613 |
"return true, if the argument represents the same numeric value |
|
614 |
as the receiver, false otherwise" |
|
615 |
||
616 |
|rSelf rNum| |
|
617 |
||
618 |
rSelf := self reduced. |
|
619 |
rNum := aNumber reduced. |
|
620 |
rSelf denominator = rNum denominator ifTrue:[ |
|
621 |
^ rSelf numerator = rNum numerator |
|
622 |
]. |
|
623 |
^ false |
|
1 | 624 |
! ! |
625 |
||
626 |
!Fraction methodsFor:'double dispatching'! |
|
627 |
||
701 | 628 |
differenceFromFloat:aFloat |
629 |
"sent when a float does not know how to subtract the receiver, a fraction" |
|
1 | 630 |
|
701 | 631 |
^ (aFloat * denominator - numerator) / denominator |
1 | 632 |
! |
633 |
||
634 |
differenceFromInteger:anInteger |
|
213 | 635 |
"sent when an integer does not know how to subtract the receiver, a fraction" |
1 | 636 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
637 |
^ (self class |
5364 | 638 |
numerator:((anInteger * denominator) - numerator) |
639 |
denominator:denominator) |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
640 |
|
2792 | 641 |
"Modified: 28.7.1997 / 19:08:53 / cg" |
1 | 642 |
! |
643 |
||
701 | 644 |
lessFromInteger:anInteger |
645 |
"sent when an integer does not know how to compare to the receiver, a fraction" |
|
646 |
||
647 |
^ (denominator * anInteger) < numerator |
|
648 |
! |
|
649 |
||
650 |
productFromFloat:aFloat |
|
651 |
"sent when a float does not know how to multiply the receiver, a fraction" |
|
652 |
||
653 |
^ aFloat * numerator / denominator |
|
654 |
! |
|
655 |
||
1 | 656 |
productFromInteger:anInteger |
213 | 657 |
"sent when an integer does not know how to multiply the receiver, a fraction" |
1 | 658 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
659 |
^ (self class |
5364 | 660 |
numerator:(anInteger * numerator) |
661 |
denominator:denominator) |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
662 |
|
2792 | 663 |
"Modified: 28.7.1997 / 19:06:22 / cg" |
1 | 664 |
! |
665 |
||
701 | 666 |
quotientFromFloat:aFloat |
667 |
"sent when a float does not know how to divide by the receiver, a fraction" |
|
668 |
||
669 |
^ (aFloat * denominator) / numerator |
|
670 |
! |
|
671 |
||
324 | 672 |
quotientFromInteger:anInteger |
673 |
"sent when an integer does not know how to divide by the receiver, a fraction" |
|
674 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
675 |
^ (self class |
5364 | 676 |
numerator:(anInteger * denominator) |
677 |
denominator:numerator) |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
678 |
|
2792 | 679 |
"Modified: 28.7.1997 / 19:08:46 / cg" |
324 | 680 |
! |
681 |
||
1 | 682 |
sumFromFloat:aFloat |
213 | 683 |
"sent when a float does not know how to add the receiver, a fraction" |
1 | 684 |
|
685 |
^ (aFloat * denominator + numerator) / denominator |
|
686 |
! |
|
687 |
||
701 | 688 |
sumFromInteger:anInteger |
689 |
"sent when an integer does not know how to add the receiver, a fraction" |
|
1 | 690 |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
691 |
^ (self class |
5364 | 692 |
numerator:(numerator + (anInteger * denominator)) |
693 |
denominator:denominator) |
|
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
694 |
|
2792 | 695 |
"Modified: 28.7.1997 / 19:08:40 / cg" |
1 | 696 |
! ! |
697 |
||
6486 | 698 |
!Fraction methodsFor:'encoding'! |
699 |
||
700 |
encodeOn:anEncoder with:aParameter |
|
701 |
||
702 |
anEncoder encodeFraction:self with:aParameter |
|
703 |
! ! |
|
704 |
||
92 | 705 |
!Fraction methodsFor:'printing & storing'! |
1 | 706 |
|
54 | 707 |
printOn:aStream |
708 |
"append a printed representation of the receiver to the |
|
709 |
argument, aStream" |
|
710 |
||
711 |
aStream nextPut:$(. |
|
712 |
numerator printOn:aStream. |
|
713 |
aStream nextPut:$/. |
|
714 |
denominator printOn:aStream. |
|
715 |
aStream nextPut:$) |
|
1 | 716 |
! ! |
701 | 717 |
|
718 |
!Fraction methodsFor:'private'! |
|
719 |
||
720 |
reduced |
|
721 |
"reduce the receiver" |
|
722 |
||
2790 | 723 |
|gcd den| |
701 | 724 |
|
2790 | 725 |
den := denominator. |
726 |
den < 0 ifTrue:[ |
|
5364 | 727 |
numerator := numerator negated. |
728 |
den := denominator := den negated. |
|
2789
e3e8707d26b4
make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents:
2140
diff
changeset
|
729 |
]. |
e3e8707d26b4
make certain, that denominator is positive (in #reduce)
Claus Gittinger <cg@exept.de>
parents:
2140
diff
changeset
|
730 |
|
2790 | 731 |
den == 1 ifTrue:[^ numerator]. |
701 | 732 |
numerator == 1 ifTrue:[^ self]. |
733 |
numerator == 0 ifTrue:[^ 0]. |
|
734 |
||
2790 | 735 |
gcd := numerator gcd:den. |
736 |
(gcd ~~ 1) ifTrue:[ |
|
6576 | 737 |
gcd < 0 ifTrue:[ |
738 |
gcd := gcd negated. |
|
739 |
]. |
|
5364 | 740 |
numerator := numerator // gcd. |
741 |
denominator := den := den // gcd. |
|
742 |
(den == 1) ifTrue:[^ numerator]. |
|
701 | 743 |
]. |
744 |
^ self |
|
745 |
! |
|
746 |
||
747 |
setNumerator:num denominator:den |
|
748 |
"set both numerator and denominator" |
|
749 |
||
750 |
numerator := num. |
|
751 |
denominator := den |
|
752 |
! ! |
|
753 |
||
6650 | 754 |
!Fraction methodsFor:'testing'! |
701 | 755 |
|
756 |
isFraction |
|
757 |
"return true, if the receiver is some kind of fraction; |
|
758 |
true is returned here - the method is redefined from Object." |
|
759 |
||
760 |
^ true |
|
6650 | 761 |
! |
701 | 762 |
|
4658
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
763 |
isLiteral |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
764 |
"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
|
765 |
(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
|
766 |
|
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
767 |
^ true |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
768 |
|
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
769 |
! |
1a74754fbe91
moved isLiteral (complex and FixedPoint are not)
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
770 |
|
701 | 771 |
negative |
772 |
"return true if the receiver is negative" |
|
773 |
||
774 |
(numerator < 0) ifTrue:[ |
|
5364 | 775 |
^ (denominator < 0) not |
701 | 776 |
]. |
777 |
^ (denominator < 0) |
|
778 |
! ! |
|
779 |
||
5552 | 780 |
!Fraction methodsFor:'truncation & rounding'! |
701 | 781 |
|
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
782 |
fractionPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
783 |
"extract the after-decimal fraction part." |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
784 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
785 |
numerator abs < denominator abs ifTrue:[ |
5364 | 786 |
^ self |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
787 |
]. |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
788 |
^ super fractionPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
789 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
790 |
" |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
791 |
(3/2) fractionPart |
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
792 |
(-3/2) fractionPart |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
793 |
(2/3) fractionPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
794 |
((3/2)*(15/4)) fractionPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
795 |
((2/3)*(4/15)) fractionPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
796 |
" |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
797 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
798 |
"Modified: / 5.11.2001 / 17:55:25 / cg" |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
799 |
! |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
800 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
801 |
integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
802 |
"extract the pre-decimal integer part." |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
803 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
804 |
numerator abs < denominator abs ifTrue:[ |
5364 | 805 |
^ 0 |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
806 |
]. |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
807 |
^ super integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
808 |
|
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
809 |
" |
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
810 |
(3/2) integerPart |
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
811 |
(-3/2) integerPart |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
812 |
(2/3) integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
813 |
((3/2)*(15/4)) integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
814 |
((2/3)*(4/15)) integerPart |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
815 |
" |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
816 |
|
6153
e92766db42c6
integerPart and fractionPart fixed for negative numbers
Claus Gittinger <cg@exept.de>
parents:
6064
diff
changeset
|
817 |
"Modified: / 5.11.2001 / 17:55:01 / cg" |
3899
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
818 |
! |
aa9b50cdfced
added #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3631
diff
changeset
|
819 |
|
701 | 820 |
rounded |
821 |
"return the receiver rounded to the nearest integer as integer" |
|
822 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
823 |
"/ mhmh - what about -(1/2) |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
824 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
825 |
|t| |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
826 |
|
701 | 827 |
self negative ifTrue:[ |
5364 | 828 |
t := self - (1/2) |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
829 |
] ifFalse:[ |
5364 | 830 |
t := self + (1/2) |
701 | 831 |
]. |
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
832 |
^ t truncated. |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
833 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
834 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
835 |
(1/3) rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
836 |
(1/3) negated rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
837 |
(1/2) rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
838 |
(1/2) negated rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
839 |
0.5 rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
840 |
-0.5 rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
841 |
(2/3) rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
842 |
(2/3) negated rounded |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
843 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
844 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
845 |
"Modified: 5.11.1996 / 11:32:32 / cg" |
701 | 846 |
! |
847 |
||
848 |
truncated |
|
849 |
"return the receiver truncated towards zero as Integer" |
|
850 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
851 |
^ numerator quo: denominator |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
852 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
853 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
854 |
(3/2) truncated |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
855 |
(3/2) negated truncated |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
856 |
" |
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
857 |
|
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
858 |
"Modified: 5.11.1996 / 12:18:46 / cg" |
701 | 859 |
! ! |
860 |
||
1878
5615af5e7f47
fixed truncated & rounded for negative fractions
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
861 |
!Fraction class methodsFor:'documentation'! |
701 | 862 |
|
863 |
version |
|
6650 | 864 |
^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.58 2002-07-18 13:39:47 stefan Exp $' |
701 | 865 |
! ! |
866 |
Fraction initialize! |