List of usage examples for java.math BigDecimal BigDecimal
public BigDecimal(long val, MathContext mc)
From source file:Main.java
public static BigDecimal cuberoot(BigDecimal b) { // Specify a math context with 40 digits of precision. MathContext mc = new MathContext(40); BigDecimal x = new BigDecimal("1", mc); // Search for the cube root via the Newton-Raphson loop. Output each // // successive iteration's value. for (int i = 0; i < ITER; i++) { x = x.subtract(//from w w w. j a va 2 s . c o m x.pow(3, mc).subtract(b, mc).divide(new BigDecimal("3", mc).multiply(x.pow(2, mc), mc), mc), mc); } return x; }
From source file:Main.java
/** * Compute the square root of x to a given scale, x >= 0. Use Newton's * algorithm.//from w w w . j a v a2 s. c o m * * @param x * the value of x * @return the result value */ public static BigDecimal sqrt(BigDecimal x) { // Check that x >= 0. if (x.signum() < 0) { throw new ArithmeticException("x < 0"); } // n = x*(10^(2*SCALE)) BigInteger n = x.movePointRight(SCALE << 1).toBigInteger(); // The first approximation is the upper half of n. int bits = (n.bitLength() + 1) >> 1; BigInteger ix = n.shiftRight(bits); BigInteger ixPrev; // Loop until the approximations converge // (two successive approximations are equal after rounding). do { ixPrev = ix; // x = (x + n/x)/2 ix = ix.add(n.divide(ix)).shiftRight(1); Thread.yield(); } while (ix.compareTo(ixPrev) != 0); return new BigDecimal(ix, SCALE); }
From source file:mondrian.util.UtilCompatibleJdk15.java
/** * This generates a BigDecimal with a precision reflecting * the precision of the input double.//from w w w .j av a 2 s . c om * * @param d input double * @return BigDecimal */ public BigDecimal makeBigDecimalFromDouble(double d) { return new BigDecimal(d, MathContext.DECIMAL64); }
From source file:cherry.foundation.type.SecureBigDecimalTest.java
@Test public void testRandomTest() { for (int i = 0; i < 100; i++) { BigInteger bi = new BigInteger(RandomUtils.nextBytes(1024)); int scale = random.nextInt(); BigDecimal plain = new BigDecimal(bi, scale); SecureBigDecimal sec0 = plainValueOf(plain); SecureBigDecimal sec1 = cryptoValueOf(sec0.crypto()); assertThat(sec1.plain(), is(plain)); }/*from w ww.ja v a2s . c om*/ }
From source file:com.splicemachine.testutil.RandomDerbyDecimalBuilder.java
public static BigDecimal buildOne(int precision, int scale, boolean negative) { checkArgument(precision >= PRECISION_MIN && precision <= PRECISION_MAX); checkArgument(scale <= precision); BigInteger unscaledVal = new BigInteger((negative ? "-" : "") + RandomStringUtils.randomNumeric(precision)); return new BigDecimal(unscaledVal, scale); }