Example usage for java.lang ArithmeticException ArithmeticException

List of usage examples for java.lang ArithmeticException ArithmeticException

Introduction

In this page you can find the example usage for java.lang ArithmeticException ArithmeticException.

Prototype

public ArithmeticException(String s) 

Source Link

Document

Constructs an ArithmeticException with the specified detail message.

Usage

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

/**
 * The natural logarithm.//from   w ww . j  av a 2s .c om
 *
 * @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:edu.umd.cfar.lamp.viper.geometry.BoundingBox.java

/**
 * Gets the width of the box.//from  w  w  w . j  a va2  s .  co  m
 * 
 * @return the width of the box.
 * @throws ArithmeticException
 *             if the set of boxes is not a singleton
 */
public int getWidth() {
    if (composed)
        throw new ArithmeticException("Cannot get got composed bboxes");
    return rect.width;
}

From source file:ubic.basecode.math.SpecFunc.java

public static final double digamma(double x) {
    double ans[] = dpsifn(x, 0, 1, 1);
    if (ans == null)
        throw new ArithmeticException(sErrorDomain);
    return -ans[0];
}

From source file:edu.umd.cfar.lamp.viper.geometry.BoundingBox.java

/**
 * Gets the height of the box.//  w  w  w.  j  a  va 2s  .c  o  m
 * 
 * @return the height of the box.
 * @throws ArithmeticException
 *             if the set of boxes is not a singleton
 */
public int getHeight() {
    if (composed)
        throw new ArithmeticException("Cannot get got composed bboxes");
    return rect.height;
}

From source file:ubic.basecode.math.SpecFunc.java

public static final double trigamma(double x) {
    double ans[] = dpsifn(x, 1, 1, 1);
    if (ans == null)
        throw new ArithmeticException(sErrorDomain);
    return ans[0];
}

From source file:ubic.basecode.math.SpecFunc.java

public static final double tetragamma(double x) {
    double ans[] = dpsifn(x, 2, 1, 1);
    if (ans == null)
        throw new ArithmeticException(sErrorDomain);
    return -2.0 * ans[0];
}

From source file:ubic.basecode.math.SpecFunc.java

public static final double pentagamma(double x) {
    double ans[] = dpsifn(x, 3, 1, 1);
    if (ans == null)
        throw new ArithmeticException(sErrorDomain);
    return 6.0 * ans[0];
}

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

/**
 * The natural logarithm./*from  www .  j  a  v  a 2s  .  c om*/
 *
 * @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:uniol.apt.analysis.synthesize.SynthesizePN.java

private static long bigIntToLong(BigInteger value) {
    if (value.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0
            || value.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0)
        throw new ArithmeticException("Cannot represent value as long: " + value);
    return value.longValue();
}

From source file:uniol.apt.analysis.synthesize.SynthesizePN.java

private static int bigIntToInt(BigInteger value) {
    if (value.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0
            || value.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0)
        throw new ArithmeticException("Cannot represent value as int: " + value);
    return value.intValue();
}