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 https://issues.apache.org/jira/browse/MATH-181, 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;
               return;
            }
            if ((FastMath.abs(p2) > overflow) || (FastMath.abs(q2) > overflow)) 
{
                throw new FractionConversionException(value, p2, q2);
            }


Reply via email to