Example usage for java.math MathContext MathContext

List of usage examples for java.math MathContext MathContext

Introduction

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

Prototype

public MathContext(String val) 

Source Link

Document

Constructs a new MathContext from a string.

Usage

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

/**
 * Subtract and round according to the larger of the two ulps.
 *
 * @param x The left term.//from w  ww  .  j  av  a 2 s .  c  om
 * @param y The right term.
 * @return The difference x-y.
 */
static public BigDecimal subtractRound(final BigDecimal x, final BigDecimal y) {
    BigDecimal resul = x.subtract(y);
    /* The estimation of the absolute error in the result is |err(y)|+|err(x)|
     */

    double errR = Math.abs(y.ulp().doubleValue() / 2.) + Math.abs(x.ulp().doubleValue() / 2.);
    MathContext mc = new MathContext(err2prec(resul.doubleValue(), errR));

    return resul.round(mc);

}

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

/**
 * Multiply and round./*from  w  w w .  jav  a 2s. c  o 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.//w  w  w  . j  a v  a2 s  .c  o  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);

    }
}

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

/**
 * Multiply and round./*  w  w  w .j  a va2  s.  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.//w  w  w.  j  a va 2 s  .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 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

/**
 * Divide and round.//from  w  w  w.  j a v a  2s . c  o  m
 *
 * @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

/**
 * Divide and round.// ww  w. j  a va  2  s.co  m
 *
 * @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  a 2 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 2s  .c o m
 *
 * @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 2s  . c  o 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);

}