Package java.math

Examples of java.math.MathContext


        else {
            /*
             * return x^y = exp(y*log(x)) ;
             */
           
            MathContext nmc=_mc_adjust_pow_(x,y,mc);
            BigDecimal logx = log(x, nmc);
            BigDecimal ylogx = y.multiply(logx);
            ylogx=exp(ylogx,nmc);
            return _dec_round_(ylogx,mc);
        }
View Full Code Here


        else {
            /* 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.
             * Since the standard BigDecimal.pow can only handle positive n, we split the algorithm.
             */
            MathContext mc = new MathContext(x.precision() - (int) Math.log10((double) (Math.abs(n))));
            if (n > 0)
                return x.pow(n, mc);
            else
                return BigDecimal.ONE.divide(x.pow(-n), mc);
        }
View Full Code Here

                         * will do.
                         */
                        eps = nu.divide(de, MathContext.DECIMAL32);
                    }
                    else {
                        MathContext mc = new MathContext(precDiv);
                        eps = nu.divide(de, mc);
                    }

                    res = subtractRound(res, eps);
                    /* reached final precision if the relative error fell below reserr,
                     * |eps/res| < reserr
                     */
                    if (eps.divide(res, MathContext.DECIMAL64).abs().compareTo(reserr) < 0) {
                        /* delete the bits of extra precision kept in this
                         * working copy.
                         */
                        MathContext mc = new MathContext(err2prec(reserr.doubleValue()));
                        return res.round(mc);
                    }
                }
            }
            else {
                /* The error in x^q is q*x^(q-1)*Delta(x) + Delta(q)*x^q*log(x).
                 * The relative error is q/x*Delta(x) + Delta(q)*log(x). Convert q to a floating point
                 * number such that its relative error becomes negligible: Delta(q)/q << Delta(x)/x/log(x) .
                 */
                int precq = 3 + err2prec((x.ulp().divide(x, MathContext.DECIMAL64)).doubleValue() / Math.log(x.doubleValue()));
                MathContext mc = new MathContext(precq);

                /*
                 * Perform the actual calculation as exponentiation of two floating point numbers.
                 */
                return pow(x, q.BigDecimalValue(mc), mc);
View Full Code Here

             *
             */

            BigDecimal resx = BigDecimal.ZERO;
            BigDecimal oldx = resx;
            MathContext nmc = _mc_adjust_(res,_mc_plus2_(mc)); // more precision (the integer part include by _mc_adjust_)...
            int k = 0;
            do {
                oldx = _dec_round_(resx,mc);                   // auto-adaptative algo...
                for (int z = 2; z > 0; z--) {
                    int k2p1 = 2 * k + 1;
View Full Code Here

        else {
            /*
             * reduce modulo 2pi
             */
            BigDecimal res = mod2pi(x);
            MathContext nmc = _mc_adjust_(res,_mc_plus2_(mc)); // more precision (the integer part include by _mc_adjust_)...
            BigDecimal p = pi(nmc);

            /*
             * Simple Taylor expansion, sum_{i=1..infinity} (-1)^(..)res^(2i+1)/(2i+1)!
             *
 
View Full Code Here

             * the java.lang.IllegalArgumentException: Digits < 0 Errors encountered with the original code.
             *
             * Use tan{x} = sin{x} / cos{x}
             *
             */
            MathContext nmc = _mc_adjust_(x,_mc_plus2_(mc)); // more precision (the integer part include by _mc_adjust_)...
            BigDecimal r=sin(x, nmc).divide(cos(x, nmc),nmc);
            return _dec_round_(r,mc);

        }
    } /* BigDecimalMath.tan */
 
View Full Code Here

            final double eps = xUlpDbl / 2. / Math.pow(Math.sin(xDbl), 2.);

            final BigDecimal xhighpr = scalePrec(res, 2);
            final BigDecimal xhighprSq = multiplyRound(xhighpr, xhighpr);

            MathContext mc = new MathContext(err2prec(xhighpr.doubleValue(), eps));
            BigDecimal resul = BigDecimal.ONE.divide(xhighpr, mc);

            /* x^(2i-1) */
            BigDecimal xpowi = xhighpr;

            Bernoulli b = new Bernoulli();

            /* 2^(2i) */
            BigInteger fourn = new BigInteger("4");
            /* (2i)! */
            BigInteger fac = BigInteger.ONE;

            for (int i = 1; ; i++) {
                Rational f = b.at(2 * i);
                fac = fac.multiply(new BigInteger("" + (2 * i))).multiply(new BigInteger("" + (2 * i - 1)));
                f = f.multiply(fourn).divide(fac);
                BigDecimal c = multiplyRound(xpowi, f);
                if (i % 2 == 0)
                    resul = resul.add(c);
                else
                    resul = resul.subtract(c);
                if (Math.abs(c.doubleValue()) < 0.1 * eps)
                    break;

                fourn = fourn.shiftLeft(2);
                xpowi = multiplyRound(xpowi, xhighprSq);
            }
            mc = new MathContext(err2prec(resul.doubleValue(), eps));
            return resul.round(mc);
        }
    } /* BigDecimalMath.cot */
 
View Full Code Here

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

                 * We need at most k terms to squeeze x^(2k+1)/(2k+1)! below this value.
                 * x^(2k+1) < x.ulp; (2k+1)*log10(x) < -x.precision; 2k*log10(x)< -x.precision;
                 * 2k*(-log10(x)) > x.precision; 2k*log10(1/x) > x.precision
                 */
                int k = (int) (x.precision() / Math.log10(1.0 / xhighpr.doubleValue())) / 2;
                MathContext mcTay = new MathContext(err2prec(x.doubleValue(), xUlpDbl / k));
                for (int i = 1; ; i++) {
                    /* TBD: at which precision will 2*i or 2*i+1 overflow?
                                        */
                    ifac = ifac.multiply(new BigInteger("" + (2 * i)));
                    ifac = ifac.multiply(new BigInteger("" + (2 * i + 1)));
View Full Code Here

            /*
             * The error in tanh x is err(x)/cosh^2(x).
             */
            double eps = 0.5 * x.ulp().doubleValue() / Math.pow(Math.cosh(x.doubleValue()), 2.0);
            MathContext mc = new MathContext(err2prec(Math.tanh(x.doubleValue()), eps));
            return BigDecimal.ONE.subtract(exp2x).divide(BigDecimal.ONE.add(exp2x), mc);
        }
    } /* BigDecimalMath.tanh */
 
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.