use of java.math.RoundingMode in project guava by hceylan.
the class DoubleMathTest method testRoundLog2Half.
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 hceylan.
the class DoubleMathTest method testRoundIntegralDoubleToInt.
public void testRoundIntegralDoubleToInt() {
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_INT_AS_BIG_DECIMAL) <= 0 & expected.compareTo(MIN_INT_AS_BIG_DECIMAL) >= 0;
try {
assertEquals(expected.intValue(), DoubleMath.roundToInt(d, mode));
assertTrue(isInBounds);
} catch (ArithmeticException e) {
assertFalse(isInBounds);
}
}
}
}
use of java.math.RoundingMode in project guava by hceylan.
the class DoubleMathTest method testRoundIntegralDoubleToLong.
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 hceylan.
the class BigIntegerMathTest method testLog2Ceiling.
public void testLog2Ceiling() {
for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
for (RoundingMode mode : asList(CEILING, UP)) {
int result = BigIntegerMath.log2(x, mode);
assertTrue(ZERO.setBit(result).compareTo(x) >= 0);
assertTrue(result == 0 || ZERO.setBit(result - 1).compareTo(x) < 0);
}
}
}
use of java.math.RoundingMode in project guava by hceylan.
the class BigIntegerMathTest method testSqrtFloor.
public void testSqrtFloor() {
for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
for (RoundingMode mode : asList(FLOOR, DOWN)) {
BigInteger result = BigIntegerMath.sqrt(x, mode);
assertTrue(result.compareTo(ZERO) > 0);
assertTrue(result.pow(2).compareTo(x) <= 0);
assertTrue(result.add(ONE).pow(2).compareTo(x) > 0);
}
}
}
Aggregations