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:com.cecs492a_group4.sp.SingleEvent.java

public synchronized void getYelpSearchResult(final int index1, final String searchToken, final String Address)
        throws InterruptedException {
    Thread thread = new Thread(new Runnable() {
        @Override/*from   w  w  w  .ja  v  a  2s . co  m*/
        public void run() {
            int randPick = ran.nextInt(limit);
            try {
                String name = "ldkfldsjkfd";
                int full = 0;
                do {

                    randPick = ran.nextInt(limit);
                    System.out.println("Event number : " + index1);
                    System.out.println(randPick);
                    System.out.println("Index: " + randPick);
                    response = yelp.searchByLocation(searchToken, Address, SingleEvent.currentRadius);
                    System.out.println("The current radius : " + SingleEvent.currentRadius);
                    System.out.println(searchToken + " gave me this response: " + response);
                    yp.setResponse(response);
                    // yp.parseBusiness();

                    int nameSIndex = response.indexOf("\"name\"", 1) + 8;

                    for (int i = 0; i < randPick; i++) {
                        nameSIndex = response.indexOf("\"name\"", ++nameSIndex) + 8;
                    }
                    int nameEIndex = response.indexOf("\",", ++nameSIndex);

                    String tmp = response;
                    name = tmp.substring(nameSIndex, nameEIndex);
                    System.out.println(name);
                    full++;
                    if (full >= 4) {
                        full = 0;
                        bussinessNames.clear(); //no more options reset array
                    }
                } while (bussinessNames.contains(name));

                try {
                    bussinessNames.add(index1, name);
                } catch (IndexOutOfBoundsException e) {
                    System.out.println("Array size " + bussinessNames.size());
                    bussinessNames.clear();
                }
                int imgSIndex = response.indexOf("\"image_url\"", 1) + 13;
                int ratingSIndex = response.indexOf("\"rating_img_url\"", 1) + 18;
                int urlSIndex = response.indexOf("\"mobile_url\"", 1) + 14;
                int phoneSIndex = response.indexOf("\"phone\":", 1) + 9;
                int addressSIndex = response.indexOf("\"display_address\"", 1) + 19;
                int distanceSIndex = response.indexOf("\"distance\"", 1) + 12;
                System.out.println("Start index :" + distanceSIndex);
                for (int i = 0; i < randPick; i++) {

                    imgSIndex = response.indexOf("\"image_url\"", ++imgSIndex) + 13;
                    ratingSIndex = response.indexOf("\"rating_img_url\"", ++ratingSIndex) + 18;
                    urlSIndex = response.indexOf("\"mobile_url\"", ++urlSIndex) + 14;
                    phoneSIndex = response.indexOf("\"phone\":", ++phoneSIndex) + 9;
                    addressSIndex = response.indexOf("\"display_address\"", ++addressSIndex) + 19;
                    distanceSIndex = response.indexOf("\"distance\"", ++distanceSIndex) + 12;
                }

                int ratingEIndex = response.indexOf("g\"", ++ratingSIndex) + 1;
                int imgEIndex = response.indexOf("g\"", ++imgSIndex) + 1;
                int phoneEIndex = response.indexOf("\",", ++phoneSIndex);
                int urlEIndex = response.indexOf("rating_img_url", ++urlSIndex) - 4;
                int addressEIndex = response.indexOf("\"], ", ++addressSIndex) + 1;

                //System.out.println("distance = " + response.substring(distanceSIndex,distanceSIndex + 9));
                //String distance = yp.getBusinessDistance(randPick);
                String distance = response.substring(distanceSIndex, distanceSIndex + 9);
                System.out.println("Distance: " + distance);
                double dis;
                try {
                    dis = Double.parseDouble(distance);
                } catch (NumberFormatException e) {
                    dis = 0.0;
                }
                System.out.println("Distance in meters:" + dis);
                System.out.println("Distance in miles:" + meters_to_miles(dis));
                BigDecimal bd = new BigDecimal(meters_to_miles(dis));
                bd = bd.round(new MathContext(2));
                dis = bd.doubleValue();
                System.out.println("dis after conversion " + dis);
                //distance = distance.substring(distanceSIndex,distanceEIndex);

                //activity = yp.getBusinessName(randPick);
                activity = name;
                //rating = yp.getBusinessRating(randPick);
                //I am going to parse the url my self fucking yelp!

                // int imgEIndex = response.indexOf("location",imgSIndex) - 4;
                String phoneNumber = response;
                phoneNumber = phoneNumber.substring(phoneSIndex, phoneEIndex);
                System.out.println("Phone number: " + phoneNumber);
                String tmp2 = response;
                tmp2 = tmp2.substring(imgSIndex, imgEIndex);
                System.out.println("mylink " + tmp2);
                //img_url = yp.getBusinessImageURL(randPick);
                img_url = tmp2;
                System.out.println(img_url);
                //rating_url = yp.getBusinessRatingUrl(randPick);
                String ratingURL = response;
                ratingURL = ratingURL.substring(ratingSIndex, ratingEIndex);
                System.out.println(ratingURL);

                String weburl = response;
                weburl = weburl.substring(urlSIndex, urlEIndex);
                System.out.println("Event URL: " + weburl);

                String eventaddress = response;
                eventaddress = eventaddress.substring(addressSIndex, addressEIndex);

                System.out.println("default Event Address" + eventaddress);

                int streaddSindex = eventaddress.indexOf("\"") + 1;

                int streaddEindex = eventaddress.indexOf("\",");
                int cityaddSindex = 1;
                try {
                    cityaddSindex = eventaddress.indexOf("\", \"") + 4;
                } catch (StringIndexOutOfBoundsException e) {
                    cityaddSindex = 0;
                    System.out.println("No city");
                }

                String streetadd = " ";
                try {
                    streetadd = eventaddress.substring(streaddSindex, streaddEindex);
                } catch (StringIndexOutOfBoundsException e) {
                    if (!Character.isDigit(eventaddress.charAt(0))) {
                        System.out.println("Faulty address");
                        streetadd = " ";
                    }
                }
                System.out.println("Street address:" + streetadd);
                String cityadd;
                if (cityaddSindex != 0)
                    cityadd = eventaddress.substring(cityaddSindex).replace("\"", "");
                else
                    cityadd = " ";

                System.out.println("City location: " + cityadd);
                eventaddress = streetadd + " " + cityadd;
                System.out.println("adjusted Event Address: " + eventaddress);

                //System.out.println(ratingURL);
                event_address = eventaddress;
                phoneNum = phoneNumber;
                web_url = weburl;
                rating_url = ratingURL;
                icon_url = new URL(img_url);
                url_rating = new URL(rating_url);
                try {
                    dayevent.add(index1, new DayEvent(activity, icon_url, url_rating, searchToken, dis, web_url,
                            phoneNum, event_address));
                } catch (IndexOutOfBoundsException e) {
                    System.out.println("Size of day event " + dayevent.size());
                }
                //} catch (JSONException e) {
                //e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    });
    thread.start();
    //test by removing the while loops (bad practice)
    /**       while (response == null) {
           }
           while (activity == null) {
           }
           while (web_url == null) {
           }
           while (img_url == null) {
           }
           while (rating_url == null) {
           }**/
    thread.join();

}

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

/**
 * The natural logarithm./*w w  w. j  av a  2 s  . co  m*/
 *
 * @param x the argument.
 * @return ln(x).
 * The precision of the result is implicitly defined by the precision in the argument.
 */
static public BigDecimal log(BigDecimal x) {
    /* the value is undefined if x is negative.
     */
    if (x.compareTo(BigDecimal.ZERO) < 0) {
        throw new ArithmeticException("Cannot take log of negative " + x.toString());
    } else if (x.compareTo(BigDecimal.ONE) == 0) {
        /* log 1. = 0. */
        return scalePrec(BigDecimal.ZERO, x.precision() - 1);
    } else if (Math.abs(x.doubleValue() - 1.0) <= 0.3) {
        /* The standard Taylor series around x=1, z=0, z=x-1. Abramowitz-Stegun 4.124.
         * The absolute error is err(z)/(1+z) = err(x)/x.
         */
        BigDecimal z = scalePrec(x.subtract(BigDecimal.ONE), 2);
        BigDecimal zpown = z;
        double eps = 0.5 * x.ulp().doubleValue() / Math.abs(x.doubleValue());
        BigDecimal resul = z;
        for (int k = 2;; k++) {
            zpown = multiplyRound(zpown, z);
            BigDecimal c = divideRound(zpown, k);
            if (k % 2 == 0) {
                resul = resul.subtract(c);
            } else {
                resul = resul.add(c);
            }
            if (Math.abs(c.doubleValue()) < eps) {
                break;
            }
        }
        MathContext mc = new MathContext(err2prec(resul.doubleValue(), eps));
        return resul.round(mc);
    } else {
        final double xDbl = x.doubleValue();
        final double xUlpDbl = x.ulp().doubleValue();
        /* Map log(x) = log root[r](x)^r = r*log( root[r](x)) with the aim
         * to move roor[r](x) near to 1.2 (that is, below the 0.3 appearing above), where log(1.2) is roughly 0.2.
         */
        int r = (int) (Math.log(xDbl) / 0.2);
        /* Since the actual requirement is a function of the value 0.3 appearing above,
         * we avoid the hypothetical case of endless recurrence by ensuring that r >= 2.
         */
        r = Math.max(2, r);
        /* Compute r-th root with 2 additional digits of precision
         */
        BigDecimal xhighpr = scalePrec(x, 2);
        BigDecimal resul = root(r, xhighpr);
        resul = log(resul).multiply(new BigDecimal(r));
        /* error propagation: log(x+errx) = log(x)+errx/x, so the absolute error
         * in the result equals the relative error in the input, xUlpDbl/xDbl .
         */
        MathContext mc = new MathContext(err2prec(resul.doubleValue(), xUlpDbl / xDbl));
        return resul.round(mc);
    }
}

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

/**
 * The natural logarithm.//from   ww  w.  j ava 2  s.  com
 *
 * @param n  The main argument, a strictly positive integer.
 * @param mc The requirements on the precision.
 * @return ln(n).
 */
static public BigDecimal log(int n, final MathContext mc) {
    /* the value is undefined if x is negative.
     */
    if (n <= 0) {
        throw new ArithmeticException("Cannot take log of negative " + n);
    } else if (n == 1) {
        return BigDecimal.ZERO;
    } else if (n == 2) {
        if (mc.getPrecision() < LOG2.precision()) {
            return LOG2.round(mc);
        } else {
            /* Broadhurst \protect\vrule width0pt\protect\href{http://arxiv.org/abs/math/9803067}{arXiv:math/9803067}
             * Error propagation: the error in log(2) is twice the error in S(2,-5,...).
             */
            int[] a = { 2, -5, -2, -7, -2, -5, 2, -3 };
            BigDecimal S = broadhurstBBP(2, 1, a, new MathContext(1 + mc.getPrecision()));
            S = S.multiply(new BigDecimal(8));
            S = sqrt(divideRound(S, 3));
            return S.round(mc);
        }
    } else if (n == 3) {
        /* summation of a series roughly proportional to (7/500)^k. Estimate count
         * of terms to estimate the precision (drop the favorable additional
         * 1/k here): 0.013^k <= 10^(-precision), so k*log10(0.013) <= -precision
         * so k>= precision/1.87.
         */
        int kmax = (int) (mc.getPrecision() / 1.87);
        MathContext mcloc = new MathContext(mc.getPrecision() + 1 + (int) (Math.log10(kmax * 0.693 / 1.098)));
        BigDecimal log3 = multiplyRound(log(2, mcloc), 19);
        /* log3 is roughly 1, so absolute and relative error are the same. The
         * result will be divided by 12, so a conservative error is the one
         * already found in mc
         */
        double eps = prec2err(1.098, mc.getPrecision()) / kmax;
        Rational r = new Rational(7153, 524288);
        Rational pk = new Rational(7153, 524288);
        for (int k = 1;; k++) {
            Rational tmp = pk.divide(k);
            if (tmp.doubleValue() < eps) {
                break;
            }
            /* how many digits of tmp do we need in the sum?
             */
            mcloc = new MathContext(err2prec(tmp.doubleValue(), eps));
            BigDecimal c = pk.divide(k).BigDecimalValue(mcloc);
            if (k % 2 != 0) {
                log3 = log3.add(c);
            } else {
                log3 = log3.subtract(c);
            }
            pk = pk.multiply(r);
        }
        log3 = divideRound(log3, 12);
        return log3.round(mc);
    } else if (n == 5) {
        /* summation of a series roughly proportional to (7/160)^k. Estimate count
         * of terms to estimate the precision (drop the favorable additional
         * 1/k here): 0.046^k <= 10^(-precision), so k*log10(0.046) <= -precision
         * so k>= precision/1.33.
         */
        int kmax = (int) (mc.getPrecision() / 1.33);
        MathContext mcloc = new MathContext(mc.getPrecision() + 1 + (int) (Math.log10(kmax * 0.693 / 1.609)));
        BigDecimal log5 = multiplyRound(log(2, mcloc), 14);
        /* log5 is roughly 1.6, so absolute and relative error are the same. The
         * result will be divided by 6, so a conservative error is the one
         * already found in mc
         */
        double eps = prec2err(1.6, mc.getPrecision()) / kmax;
        Rational r = new Rational(759, 16384);
        Rational pk = new Rational(759, 16384);
        for (int k = 1;; k++) {
            Rational tmp = pk.divide(k);
            if (tmp.doubleValue() < eps) {
                break;
            }
            /* how many digits of tmp do we need in the sum?
             */
            mcloc = new MathContext(err2prec(tmp.doubleValue(), eps));
            BigDecimal c = pk.divide(k).BigDecimalValue(mcloc);
            log5 = log5.subtract(c);
            pk = pk.multiply(r);
        }
        log5 = divideRound(log5, 6);
        return log5.round(mc);
    } else if (n == 7) {
        /* summation of a series roughly proportional to (1/8)^k. Estimate count
         * of terms to estimate the precision (drop the favorable additional
         * 1/k here): 0.125^k <= 10^(-precision), so k*log10(0.125) <= -precision
         * so k>= precision/0.903.
         */
        int kmax = (int) (mc.getPrecision() / 0.903);
        MathContext mcloc = new MathContext(
                mc.getPrecision() + 1 + (int) (Math.log10(kmax * 3 * 0.693 / 1.098)));
        BigDecimal log7 = multiplyRound(log(2, mcloc), 3);
        /* log7 is roughly 1.9, so absolute and relative error are the same.
         */
        double eps = prec2err(1.9, mc.getPrecision()) / kmax;
        Rational r = new Rational(1, 8);
        Rational pk = new Rational(1, 8);
        for (int k = 1;; k++) {
            Rational tmp = pk.divide(k);
            if (tmp.doubleValue() < eps) {
                break;
            }
            /* how many digits of tmp do we need in the sum?
             */
            mcloc = new MathContext(err2prec(tmp.doubleValue(), eps));
            BigDecimal c = pk.divide(k).BigDecimalValue(mcloc);
            log7 = log7.subtract(c);
            pk = pk.multiply(r);
        }
        return log7.round(mc);
    } else {
        /* At this point one could either forward to the log(BigDecimal) signature (implemented)
         * or decompose n into Ifactors and use an implemenation of all the prime bases.
         * Estimate of the result; convert the mc argument to an absolute error eps
         * log(n+errn) = log(n)+errn/n = log(n)+eps
         */
        double res = Math.log((double) n);
        double eps = prec2err(res, mc.getPrecision());
        /* errn = eps*n, convert absolute error in result to requirement on absolute error in input
         */
        eps *= n;

        /* Convert this absolute requirement of error in n to a relative error in n
         */
        final MathContext mcloc = new MathContext(1 + err2prec((double) n, eps));
        /* Padd n with a number of zeros to trigger the required accuracy in
         * the standard signature method
         */
        BigDecimal nb = scalePrec(new BigDecimal(n), mcloc);
        return log(nb);
    }
}

From source file:module.siadap.domain.wrappers.UnitSiadapWrapper.java

public BigDecimal getExcellencyEvaluationPercentage() {
    int totalPeopleWorkingForUnit = getUnitEmployees(true).size();
    Collection<PersonSiadapWrapper> excellentEvaluationPersons = getUnitEmployees(true, new Predicate() {

        @Override//from   ww  w. j  a v a2  s .  c  om
        public boolean evaluate(Object personObject) {
            PersonSiadapWrapper personWrapper = (PersonSiadapWrapper) personObject;
            if (personWrapper.getSiadap() == null
                    || personWrapper.getSiadap().getDefaultSiadapEvaluationUniverse() == null) {
                return false;
            }
            return personWrapper.getSiadap().getDefaultSiadapEvaluationUniverse().hasExcellencyAwarded();
        }
    });
    int excellentCount = excellentEvaluationPersons.size();
    if ((excellentCount == 0) || (totalPeopleWorkingForUnit == 0)) {
        return BigDecimal.ZERO;
    }

    return new BigDecimal(excellentCount)
            .divide(new BigDecimal(totalPeopleWorkingForUnit), UnitSiadapWrapper.SCALE, RoundingMode.HALF_EVEN)
            .multiply(new BigDecimal(100), new MathContext(UnitSiadapWrapper.SCALE));
}

From source file:module.siadap.domain.wrappers.UnitSiadapWrapper.java

public BigDecimal getRelevantEvaluationPercentage() {
    int totalPeopleWorkingForUnit = getUnitEmployees(true).size();
    Collection<PersonSiadapWrapper> relevantEvaluationPersons = getUnitEmployees(true, new Predicate() {

        @Override/*from   w  w  w .j a v  a 2 s  .  c  om*/
        public boolean evaluate(Object personObject) {
            PersonSiadapWrapper personWrapper = (PersonSiadapWrapper) personObject;
            if (personWrapper.getSiadap() == null
                    || personWrapper.getSiadap().getDefaultSiadapEvaluationUniverse() == null) {
                return false;
            }
            return personWrapper.getSiadap().getDefaultSiadapEvaluationUniverse().hasRelevantEvaluation();
        }

    });
    int relevantCount = relevantEvaluationPersons.size();

    if ((relevantCount == 0) || (totalPeopleWorkingForUnit == 0)) {
        return BigDecimal.ZERO;
    }

    return new BigDecimal(relevantCount)
            .divide(new BigDecimal(totalPeopleWorkingForUnit), UnitSiadapWrapper.SCALE, RoundingMode.HALF_EVEN)
            .multiply(new BigDecimal(100), new MathContext(UnitSiadapWrapper.SCALE));
}

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

/**
 * The natural logarithm.//from w  ww.j  a v a 2s .c o  m
 *
 * @param r  The main argument, a strictly positive value.
 * @param mc The requirements on the precision.
 * @return ln(r).
 */
static public BigDecimal log(final Rational r, final MathContext mc) {
    /* the value is undefined if x is negative.
     */
    if (r.compareTo(Rational.ZERO) <= 0) {
        throw new ArithmeticException("Cannot take log of negative " + r.toString());
    } else if (r.compareTo(Rational.ONE) == 0) {
        return BigDecimal.ZERO;
    } else {
        /* log(r+epsr) = log(r)+epsr/r. Convert the precision to an absolute error in the result.
         * eps contains the required absolute error of the result, epsr/r.
         */
        double eps = prec2err(Math.log(r.doubleValue()), mc.getPrecision());
        /* Convert this further into a requirement of the relative precision in r, given that
         * epsr/r is also the relative precision of r. Add one safety digit.
         */
        MathContext mcloc = new MathContext(1 + err2prec(eps));
        final BigDecimal resul = log(r.BigDecimalValue(mcloc));
        return resul.round(mc);
    }
}

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

/**
 * Power function.//from  ww  w .  j  ava 2s  .c  o m
 *
 * @param x Base of the power.
 * @param y Exponent of the power.
 * @return x^y.
 * The estimation of the relative error in the result is |log(x)*err(y)|+|y*err(x)/x|
 */
static public BigDecimal pow(final BigDecimal x, final BigDecimal y) {
    if (x.compareTo(BigDecimal.ZERO) < 0) {
        throw new ArithmeticException("Cannot power negative " + x.toString());
    } else if (x.compareTo(BigDecimal.ZERO) == 0) {
        return BigDecimal.ZERO;
    } else {
        /* return x^y = exp(y*log(x)) ;
         */
        BigDecimal logx = log(x);
        BigDecimal ylogx = y.multiply(logx);
        BigDecimal resul = exp(ylogx);
        /* The estimation of the relative error in the result is |log(x)*err(y)|+|y*err(x)/x|
         */
        double errR = Math.abs(logx.doubleValue() * y.ulp().doubleValue() / 2.)
                + Math.abs(y.doubleValue() * x.ulp().doubleValue() / 2. / x.doubleValue());
        MathContext mcR = new MathContext(err2prec(1.0, errR));
        return resul.round(mcR);
    }
}

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

/**
 * Raise to an integer power and round.// w w  w.  ja  v a 2 s  .c o 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

/**
 * Trigonometric sine.//from w w  w . j av a  2s. co m
 *
 * @param x The argument in radians.
 * @return sin(x) in the range -1 to 1.
 */
static public BigDecimal sin(final BigDecimal x) {
    if (x.compareTo(BigDecimal.ZERO) < 0) {
        return sin(x.negate()).negate();
    } else if (x.compareTo(BigDecimal.ZERO) == 0) {
        return BigDecimal.ZERO;
    } else {
        /* reduce modulo 2pi
         */
        BigDecimal res = mod2pi(x);
        double errpi = 0.5 * Math.abs(x.ulp().doubleValue());
        int val = 2 + err2prec(FastMath.PI, errpi);
        MathContext mc = new MathContext(val);
        BigDecimal p = pi(mc);
        mc = new MathContext(x.precision());
        if (res.compareTo(p) > 0) {
            /* pi<x<=2pi: sin(x)= - sin(x-pi)
             */
            return sin(subtractRound(res, p)).negate();
        } else if (res.multiply(new BigDecimal("2")).compareTo(p) > 0) {
            /* pi/2<x<=pi: sin(x)= sin(pi-x)
             */
            return sin(subtractRound(p, res));
        } else {
            /* for the range 0<=x<Pi/2 one could use sin(2x)=2sin(x)cos(x)
             * to split this further. Here, use the sine up to pi/4 and the cosine higher up.
             */
            if (res.multiply(new BigDecimal("4")).compareTo(p) > 0) {
                /* x>pi/4: sin(x) = cos(pi/2-x)
                 */
                return cos(subtractRound(p.divide(new BigDecimal("2")), res));
            } else {
                /* Simple Taylor expansion, sum_{i=1..infinity} (-1)^(..)res^(2i+1)/(2i+1)! */
                BigDecimal resul = res;
                /* x^i */
                BigDecimal xpowi = res;
                /* 2i+1 factorial */
                BigInteger ifac = BigInteger.ONE;
                /* The error in the result is set by the error in x itself.
                 */
                double xUlpDbl = res.ulp().doubleValue();
                /* The error in the result is set by the error in x itself.
                 * 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) (res.precision() / Math.log10(1.0 / res.doubleValue())) / 2;
                MathContext mcTay = new MathContext(err2prec(res.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)));
                    xpowi = xpowi.multiply(res).multiply(res).negate();
                    BigDecimal corr = xpowi.divide(new BigDecimal(ifac), mcTay);
                    resul = resul.add(corr);
                    if (corr.abs().doubleValue() < 0.5 * xUlpDbl) {
                        break;
                    }
                }
                /* The error in the result is set by the error in x itself.
                 */
                mc = new MathContext(res.precision());
                return resul.round(mc);
            }
        }
    } /* sin */
}

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

/**
 * Trigonometric cosine./*from ww  w  .j  av  a 2 s .  c  om*/
 *
 * @param x The argument in radians.
 * @return cos(x) in the range -1 to 1.
 */
static public BigDecimal cos(final BigDecimal x) {
    if (x.compareTo(BigDecimal.ZERO) < 0) {
        return cos(x.negate());
    } else if (x.compareTo(BigDecimal.ZERO) == 0) {
        return BigDecimal.ONE;
    } else {
        /* reduce modulo 2pi
         */
        BigDecimal res = mod2pi(x);
        double errpi = 0.5 * Math.abs(x.ulp().doubleValue());
        int val = +err2prec(FastMath.PI, errpi);
        MathContext mc = new MathContext(val);
        BigDecimal p = pi(mc);
        mc = new MathContext(x.precision());
        if (res.compareTo(p) > 0) {
            /* pi<x<=2pi: cos(x)= - cos(x-pi)
             */
            return cos(subtractRound(res, p)).negate();
        } else if (res.multiply(new BigDecimal("2")).compareTo(p) > 0) {
            /* pi/2<x<=pi: cos(x)= -cos(pi-x)
             */
            return cos(subtractRound(p, res)).negate();
        } else {
            /* for the range 0<=x<Pi/2 one could use cos(2x)= 1-2*sin^2(x)
             * to split this further, or use the cos up to pi/4 and the sine higher up.
            throw new ProviderException("Unimplemented cosine ") ;
             */
            if (res.multiply(new BigDecimal("4")).compareTo(p) > 0) {
                /* x>pi/4: cos(x) = sin(pi/2-x)
                 */
                return sin(subtractRound(p.divide(new BigDecimal("2")), res));
            } else {
                /* Simple Taylor expansion, sum_{i=0..infinity} (-1)^(..)res^(2i)/(2i)! */
                BigDecimal resul = BigDecimal.ONE;
                /* x^i */
                BigDecimal xpowi = BigDecimal.ONE;
                /* 2i factorial */
                BigInteger ifac = BigInteger.ONE;
                /* The absolute error in the result is the error in x^2/2 which is x times the error in x.
                 */
                double xUlpDbl = 0.5 * res.ulp().doubleValue() * res.doubleValue();
                /* The error in the result is set by the error in x^2/2 itself, xUlpDbl.
                 * We need at most k terms to push x^(2k+1)/(2k+1)! below this value.
                 * x^(2k) < xUlpDbl; (2k)*log(x) < log(xUlpDbl);
                 */
                int k = (int) (Math.log(xUlpDbl) / Math.log(res.doubleValue())) / 2;
                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)));
                    ifac = ifac.multiply(new BigInteger("" + (2 * i)));
                    xpowi = xpowi.multiply(res).multiply(res).negate();
                    BigDecimal corr = xpowi.divide(new BigDecimal(ifac), mcTay);
                    resul = resul.add(corr);
                    if (corr.abs().doubleValue() < 0.5 * xUlpDbl) {
                        break;
                    }
                }
                /* The error in the result is governed by the error in x itself.
                 */
                mc = new MathContext(err2prec(resul.doubleValue(), xUlpDbl));
                return resul.round(mc);
            }
        }
    }
}