Search in sources :

Example 96 with RoundingMode

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) {
            }
        }
    }
}
Also used : RoundingMode(java.math.RoundingMode) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 97 with RoundingMode

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);
            }
        }
    }
}
Also used : RoundingMode(java.math.RoundingMode) BigDecimal(java.math.BigDecimal) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 98 with RoundingMode

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) {
        }
    }
}
Also used : RoundingMode(java.math.RoundingMode) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 99 with RoundingMode

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));
            }
        }
    }
}
Also used : RoundingMode(java.math.RoundingMode) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 100 with RoundingMode

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));
        }
    }
}
Also used : RoundingMode(java.math.RoundingMode) BigDecimal(java.math.BigDecimal) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Aggregations

RoundingMode (java.math.RoundingMode)203 BigDecimal (java.math.BigDecimal)127 BigInteger (java.math.BigInteger)88 MathContext (java.math.MathContext)72 GwtIncompatible (com.google.common.annotations.GwtIncompatible)39 DecimalFormat (java.text.DecimalFormat)5 IDataMap (permafrost.tundra.data.IDataMap)5 Test (org.junit.Test)3 MifosConfigurationManager (org.mifos.config.business.MifosConfigurationManager)3 DecimalFormatSymbols (java.text.DecimalFormatSymbols)1 NumberFormat (java.text.NumberFormat)1 AseDatabasePlatform (org.jumpmind.db.platform.ase.AseDatabasePlatform)1 MsSql2000DatabasePlatform (org.jumpmind.db.platform.mssql.MsSql2000DatabasePlatform)1 MsSql2005DatabasePlatform (org.jumpmind.db.platform.mssql.MsSql2005DatabasePlatform)1 MsSql2008DatabasePlatform (org.jumpmind.db.platform.mssql.MsSql2008DatabasePlatform)1 MySqlDatabasePlatform (org.jumpmind.db.platform.mysql.MySqlDatabasePlatform)1 OracleDatabasePlatform (org.jumpmind.db.platform.oracle.OracleDatabasePlatform)1 SqlAnywhereDatabasePlatform (org.jumpmind.db.platform.sqlanywhere.SqlAnywhereDatabasePlatform)1 ForecastBindingConfig (org.openhab.binding.weather.internal.common.binding.ForecastBindingConfig)1 WeatherBindingConfig (org.openhab.binding.weather.internal.common.binding.WeatherBindingConfig)1