Example usage for java.math BigDecimal precision

List of usage examples for java.math BigDecimal precision

Introduction

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

Prototype

int precision

To view the source code for java.math BigDecimal precision.

Click Source Link

Document

The number of decimal digits in this BigDecimal, or 0 if the number of digits are not known (lookaside information).

Usage

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

/**
 * Divide and round.//from  www  . j  ava2  s.  c om
 *
 * @param n The numerator
 * @param x The denominator
 * @return the divided n/x
 */
static public BigDecimal divideRound(final BigInteger n, final BigDecimal x) {
    /* The estimation of the relative error in the result is |err(x)/x|
     */
    MathContext mc = new MathContext(x.precision());

    return new BigDecimal(n).divide(x, mc);

}

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

/**
 * Divide and round.//from   www . j  a  v  a 2 s .co  m
 *
 * @param n The numerator.
 * @param x The denominator.
 * @return the divided n/x.
 */
static public BigDecimal divideRound(final int n, final BigDecimal x) {
    /* The estimation of the relative error in the result is |err(x)/x|
     */
    MathContext mc = new MathContext(x.precision());

    return new BigDecimal(n).divide(x, mc);

}

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

/**
 * Divide and round./*from w  ww.j a  v a  2s  .  c om*/
 *
 * @param x The numerator
 * @param n The denominator
 * @return the divided x/n
 */
static public BigDecimal divideRound(final BigDecimal x, final int n) {
    /* The estimation of the relative error in the result is |err(x)/x|
     */
    MathContext mc = new MathContext(x.precision());

    return x.divide(new BigDecimal(n), mc);

}

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

/**
 * Divide and round.//  w w  w .  j  a  v a2 s.c o m
 *
 * @param x The numerator
 * @param n The denominator
 * @return the divided x/n
 */
static public BigDecimal divideRound(final BigDecimal x, final BigInteger n) {
    /* The estimation of the relative error in the result is |err(x)/x|
     */
    MathContext mc = new MathContext(x.precision());

    return x.divide(new BigDecimal(n), mc);

}

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

/**
 * Divide and round./*from w  w w.j a v  a  2  s  .c om*/
 *
 * @param x The numerator
 * @param y The denominator
 * @return the divided x/y
 */
static public BigDecimal divideRound(final BigDecimal x, final BigDecimal y) {
    /* The estimation of the relative error in the result is |err(y)/y|+|err(x)/x|
     */
    MathContext mc = new MathContext(Math.min(x.precision(), y.precision()));

    return x.divide(y, mc);

}

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

/**
 * Multiply and round./*from   www. j  a v  a  2s .  c o  m*/
 *
 * @param x The left factor.
 * @param n The right factor.
 * @return The product x*n.
 */
static public BigDecimal multiplyRound(final BigDecimal x, final int n) {
    BigDecimal resul = x.multiply(new BigDecimal(n));
    /* The estimation of the absolute error in the result is |n*err(x)|
     */
    MathContext mc = new MathContext(n != 0 ? x.precision() : 0);

    return resul.round(mc);

}

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

/**
 * Multiply and round.//from   w  w w  .  j a v a2s  .c  om
 *
 * @param x The left factor.
 * @param n The right factor.
 * @return the product x*n
 */
static public BigDecimal multiplyRound(final BigDecimal x, final BigInteger n) {
    BigDecimal resul = x.multiply(new BigDecimal(n));
    /* The estimation of the absolute error in the result is |n*err(x)|
     */
    MathContext mc = new MathContext(n.compareTo(BigInteger.ZERO) != 0 ? x.precision() : 0);

    return resul.round(mc);

}

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

/**
 * Raise to an integer power and round.//from   ww  w .j a va 2s  . co  m
 *
 * @param x The base.
 * @param n The exponent.
 * @return x^n.
 */
static public BigDecimal powRound(final BigDecimal x, final int n) {
    /* The relative error in the result is n times the relative error in the input.
     * The estimation is slightly optimistic due to the integer rounding of the logarithm.
     */
    MathContext mc = new MathContext(x.precision() - (int) Math.log10((double) (Math.abs(n))));
    return x.pow(n, mc);
}

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

/**
 * Multiply and round.//  w  w  w .j  a  v a  2 s.co m
 *
 * @param x The left factor.
 * @param y The right factor.
 * @return The product x*y.
 */
static public BigDecimal multiplyRound(final BigDecimal x, final BigDecimal y) {
    BigDecimal resul = x.multiply(y);

    /* The estimation of the relative error in the result is the sum of the relative
     * errors |err(y)/y|+|err(x)/x|
     */
    MathContext mc = new MathContext(Math.min(x.precision(), y.precision()));

    return resul.round(mc);

}

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

/**
 * Multiply and round./*from  w  ww .j ava  2  s.  co m*/
 *
 * @param x The left factor.
 * @param f The right factor.
 * @return The product x*f.
 */
static public BigDecimal multiplyRound(final BigDecimal x, final Rational f) {
    if (f.compareTo(BigInteger.ZERO) == 0) {
        return BigDecimal.ZERO;
    } else {
        /* Convert the rational value with two digits of extra precision
         */
        MathContext mc = new MathContext(2 + x.precision());
        BigDecimal fbd = f.BigDecimalValue(mc);
        /* and the precision of the product is then dominated by the precision in x
         */

        return multiplyRound(x, fbd);

    }
}