Example usage for java.math BigDecimal round

List of usage examples for java.math BigDecimal round

Introduction

In this page you can find the example usage for java.math BigDecimal round.

Prototype

public BigDecimal round(MathContext mc) 

Source Link

Document

Returns a BigDecimal rounded according to the MathContext settings.

Usage

From source file:org.nd4j.linalg.util.BigDecimalMath.java

/**
 * The hyperbolic cosine./*  w  ww  . ja  v a  2s  .c  om*/
 *
 * @param x The argument.
 * @return The cosh(x) = (exp(x)+exp(-x))/2 .
 */
static public BigDecimal cosh(final BigDecimal x) {
    if (x.compareTo(BigDecimal.ZERO) < 0) {
        return cos(x.negate());
    } else if (x.compareTo(BigDecimal.ZERO) == 0) {
        return BigDecimal.ONE;
    } else {
        if (x.doubleValue() > 1.5) {
            /* cosh^2(x) = 1+ sinh^2(x).
             */
            return hypot(1, sinh(x));

        } else {
            BigDecimal xhighpr = scalePrec(x, 2);
            /* Simple Taylor expansion, sum_{0=1..infinity} x^(2i)/(2i)! */
            BigDecimal resul = BigDecimal.ONE;
            /* x^i */
            BigDecimal xpowi = BigDecimal.ONE;
            /* 2i factorial */
            BigInteger ifac = BigInteger.ONE;
            /* The absolute error in the result is the error in x^2/2 which is x times the error in x.
             */

            double xUlpDbl = 0.5 * x.ulp().doubleValue() * x.doubleValue();
            /* The error in the result is set by the error in x^2/2 itself, xUlpDbl.
             * We need at most k terms to push x^(2k)/(2k)! below this value.
             * x^(2k) < xUlpDbl; (2k)*log(x) < log(xUlpDbl);
             */

            int k = (int) (Math.log(xUlpDbl) / Math.log(x.doubleValue())) / 2;
            /* The individual terms are all smaller than 1, so an estimate of 1.0 for
             * the absolute value will give a safe relative error estimate for the indivdual terms
             */
            MathContext mcTay = new MathContext(err2prec(1., xUlpDbl / k));

            for (int i = 1;; i++) {
                /* TBD: at which precision will 2*i-1 or 2*i overflow?
                 */
                ifac = ifac.multiply(new BigInteger("" + (2 * i - 1)));
                ifac = ifac.multiply(new BigInteger("" + (2 * i)));
                xpowi = xpowi.multiply(xhighpr).multiply(xhighpr);
                BigDecimal corr = xpowi.divide(new BigDecimal(ifac), mcTay);
                resul = resul.add(corr);

                if (corr.abs().doubleValue() < 0.5 * xUlpDbl) {
                    break;
                }

            } /* The error in the result is governed by the error in x itself.
              */
            MathContext mc = new MathContext(err2prec(resul.doubleValue(), xUlpDbl));

            return resul.round(mc);

        }
    }
}

From source file:org.nd4j.linalg.util.BigDecimalMath.java

/**
 * The inverse trigonometric sine./*from   w  ww.j  a  v a  2s. c  o  m*/
 *
 * @param x the argument.
 * @return the arcsin(x) in radians.
 */
static public BigDecimal asin(final BigDecimal x) {
    if (x.compareTo(BigDecimal.ONE) > 0 || x.compareTo(BigDecimal.ONE.negate()) < 0) {
        throw new ArithmeticException("Out of range argument " + x.toString() + " of asin");

    } else if (x.compareTo(BigDecimal.ZERO) == 0) {
        return BigDecimal.ZERO;
    } else if (x.compareTo(BigDecimal.ONE) == 0) {
        /* arcsin(1) = pi/2
         */
        double errpi = Math.sqrt(x.ulp().doubleValue());
        MathContext mc = new MathContext(err2prec(3.14159, errpi));

        return pi(mc).divide(new BigDecimal(2));

    } else if (x.compareTo(BigDecimal.ZERO) < 0) {
        return asin(x.negate()).negate();

    } else if (x.doubleValue() > 0.7) {
        final BigDecimal xCompl = BigDecimal.ONE.subtract(x);
        final double xDbl = x.doubleValue();
        final double xUlpDbl = x.ulp().doubleValue() / 2.;
        final double eps = xUlpDbl / 2. / Math.sqrt(1. - Math.pow(xDbl, 2.));

        final BigDecimal xhighpr = scalePrec(xCompl, 3);
        final BigDecimal xhighprV = divideRound(xhighpr, 4);
        BigDecimal resul = BigDecimal.ONE;
        /* x^(2i+1) */
        BigDecimal xpowi = BigDecimal.ONE;
        /* i factorial */
        BigInteger ifacN = BigInteger.ONE;
        BigInteger ifacD = BigInteger.ONE;

        for (int i = 1;; i++) {
            ifacN = ifacN.multiply(new BigInteger("" + (2 * i - 1)));
            ifacD = ifacD.multiply(new BigInteger("" + i));

            if (i == 1) {
                xpowi = xhighprV;
            } else {
                xpowi = multiplyRound(xpowi, xhighprV);
            }
            BigDecimal c = divideRound(multiplyRound(xpowi, ifacN),
                    ifacD.multiply(new BigInteger("" + (2 * i + 1))));
            resul = resul.add(c);
            /* series started 1+x/12+... which yields an estimate of the sums error
             */

            if (Math.abs(c.doubleValue()) < xUlpDbl / 120.) {
                break;
            }

        }
        /* sqrt(2*z)*(1+...)
         */
        xpowi = sqrt(xhighpr.multiply(new BigDecimal(2)));
        resul = multiplyRound(xpowi, resul);
        MathContext mc = new MathContext(resul.precision());
        BigDecimal pihalf = pi(mc).divide(new BigDecimal(2));
        mc = new MathContext(err2prec(resul.doubleValue(), eps));

        return pihalf.subtract(resul, mc);

    } else {
        /* absolute error in the result is err(x)/sqrt(1-x^2) to lowest order
         */
        final double xDbl = x.doubleValue();
        final double xUlpDbl = x.ulp().doubleValue() / 2.;
        final double eps = xUlpDbl / 2. / Math.sqrt(1. - Math.pow(xDbl, 2.));
        final BigDecimal xhighpr = scalePrec(x, 2);
        final BigDecimal xhighprSq = multiplyRound(xhighpr, xhighpr);
        BigDecimal resul = xhighpr.plus();
        /* x^(2i+1) */
        BigDecimal xpowi = xhighpr;
        /* i factorial */
        BigInteger ifacN = BigInteger.ONE;
        BigInteger ifacD = BigInteger.ONE;

        for (int i = 1;; i++) {
            ifacN = ifacN.multiply(new BigInteger("" + (2 * i - 1)));
            ifacD = ifacD.multiply(new BigInteger("" + (2 * i)));
            xpowi = multiplyRound(xpowi, xhighprSq);
            BigDecimal c = divideRound(multiplyRound(xpowi, ifacN),
                    ifacD.multiply(new BigInteger("" + (2 * i + 1))));
            resul = resul.add(c);

            if (Math.abs(c.doubleValue()) < 0.1 * eps) {
                break;
            }

        }
        MathContext mc = new MathContext(err2prec(resul.doubleValue(), eps));

        return resul.round(mc);

    }
}