Package java.math

Examples of java.math.MathContext


        BigDecimal resul = x.add(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);
    } /* addRound */
 
View Full Code Here


        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);
    } /* subtractRound */
 
View Full Code Here

    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);
    } /* multiplyRound */
 
View Full Code Here

            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
             */
 
View Full Code Here

    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);
    }
View Full Code Here

    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);
    }
View Full Code Here

     */
    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()));
        BigDecimal resul = x.divide(y, mc);
        /* If x and y are precise integer values that may have common factors,
         * the method above will truncate trailing zeros, which may result in
         * a smaller apparent accuracy than starte... add missing trailing zeros now.
         */
 
View Full Code Here

    static public BigComplex invertRound(final BigComplex z) {
        if (z.im.compareTo(BigDecimal.ZERO) == 0) {
            /*
             * In this case with vanishing Im(x), the result is  simply 1/Re z.
             */
            final MathContext mc = new MathContext(z.re.precision());
            return new BigComplex(BigDecimal.ONE.divide(z.re, mc));
        }
        else if (z.re.compareTo(BigDecimal.ZERO) == 0) {
            /*
             * In this case with vanishing Re(z), the result is  simply -i/Im z
             */
            final MathContext mc = new MathContext(z.im.precision());
            return new BigComplex(BigDecimal.ZERO, BigDecimal.ONE.divide(z.im, mc).negate());
        }
        else {
            /*
             * 1/(x.re+I*x.im) = 1/(x.re+x.im^2/x.re) - I /(x.im +x.re^2/x.im)
             */
            BigDecimal R = addRound(z.re, divideRound(multiplyRound(z.im, z.im), z.re));
            BigDecimal I = addRound(z.im, divideRound(multiplyRound(z.re, z.re), z.im));
            MathContext mc = new MathContext(1 + R.precision());
            R = BigDecimal.ONE.divide(R, mc);
            mc = new MathContext(1 + I.precision());
            I = BigDecimal.ONE.divide(I, mc);
            return new BigComplex(R, I.negate());
        }
    }
View Full Code Here

     */
    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);
    }
View Full Code Here

     */
    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);
    } /* divideRound */
 
View Full Code Here

TOP

Related Classes of java.math.MathContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.