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:com.examples.with.different.packagename.concolic.MathRuntimeException.java

/**
 * Constructs a new <code>ArithmeticException</code> with specified formatted detail message.
 * Message formatting is delegated to {@link java.text.MessageFormat}.
 * @param pattern format specifier/*from  w  ww.ja  va  2 s  .c  o  m*/
 * @param arguments format arguments
 * @return built exception
 */
public static ArithmeticException createArithmeticException(final String pattern, final Object... arguments) {
    return new ArithmeticException(buildMessage(Locale.US, pattern, arguments)) {

        /** Serializable version identifier. */
        private static final long serialVersionUID = 7705628723242533939L;

        /** {@inheritDoc} */
        @Override
        public String getLocalizedMessage() {
            return buildMessage(Locale.getDefault(), pattern, arguments);
        }

    };
}

From source file:pt.webdetails.cda.CdaCoreService.java

private static void setPostProcessOptions(DoQueryParameters parameters, final QueryOptions queryOptions) {
    // Handle paging options
    // We assume that any paging options found mean that the user actively wants paging.
    final long pageSize = parameters.getPageSize();
    final long pageStart = parameters.getPageStart();
    final boolean paginate = parameters.isPaginateQuery();
    if (pageSize > 0 || pageStart > 0 || paginate) {
        if (pageSize > Integer.MAX_VALUE || pageStart > Integer.MAX_VALUE) {
            throw new ArithmeticException("Paging values too large");
        }/*from ww  w .j a va2  s .  co m*/
        queryOptions.setPaginate(true);
        queryOptions.setPageSize(pageSize > 0 ? (int) pageSize : paginate ? DEFAULT_PAGE_SIZE : 0);
        queryOptions.setPageStart(pageStart > 0 ? (int) pageStart : paginate ? DEFAULT_START_PAGE : 0);
    }

    try {
        queryOptions.setOutputIndexId(parameters.getOutputIndexId());
    } catch (NumberFormatException e) {
        logger.error("Illegal outputIndexId '" + parameters.getOutputIndexId() + "'");
    }

    final ArrayList<String> sortBy = new ArrayList<String>();
    for (String sort : parameters.getSortBy()) {
        if (!StringUtils.isEmpty(sort)) {
            sortBy.add(sort);
        }
    }
    queryOptions.setSortBy(sortBy);
}

From source file:org.apache.hadoop.fs.nfs.tools.Nfs3Console.java

private static double calculateTransferRateMB(long bytesTransferred, long timeInMilliSeconds)
        throws ArithmeticException {
    if (timeInMilliSeconds == 0) {
        throw new ArithmeticException("Divide by zero");
    } else {//from w ww .  j a va 2s.c  o  m
        return (bytesTransferred / (1024.0 * 1024.0)) / (timeInMilliSeconds / 1000.0);
    }
}

From source file:Rotation.java

/** Build a rotation from an axis and an angle.
 * <p>We use the convention that angles are oriented according to
 * the effect of the rotation on vectors around the axis. That means
 * that if (i, j, k) is a direct frame and if we first provide +k as
 * the axis and PI/2 as the angle to this constructor, and then
 * {@link #applyTo(Vector3D) apply} the instance to +i, we will get
 * +j.</p>/*from  w w  w  .jav a  2  s.com*/
 * @param axis axis around which to rotate
 * @param angle rotation angle.
 * @exception ArithmeticException if the axis norm is zero
 */
public Rotation(Vector3D axis, double angle) {

    double norm = axis.getNorm();
    if (norm == 0) {
        throw new ArithmeticException("zero norm for rotation axis");
    }

    double halfAngle = -0.5 * angle;
    double coeff = Math.sin(halfAngle) / norm;

    q0 = Math.cos(halfAngle);
    q1 = coeff * axis.getX();
    q2 = coeff * axis.getY();
    q3 = coeff * axis.getZ();

}

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

/**
 * Get the point of intersection between the ray from the centroid of this
 * box through q1 that is closest to q1.
 * /*  www. j a  va2 s .co  m*/
 * @param q1
 *            A point that is not the centroid.
 * @return A point on the perimeter of the box on the ray from the centroid
 *         through q1.
 */
public Pnt getNearIntersection(Pnt q1) {
    Pnt p1 = this.getCentroid();
    Pnt r1 = new Pnt();
    Pnt prev = getVertex(getNumberOfVerteces());
    boolean isNegative = false;
    Pnt curr = null;
    for (Iterator iter = getVerteces(); iter.hasNext(); prev = curr) {
        curr = (Pnt) iter.next();

        // Since points are CCW, the line we want is the first positivie
        // value.
        // They cannot all be negative, although one or two may be zero.
        Rational signArea = Util.areaSign(curr, p1, q1);
        if (isNegative && signArea.isPositive()) {
            Util.lineIntersection(p1, q1, prev, curr, r1);
            return r1;
        } else if (signArea.isNegative()) {
            isNegative = true;
        } else if (isNegative && signArea.isZero()) {
            isNegative = false;
            return new Pnt(curr);
        }
    }
    if (!isNegative) {
        throw new ArithmeticException("No near intersection with " + q1);
    } else {
        curr = getVertex(0);
        Util.lineIntersection(p1, q1, prev, curr, r1);
        return r1;
    }
}

From source file:elements.Vector3D.java

/** Get a vector orthogonal to the instance.
 * <p>There are an infinite number of normalized vectors orthogonal
 * to the instance. This method picks up one of them almost
 * arbitrarily. It is useful when one needs to compute a reference
 * frame with one of the axes in a predefined direction. The
 * following example shows how to build a frame having the k axis
 * aligned with the known vector u :/*from ww w  .j a  va  2  s.  c  om*/
 * <pre><code>
 *   Vector3D k = u.normalize();
 *   Vector3D i = k.orthogonal();
 *   Vector3D j = Vector3D.crossProduct(k, i);
 * </code></pre></p>
 * @return a new normalized vector orthogonal to the instance
 * @exception ArithmeticException if the norm of the instance is null
 */
public Vector3D orthogonal() {

    double threshold = 0.1 * getNorm();
    if (threshold == 0) {
        throw new ArithmeticException("null norm");
    }

    //if ((x >= -threshold) && (x <= threshold)) {
    //    double inverse  = 1 / Math.sqrt(y * y + z * z);
    //    return new Vector3D(0, inverse * z, -inverse * y);

    // }
    // else if ((y >= -threshold) && (y <= threshold)) {
    //  double inverse  = 1 / Math.sqrt(x * x + z * z);
    //     return new Vector3D(-inverse * z, 0, inverse * x);
    //   }
    double inverse = 1 / Math.sqrt(x * x + y * y);
    return new Vector3D(inverse * y, -inverse * x, 0);

}

From source file:bb.mcmc.analysis.HeidelbergConvergeStat.java

/**
 * Returns the modified Bessel function of the third kind
 * of order <tt>nn</tt> of the argument.
 * <p>// ww w  . j  av a  2  s.c  o m
 * The range is partitioned into the two intervals [0,9.55] and
 * (9.55, infinity).  An ascending power series is used in the
 * low range, and an asymptotic expansion in the high range.
 *
 * @param nn the order of the Bessel function.
 * @param x the value to compute the bessel function of.
 */
static public double kn2(double nd, double x) throws ArithmeticException {
    /*
    Algorithm for Kn.
             n-1 
          -n   -  (n-k-1)!    2   k
    K (x)  =  0.5 (x/2)     >  -------- (-x /4)
     n                      -     k!
             k=0
            
          inf.                                   2   k
          n         n   -                                   (x /4)
     + (-1)  0.5(x/2)    >  {p(k+1) + p(n+k+1) - 2log(x/2)} ---------
           -                                  k! (n+k)!
          k=0
            
    where  p(m) is the psi function: p(1) = -EUL and
            
            m-1
             -
         p(m)  =  -EUL +  >  1/k
             -
            k=1
            
    For large x,
                          2        2     2
                        u-1     (u-1 )(u-3 )
    K (z)  =  sqrt(pi/2z) exp(-z) { 1 + ------- + ------------ + ...}
     v                                        1            2
                      1! (8z)     2! (8z)
    asymptotically, where
            
    2
       u = 4 v .
            
    */
    final double EUL = 5.772156649015328606065e-1;
    final double MAXNUM = Double.MAX_VALUE;
    final int MAXFAC = 31;
    int nn;// = (int) nd;

    double k, kf, nk1f, nkf, zn, t, s, z0, z;
    double ans, fn, pn, pk, zmn, tlg, tox;
    int i;//, n;

    if (nd < 0)
        nn = (int) -nd;
    else
        nn = (int) nd;

    if (nn > MAXFAC)
        throw new ArithmeticException("Overflow");
    if (x <= 0.0)
        throw new IllegalArgumentException();

    if (x <= 9.55) {
        ans = 0.0;
        z0 = 0.25 * x * x;
        fn = 1.0;
        pn = 0.0;
        zmn = 1.0;
        tox = 2.0 / x;

        if (nn > 0) {
            /* compute factorial of n and psi(n) */
            pn = -EUL;
            k = 1.0;
            for (i = 1; i < nn; i++) {
                pn += 1.0 / k;
                k += 1.0;
                fn *= k;
            }

            zmn = tox;

            if (nn == 1) {
                ans = 1.0 / x;
            } else {
                nk1f = fn / nd;
                kf = 1.0;
                s = nk1f;
                z = -z0;
                zn = 1.0;
                for (i = 1; i < nn; i++) {
                    nk1f = nk1f / (nn - i);
                    kf = kf * i;
                    zn *= z;
                    t = nk1f * zn / kf;
                    s += t;
                    if ((MAXNUM - Math.abs(t)) < Math.abs(s))
                        throw new ArithmeticException("Overflow");
                    if ((tox > 1.0) && ((MAXNUM / tox) < zmn))
                        throw new ArithmeticException("Overflow");
                    zmn *= tox;
                }
                s *= 0.5;
                t = Math.abs(s);
                if ((zmn > 1.0) && ((MAXNUM / zmn) < t))
                    throw new ArithmeticException("Overflow");
                if ((t > 1.0) && ((MAXNUM / t) < zmn))
                    throw new ArithmeticException("Overflow");
                ans = s * zmn;
            }
        }

        tlg = 2.0 * Math.log(0.5 * x);
        pk = -EUL;
        if (nn == 0) {
            pn = pk;
            t = 1.0;
        } else {
            pn = pn + 1.0 / nd;
            t = 1.0 / fn;
        }
        s = (pk + pn - tlg) * t;
        k = 1.0;
        do {
            t *= z0 / (k * (k + nd));
            pk += 1.0 / k;
            pn += 1.0 / (k + nd);
            s += (pk + pn - tlg) * t;
            k += 1.0;
        } while (Math.abs(t / s) > MACHEP);

        s = 0.5 * s / zmn;
        if ((nn & 1) > 0)
            s = -s;
        ans += s;

        return (ans);
    }

    /* Asymptotic expansion for Kn(x) */
    /* Converges to 1.4e-17 for x > 18.4 */
    if (x > MAXLOG)
        throw new ArithmeticException("Underflow");
    k = nd;
    pn = 4.0 * k * k;
    pk = 1.0;
    z0 = 8.0 * x;
    fn = 1.0;
    t = 1.0;
    s = t;
    nkf = MAXNUM;
    i = 0;
    do {
        z = pn - pk * pk;
        t = t * z / (fn * z0);
        nk1f = Math.abs(t);
        if ((i >= nn) && (nk1f > nkf)) {
            ans = Math.exp(-x) * Math.sqrt(Math.PI / (2.0 * x)) * s;
            return (ans);
        }
        nkf = nk1f;
        s += t;
        fn += 1.0;
        pk += 2.0;
        i += 1;
    } while (Math.abs(t / s) > MACHEP);

    ans = Math.exp(-x) * Math.sqrt(Math.PI / (2.0 * x)) * s;
    return (ans);
}

From source file:NumberUtils.java

/**
 * Compares the first number to the second one numerically and 
 * returns an integer depending on the comparison result:
 * a negative value if the first number is the smaller one,
 * a zero value if they are equal, and/*from  www. j  a v  a2s . c o  m*/
 * a positive value if the first number is the larger one.
 *
 * The main strategy goes like follows:
 * 1. If one of the arguments is <code>null</code> or 'not a number',
 *    throw an exception.
 * 2. If both values are 'long compatible', compare their <code>longValue()</code>
 *    using the usual comparison operators for primitive types (&lt;, ==, &gt;).
 * 3. If both values are 'double compatible', compare their <code>doubleValue()</code>
 *    using the usual comparison operators for primitive types (&lt;, ==, &gt;).
 * 4. If one of the values is infinite (and the other is finite),
 *    determine the result depending on the sign of the infinite value.
 * 5. Otherwise convert both values to <code>java.math.BigDecimal</code> and
 *    return the result of the <code>BigDecimal.compareTo(BigDecimal)</code> method.
 *
 * As a consequence, the method is not suitable to implement a
 * <code>java.util.Comparator</code> for numbers. To achieve this,
 * one had to accept 'not a number' arguments and place them somewhere
 * in the row of numbers (probably at the upper end, i.e. larger than
 * positive infinity, as <code>Double.compare(double, double)</code>
 * does it).
 * So the behavior of this method is like that of the comparison
 * operator for primitive types and not like that of the related
 * <code>compareTo(...)</code> methods. Besides the handling of
 * 'not a number' values this makes a difference, when comparing
 * the float or double values <code>-0.0</code> and <code>0.0</code>:
 * again, like the operators, we consider them as equal (whereas
 * according to <code>Double.compareTo(...)</code> <code>-0.0</code>
 * is less than <code>0.0</code>).
 *
 * @param first
 * @param second
 * @return int
 * @throws ArithmeticException One or both of the given numbers is <code>null</code> or 'not a number'.
 */
public static int compare(Number first, Number second) throws ArithmeticException {
    if (first == null || second == null || isNaN(first) || isNaN(second))
        throw new ArithmeticException("Arguments must not be null or NaN.");

    int result = -2;

    if (isLongCompatible(first) && isLongCompatible(second)) {
        long v1 = first.longValue(), v2 = second.longValue();
        result = v1 < v2 ? -1 : v1 == v2 ? 0 : v1 > v2 ? 1 : 2;
    } else if (isDoubleCompatible(first) && isDoubleCompatible(second)) {
        double v1 = first.doubleValue(), v2 = second.doubleValue();
        result = v1 < v2 ? -1 : v1 == v2 ? 0 : v1 > v2 ? 1 : 2;
    }

    if (result == 2) // should not happen
        throw new ArithmeticException("Arguments " + first + " and " + second + " are not comparable.");
    if (result > -2)
        return result;

    if (isInfinite(first)) // => second is finite
        return first.doubleValue() == Double.NEGATIVE_INFINITY ? -1 : 1;
    if (isInfinite(second)) // => first is finite
        return second.doubleValue() == Double.POSITIVE_INFINITY ? -1 : 1;

    return toBigDecimal(first).compareTo(toBigDecimal(second));
}

From source file:syncleus.dann.data.vector.Vector.java

public Vector normalize() {
    if (this.isOrigin())
        throw new ArithmeticException("cant normalize a 0 vector");
    final double norm = this.getNorm();
    return this.multiply(1.0 / norm);
}

From source file:jsat.distributions.empirical.MyKernelDensityEstimator.java

/**
 * Sets the bandwidth used for smoothing. Higher values make the pdf smoother, but can obscure features. Too small a bandwidth will causes spikes at only
 * the data points./*from   w w w  .  j  a  v a  2  s .c om*/
 * 
 * @param val
 *            new bandwidth
 */
public void setBandwith(double val) {
    if (val <= 0 || Double.isInfinite(val)) {
        throw new ArithmeticException("Bandwith parameter h must be greater than zero, not " + 0);
    }

    this.h = val;
}