Package java.math

Examples of java.math.MathContext


            /*
             * The absolute error in arcsinh x is err(x)/sqrt(1+x^2)
             */
            double xDbl = x.doubleValue();
            double eps = 0.5 * x.ulp().doubleValue() / Math.hypot(1., xDbl);
            MathContext mc = new MathContext(err2prec(logx.doubleValue(), eps));
            return logx.round(mc);
        }
    } /* BigDecimalMath.asinh */
 
View Full Code Here


            /*
             * The absolute error in arcsinh x is err(x)/sqrt(x^2-1)
             */
            double xDbl = x.doubleValue();
            double eps = 0.5 * x.ulp().doubleValue() / Math.sqrt(xDbl * xDbl - 1.);
            MathContext mc = new MathContext(err2prec(logx.doubleValue(), eps));
            return logx.round(mc);
        }
    } /* BigDecimalMath.acosh */
 
View Full Code Here

            /*
             * add intermediately 2 digits to the partial sum accumulation
             */
            z = scalePrec(z, 2);
            MathContext mcloc = new MathContext(z.precision());

            /*
             * measure of the absolute error is the relative error in the first, logarithmic term
             */
            double eps = x.ulp().doubleValue() / x.doubleValue();

            BigDecimal resul = log(scalePrec(x, 2)).negate();

            if (x.compareTo(BigDecimal.ONE) != 0) {

                BigDecimal gammCompl = BigDecimal.ONE.subtract(gamma(mcloc));
                resul = resul.add(multiplyRound(z, gammCompl));
                Bernoulli bern_cache=new Bernoulli();
                Factorial fact_cache=new Factorial();
                for (int n = 2; ; n++) {
                    /* multiplying z^n/n by zeta(n-1) means that the two relative errors add.
                     * so the requirement in the relative error of zeta(n)-1 is that this is somewhat
                     * smaller than the relative error in z^n/n (the absolute error of thelatter  is the
                     * absolute error in z)
                     */
                    BigDecimal c = divideRound(z.pow(n, mcloc), n);
                    MathContext m = new MathContext(err2prec(n * z.ulp().doubleValue() / 2. / z.doubleValue()));
                    c = c.round(m);

                    /* At larger n, zeta(n)-1 is roughly 1/2^n. The product is c/2^n.
                     * The relative error in c is c.ulp/2/c . The error in the product should be small versus eps/10.
                     * Error from 1/2^n is c*err(sigma-1).
                     * We need a relative error of zeta-1 of the order of c.ulp/50/c. This is an absolute
                     * error in zeta-1 of c.ulp/50/c/2^n, and also the absolute error in zeta, because zeta is
                     * of the order of 1.
                     */
                    if (eps / 100. / c.doubleValue() < 0.01)
                        m = new MathContext(err2prec(eps / 100. / c.doubleValue()));
                    else
                        m = new MathContext(2);
                    /* zeta(n) -1 */
                    BigDecimal zetm1 = zeta(n, m,bern_cache,fact_cache).subtract(BigDecimal.ONE);
                    c = multiplyRound(c, zetm1);

                    if (n % 2 == 0)
                        resul = resul.add(c);
                    else
                        resul = resul.subtract(c);

                    /* alternating sum, so truncating as eps is reached suffices
                                        */
                    if (Math.abs(c.doubleValue()) < eps)
                        break;
                }
            }

            /* The relative error in the result is the absolute error in the
             * input variable times the digamma (psi) value at that point.
             */
            double zdbl = z.doubleValue();
            eps = psi(zdbl) * x.ulp().doubleValue() / 2.;
            mcloc = new MathContext(err2prec(eps));
            return exp(resul).round(mcloc);
        }
    } /* BigDecimalMath.gamma */
 
View Full Code Here

             * that this is equivalent to mc: Delta(x) = precision/psi(x).
             */
            double qdbl = q.doubleValue();
            double deltx = 5. * Math.pow(10., -mc.getPrecision()) / psi(qdbl);

            MathContext mcx = new MathContext(err2prec(qdbl, deltx));
            BigDecimal x = q.BigDecimalValue(mcx);

            /* forward calculation to the general floating point case */
            return Gamma(x);
        }
View Full Code Here

             */
            double eps = 0.5 * xUlpDbl / Math.abs(xDbl);
            for (int i = 1; i < n; i++) {
                eps += 0.5 * xUlpDbl / Math.abs(xDbl + i);
                resul = resul.multiply(xhighpr.add(new BigDecimal(i)));
                final MathContext mcloc = new MathContext(4 + err2prec(eps));
                resul = resul.round(mcloc);
            }
            return resul.round(new MathContext(err2prec(eps)));
        }
    } /* BigDecimalMath.pochhammer */
 
View Full Code Here

        double err2pi;
        if (k != 0)
            err2pi = 0.25 * Math.abs(x.ulp().doubleValue() / k);
        else
            err2pi = 0.5 * Math.abs(x.ulp().doubleValue());
        MathContext mc = new MathContext(2 + err2prec(6.283, err2pi));
        BigDecimal twopi = pi(mc).multiply(new BigDecimal(2));

        /* Delegate the actual operation to the BigDecimal class, which may return
         * a negative value of x was negative .
         */
        BigDecimal res = x.remainder(twopi);
        if (res.compareTo(BigDecimal.ZERO) < 0)
            res = res.add(twopi);

        /*
         * The actual precision is set by the input value, its absolute value of x.ulp()/2.
         */
        mc = new MathContext(err2prec(res.doubleValue(), x.ulp().doubleValue() / 2.));
        return res.round(mc);
    } /* mod2pi */
 
View Full Code Here

        double errpi;
        if (k != 0)
            errpi = 0.5 * Math.abs(x.ulp().doubleValue() / k);
        else
            errpi = 0.5 * Math.abs(x.ulp().doubleValue());
        MathContext mc = new MathContext(2 + err2prec(3.1416, errpi));
        BigDecimal onepi = pi(mc);
        BigDecimal pihalf = onepi.divide(new BigDecimal(2));

        /* Delegate the actual operation to the BigDecimal class, which may return
         * a negative value of x was negative .
         */
        BigDecimal res = x.remainder(onepi);
        if (res.compareTo(pihalf) > 0)
            res = res.subtract(onepi);
        else if (res.compareTo(pihalf.negate()) < 0)
            res = res.add(onepi);

        /*
         * The actual precision is set by the input value, its absolute value of x.ulp()/2.
         */
        mc = new MathContext(err2prec(res.doubleValue(), x.ulp().doubleValue() / 2.));
        return res.round(mc);
    } /* modpi */
 
View Full Code Here

     * Optimizations needed... too long!!!... (memoization???)
     *
     */
    static public BigDecimal zeta(final int n, final MathContext mc, Bernoulli bern_cache, Factorial fact_cache) {
       
        MathContext nmc=new MathContext(3+mc.getPrecision(),mc.getRoundingMode());
       
        if (n <= 0)
            throw new ProviderException("Not implemented: zeta at negative argument " + n);
        if (n == 1)
            throw new ArithmeticException("Pole at zeta(1) ");

        if (n % 2 == 0) {
            /*
             * Even indices. Abramowitz-Stegun 23.2.16. Start with 2^(n-1)*B(n)/n!
             */
            //System.out.print("'");
            Rational b = (bern_cache!=null ? bern_cache : new Bernoulli()).at(n).abs();
            b = b.divide((fact_cache!=null ? fact_cache : new Factorial()).at(n));
            b = b.multiply(BigInteger.ONE.shiftLeft(n - 1));
            //System.out.print("'");

            /* to be multiplied by pi^n. Absolute error in the result of pi^n is n times
             * error in pi times pi^(n-1). Relative error is n*error(pi)/pi, requested by mc.
             * Need one more digit in pi if n=10, two digits if n=100 etc, and add one extra digit.
             */
            MathContext mcpi = new MathContext(mc.getPrecision() + (int) (Math.log10(10.0 * n)));
            BigDecimal piton = pi(mcpi).pow(n, mc);
            return piton.multiply(b.BigDecimalValue(mc),mc);
        }
        else if (n == 3) {
            /* Broadhurst BBP <a href="http://arxiv.org/abs/math/9803067">arXiv:math/9803067</a>
             * Error propagation: S31 is roughly 0.087, S33 roughly 0.131
             */
            int[] a31 = { 1,
                -7,
                -1,
                10,
                -1,
                -7,
                1,
                0 };
            int[] a33 = { 1,
                1,
                -1,
                -2,
                -1,
                1,
                1,
                0 };
            BigDecimal S31 = broadhurstBBP(3, 1, a31, mc);
            BigDecimal S33 = broadhurstBBP(3, 3, a33, mc);
            S31 = S31.multiply(new BigDecimal(48),nmc);
            S33 = S33.multiply(new BigDecimal(32),nmc);
            return S31.add(S33).divide(new BigDecimal(7), mc);
        }
        else if (n == 5) {
            /* Broadhurst BBP <a href=http://arxiv.org/abs/math/9803067">arXiv:math/9803067</a>
             * Error propagation: S51 is roughly -11.15, S53 roughly 22.165, S55 is roughly 0.031
             * 9*2048*S51/6265 = -3.28. 7*2038*S53/61651= 5.07. 738*2048*S55/61651= 0.747.
             * The result is of the order 1.03, so we add 2 digits to S51 and S52 and one digit to S55.
             */
            int[] a51 = { 31,
                -1614,
                -31,
                -6212,
                -31,
                -1614,
                31,
                74552 };
            int[] a53 = { 173,
                284,
                -173,
                -457,
                -173,
                284,
                173,
                -111 };
            int[] a55 = { 1,
                0,
                -1,
                -1,
                -1,
                0,
                1,
                1 };
            BigDecimal S51 = broadhurstBBP(5, 1, a51, new MathContext(2 + mc.getPrecision()));
            BigDecimal S53 = broadhurstBBP(5, 3, a53, new MathContext(2 + mc.getPrecision()));
            BigDecimal S55 = broadhurstBBP(5, 5, a55, new MathContext(1 + mc.getPrecision()));
            S51 = S51.multiply(new BigDecimal(18432),nmc);
            S53 = S53.multiply(new BigDecimal(14336),nmc);
            S55 = S55.multiply(new BigDecimal(1511424),nmc);
            return S51.add(S53).subtract(S55).divide(new BigDecimal(62651), mc);
        }
        else {
            /* Cohen et al Exp Math 1 (1) (1992) 25
                        */
            //System.out.print("'");
            Rational betsum = new Rational();
            Bernoulli bern = bern_cache!=null ? bern_cache : new Bernoulli();
            Factorial fact = fact_cache!=null ? fact_cache : new Factorial();
            for (int npr = 0; npr <= (n + 1) / 2; npr++) {
                Rational b = bern.at(2 * npr).multiply(bern.at(n + 1 - 2 * npr));
                b = b.divide(fact.at(2 * npr)).divide(fact.at(n + 1 - 2 * npr));
                b = b.multiply(1 - 2 * npr);
                if (npr % 2 == 0)
                    betsum = betsum.add(b);
                else
                    betsum = betsum.subtract(b);
            }
            //System.out.print("'");
            betsum = betsum.divide(n - 1);
            /* The first term, including the facor (2pi)^n, is essentially most
             * of the result, near one. The second term below is roughly in the range 0.003 to 0.009.
             * So the precision here is matching the precisionn requested by mc, and the precision
             * requested for 2*pi is in absolute terms adjusted.
             */
            MathContext mcloc = new MathContext(2 + mc.getPrecision() + (int) (Math.log10((double) (n))));
            BigDecimal ftrm = pi(mcloc).multiply(TWO,mcloc);
            ftrm = ftrm.pow(n,mcloc);
            ftrm = ftrm.multiply(betsum.BigDecimalValue(mcloc),mcloc);
            BigDecimal exps = new BigDecimal(0);

View Full Code Here

        else {
            /* Result is roughly 2^(-n), desired accuracy 18 digits. If zeta(n) is computed, the equivalent accuracy
             * in relative units is higher, because zeta is around 1.
             */
            double eps = 1.e-18 * Math.pow(2., (double) (-n));
            MathContext mc = new MathContext(err2prec(eps));
            Bernoulli bern_cache=new Bernoulli();
            Factorial fact_cache=new Factorial();
            return zeta(n, mc,bern_cache,fact_cache).subtract(BigDecimal.ONE).doubleValue();
        }
    } /* zeta */
 
View Full Code Here

        //int kmax = (int) (6.6 * mc.getPrecision() / p); // removed by l.bruninx, 2012-03-22

        /* Now eps is the absolute error in each term */
        //eps /= kmax; // removed by l.bruninx, 2012-03-22
        BigDecimal res = BigDecimal.ZERO;
        MathContext nmc=_mc_plus2_(mc);
        BigDecimal oldres=res;     // modoiied by l.bruninx, 2012-03-22
        int c = 0;                 // modoiied by l.bruninx, 2012-03-22
        do{                        // modoiied by l.bruninx, 2012-03-22
            oldres=res.round(mc)// modoiied by l.bruninx, 2012-03-22
            Rational r = new Rational();
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.