commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From luc <...@spaceroots.org>
Subject Re: math3.fraction.Fraction :: overflow exception with maxDenominator
Date Sun, 23 Jun 2013 06:55:35 GMT
Hi Allison,

Le 2013-06-20 12:54, Allison, Timothy B. a écrit :
> 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?

Yes, this is a reasonable fix. Could you open a JIRA issue and attache 
your patch
including a test case?

Thanks a lot
Luc

>
>             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);
>             }


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message