You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by bu...@apache.org on 2005/06/20 13:29:59 UTC
DO NOT REPLY [Bug 35432] -
Additional method for Fraction to bring several fractions to common denominator
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=35432>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.
http://issues.apache.org/bugzilla/show_bug.cgi?id=35432
------- Additional Comments From J.Weimar@tu-bs.de 2005-06-20 13:29 -------
Fraction.java:
/**
* Extend all <code>Fraction</code> in the array to the smallest common
denominator.
* @param fractions an array of fractions
* @return the common denominator which all fractions now share. This is the
least common multiple of all
* <code>Fraction</code>s in the input.
* @throws IllegalArgumentException if one fraction is <code>null</code>
* @throws ArithmeticException if the resulting numerator or denominator
* cannot be represented in an <code>int</code>.
*/
public static int extendCommon(Fraction[] fractions){
if (fractions == null) {
throw new IllegalArgumentException("The array of fractions must not
be null");
}
// calculate the least common multiple of all denominators.
int lcm = 1;
for (int i=fractions.length-1; i >= 0; i--){
if (fractions[i] == null) {
throw new IllegalArgumentException("The fractions must not be
null");
}
fractions[i].reduce();
lcm = MathUtils.lcm(lcm, fractions[i].denominator);
}
if (lcm==1) {
// all fractions are unchanged.
return 1;
}
// loop through the fractions again, extending each by lcm/denom
for (int i=fractions.length-1; i >= 0; i--){
Fraction fi = fractions[i];
int factor = lcm/fi.denominator;
// extend numerator and denominator
fi.numerator = MathUtils.mulAndCheck(fi.numerator, factor);
fi.denominator = MathUtils.mulAndCheck(fi.denominator, factor);
}
return lcm;
}
FractionTest.java:
public void testExtendCommon() {
Fraction array0[] = new Fraction[]{};
int lcm = Fraction.extendCommon(array0);
assertEquals(1,lcm);
Fraction a = new Fraction(1, 2);
Fraction array1[] = new Fraction[]{a};
lcm = Fraction.extendCommon(array1);
assertEquals(2,lcm);
assertFraction(1, 2, a);
Fraction b = new Fraction(2, 3);
Fraction array2[] = new Fraction[]{a,b};
lcm = Fraction.extendCommon(array2);
assertEquals(6,lcm);
assertFraction(3, 6, a);
assertFraction(4, 6, b);
Fraction c = new Fraction(1, 100000);
Fraction array3[] = new Fraction[]{a,b,c};
lcm = Fraction.extendCommon(array3);
assertEquals(300000,lcm);
assertFraction(150000, 300000, a);
assertFraction(200000, 300000, b);
assertFraction(3, 300000, c);
lcm = Fraction.extendCommon(array2);
assertEquals(6,lcm);
assertFraction(3, 6, a);
assertFraction(4, 6, b);
// Now check integer overflow
Fraction f1 = new Fraction(Integer.MAX_VALUE, 5);
Fraction f2 = new Fraction(-1,Integer.MAX_VALUE);
Fraction array2a[] = new Fraction[]{f1,f2};
try {
lcm = Fraction.extendCommon(array2a); // should overflow
fail("expecting ArithmeticException but got lcm=" + lcm);
} catch (ArithmeticException ex) {}
}
--
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org