mirror of
https://github.com/apache/commons-math.git
synced 2025-02-13 05:26:44 +00:00
[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:
parent
63a48705a4
commit
d7c0f27e9f
@ -178,7 +178,7 @@ public class Fraction
|
|||||||
long overflow = Integer.MAX_VALUE;
|
long overflow = Integer.MAX_VALUE;
|
||||||
double r0 = value;
|
double r0 = value;
|
||||||
long a0 = (long)FastMath.floor(r0);
|
long a0 = (long)FastMath.floor(r0);
|
||||||
if (a0 > overflow) {
|
if (FastMath.abs(a0) > overflow) {
|
||||||
throw new FractionConversionException(value, a0, 1l);
|
throw new FractionConversionException(value, a0, 1l);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,7 +206,7 @@ public class Fraction
|
|||||||
long a1 = (long)FastMath.floor(r1);
|
long a1 = (long)FastMath.floor(r1);
|
||||||
p2 = (a1 * p1) + p0;
|
p2 = (a1 * p1) + p0;
|
||||||
q2 = (a1 * q1) + q0;
|
q2 = (a1 * q1) + q0;
|
||||||
if ((p2 > overflow) || (q2 > overflow)) {
|
if ((FastMath.abs(p2) > overflow) || (FastMath.abs(q2) > overflow)) {
|
||||||
throw new FractionConversionException(value, p2, q2);
|
throw new FractionConversionException(value, p2, q2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,6 +60,7 @@ public class FractionTest {
|
|||||||
} catch (MathArithmeticException ex) {
|
} catch (MathArithmeticException ex) {
|
||||||
// success
|
// success
|
||||||
}
|
}
|
||||||
|
|
||||||
assertFraction(0, 1, new Fraction(0.00000000000001));
|
assertFraction(0, 1, new Fraction(0.00000000000001));
|
||||||
assertFraction(2, 5, new Fraction(0.40000000000001));
|
assertFraction(2, 5, new Fraction(0.40000000000001));
|
||||||
assertFraction(15, 1, new Fraction(15.0000000000001));
|
assertFraction(15, 1, new Fraction(15.0000000000001));
|
||||||
@ -134,6 +135,8 @@ public class FractionTest {
|
|||||||
public void testIntegerOverflow() {
|
public void testIntegerOverflow() {
|
||||||
checkIntegerOverflow(0.75000000001455192);
|
checkIntegerOverflow(0.75000000001455192);
|
||||||
checkIntegerOverflow(1.0e10);
|
checkIntegerOverflow(1.0e10);
|
||||||
|
checkIntegerOverflow(-1.0e10);
|
||||||
|
checkIntegerOverflow(-43979.60679604749);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkIntegerOverflow(double a) {
|
private void checkIntegerOverflow(double a) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user