commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Allison, Timothy B." <>
Subject math3.fraction.Fraction :: overflow exception with maxDenominator
Date Thu, 20 Jun 2013 10:54:05 GMT
I'm getting an overflow exception when I try to create a fraction with a maxDenominator from
a double that is very close to a simple fraction.  For example:

double d = 0.5000000001;
Fraction f = new Fraction(d, 10);

According to, there are two separate use cases:
one for a user-specified epsilon (in which case maxDenominator is set to Integer.MAX_VALUE),
and one for a user-specified maxDenominator (in which case, epsilon is set to 0.0f).

If I add a check for whether q2 is > maxDenominator before the potential overflow throw,
I no longer have the problem mentioned above. The overflow throw, I think, is designed for
the user-defined epsilon use case, not the maxDenominator use case.

Is this a reasonable fix?  Should I submit a patch?

            double r1 = 1.0 / (r0 - a0);
            long a1 = (long)FastMath.floor(r1);
            p2 = (a1 * p1) + p0;
            q2 = (a1 * q1) + q0;

            if (q2 >= maxDenominator){
               this.numerator = (int) p1;
               this.denominator = (int) q1;
            if ((FastMath.abs(p2) > overflow) || (FastMath.abs(q2) > overflow)) {
                throw new FractionConversionException(value, p2, q2);

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message