use of java.math.RoundingMode in project guava by google.
the class DoubleMathTest method testRoundLog2ThrowsOnNegative.
// DoubleMath.log2(double, RoundingMode)
@GwtIncompatible
public void testRoundLog2ThrowsOnNegative() {
for (RoundingMode mode : ALL_ROUNDING_MODES) {
for (double d : POSITIVE_FINITE_DOUBLE_CANDIDATES) {
try {
DoubleMath.log2(-d, mode);
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
}
}
}
use of java.math.RoundingMode in project guava by google.
the class DoubleMathTest method testRoundIntegralDoubleToLong.
// DoubleMath.roundToLong(double, RoundingMode)
@GwtIncompatible
public void testRoundIntegralDoubleToLong() {
for (double d : INTEGRAL_DOUBLE_CANDIDATES) {
for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) {
BigDecimal expected = new BigDecimal(d).setScale(0, mode);
boolean isInBounds = expected.compareTo(MAX_LONG_AS_BIG_DECIMAL) <= 0 & expected.compareTo(MIN_LONG_AS_BIG_DECIMAL) >= 0;
try {
assertEquals(expected.longValue(), DoubleMath.roundToLong(d, mode));
assertTrue(isInBounds);
} catch (ArithmeticException e) {
assertFalse(isInBounds);
}
}
}
}
use of java.math.RoundingMode in project guava by google.
the class DoubleMathTest method testRoundInfiniteToLongAlwaysFails.
// DoubleMath.roundToLong(double, RoundingMode)
@GwtIncompatible
public void testRoundInfiniteToLongAlwaysFails() {
for (RoundingMode mode : ALL_ROUNDING_MODES) {
try {
DoubleMath.roundToLong(Double.POSITIVE_INFINITY, mode);
fail("Expected ArithmeticException");
} catch (ArithmeticException expected) {
}
try {
DoubleMath.roundToLong(Double.NEGATIVE_INFINITY, mode);
fail("Expected ArithmeticException");
} catch (ArithmeticException expected) {
}
}
}
use of java.math.RoundingMode in project guava by google.
the class DoubleMathTest method testRoundLog2Half.
// DoubleMath.log2(double, RoundingMode)
@GwtIncompatible
public void testRoundLog2Half() {
// We don't expect perfect rounding accuracy.
for (int exp : asList(-1022, -50, -1, 0, 1, 2, 3, 4, 100, 1022, 1023)) {
for (RoundingMode mode : asList(HALF_EVEN, HALF_UP, HALF_DOWN)) {
double x = Math.scalb(Math.sqrt(2) + 0.001, exp);
double y = Math.scalb(Math.sqrt(2) - 0.001, exp);
if (exp < 0) {
assertEquals(exp + 1, DoubleMath.log2(x, mode));
assertEquals(exp, DoubleMath.log2(y, mode));
} else {
assertEquals(exp + 1, DoubleMath.log2(x, mode));
assertEquals(exp, DoubleMath.log2(y, mode));
}
}
}
}
use of java.math.RoundingMode in project guava by google.
the class DoubleMathTest method testRoundFractionalDoubleToBigInteger.
// DoubleMath.roundToBigInteger(double, RoundingMode)
@GwtIncompatible
public void testRoundFractionalDoubleToBigInteger() {
for (double d : FRACTIONAL_DOUBLE_CANDIDATES) {
for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) {
BigDecimal expected = new BigDecimal(d).setScale(0, mode);
assertEquals(expected.toBigInteger(), DoubleMath.roundToBigInteger(d, mode));
}
}
}
Aggregations