[MATH-836] Improve overflow check for negative values in Fraction constructor.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1368253 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2012-08-01 21:24:27 +00:00
parent 63a48705a4
commit d7c0f27e9f
2 changed files with 5 additions and 2 deletions

View File

@ -178,7 +178,7 @@ public class Fraction
long overflow = Integer.MAX_VALUE;
double r0 = value;
long a0 = (long)FastMath.floor(r0);
if (a0 > overflow) {
if (FastMath.abs(a0) > overflow) {
throw new FractionConversionException(value, a0, 1l);
}
@ -206,7 +206,7 @@ public class Fraction
long a1 = (long)FastMath.floor(r1);
p2 = (a1 * p1) + p0;
q2 = (a1 * q1) + q0;
if ((p2 > overflow) || (q2 > overflow)) {
if ((FastMath.abs(p2) > overflow) || (FastMath.abs(q2) > overflow)) {
throw new FractionConversionException(value, p2, q2);
}

View File

@ -60,6 +60,7 @@ public class FractionTest {
} catch (MathArithmeticException ex) {
// success
}
assertFraction(0, 1, new Fraction(0.00000000000001));
assertFraction(2, 5, new Fraction(0.40000000000001));
assertFraction(15, 1, new Fraction(15.0000000000001));
@ -134,6 +135,8 @@ public class FractionTest {
public void testIntegerOverflow() {
checkIntegerOverflow(0.75000000001455192);
checkIntegerOverflow(1.0e10);
checkIntegerOverflow(-1.0e10);
checkIntegerOverflow(-43979.60679604749);
}
private void checkIntegerOverflow(double a) {