Search in sources :

Example 46 with RoundingMode

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));
            }
        }
    }
}
Also used : RoundingMode(java.math.RoundingMode)

Example 47 with RoundingMode

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

Example 48 with RoundingMode

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

Example 49 with RoundingMode

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);
        }
    }
}
Also used : RoundingMode(java.math.RoundingMode) BigInteger(java.math.BigInteger)

Example 50 with RoundingMode

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);
        }
    }
}
Also used : RoundingMode(java.math.RoundingMode) BigInteger(java.math.BigInteger)

Aggregations

RoundingMode (java.math.RoundingMode)206 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 GridBagConstraints (java.awt.GridBagConstraints)2 GridBagLayout (java.awt.GridBagLayout)2 Insets (java.awt.Insets)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 ButtonGroup (javax.swing.ButtonGroup)2 JLabel (javax.swing.JLabel)2 JPanel (javax.swing.JPanel)2 JRadioButton (javax.swing.JRadioButton)2 JSpinner (javax.swing.JSpinner)2 SpinnerNumberModel (javax.swing.SpinnerNumberModel)2