File |
Line |
org/apache/commons/numbers/fraction/BigFraction.java |
167 |
org/apache/commons/numbers/fraction/Fraction.java |
190 |
}
// Support 2^31 as maximum denominator.
// This is negative as an integer so convert to long.
final long maxDen = Math.abs((long) maxDenominator);
long p0 = 1;
long q0 = 0;
long p1 = a0;
long q1 = 1;
long p2;
long q2;
int n = 0;
boolean stop = false;
do {
++n;
final double r1 = 1.0 / (r0 - a0);
final long a1 = (long) Math.floor(r1);
p2 = (a1 * p1) + p0;
q2 = (a1 * q1) + q0;
if (Long.compareUnsigned(p2, OVERFLOW) > 0 ||
Long.compareUnsigned(q2, OVERFLOW) > 0) {
// In maxDenominator mode, fall-back to the previous valid fraction.
if (epsilon == 0) { |
File |
Line |
org/apache/commons/numbers/fraction/BigFraction.java |
192 |
org/apache/commons/numbers/fraction/Fraction.java |
216 |
if (epsilon == 0) {
p2 = p1;
q2 = q1;
break;
}
throw new FractionException(FractionException.ERROR_CONVERSION_OVERFLOW, value, p2, q2);
}
final double convergent = (double) p2 / (double) q2;
if (n < maxIterations &&
Math.abs(convergent - absValue) > epsilon &&
q2 < maxDen) {
p0 = p1;
p1 = p2;
q0 = q1;
q1 = q2;
a0 = a1;
r0 = r1;
} else {
stop = true;
}
} while (!stop);
if (n >= maxIterations) {
throw new FractionException(FractionException.ERROR_CONVERSION, value, maxIterations);
} |