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:Main.java

public static int toInt(long l) {
    if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE)
        throw new ArithmeticException("Value (" + l + ") cannot fit into int");
    return (int) l;
}

From source file:MathUtils.java

public static int pgcd(int u, int v) {
    if (u * v == 0) {
        return (Math.abs(u) + Math.abs(v));
    }/*from  ww w .ja  v a  2  s  .c o  m*/
    // keep u and v negative, as negative integers range down to
    // -2^31, while positive numbers can only be as large as 2^31-1
    // (i.e. we can't necessarily negate a negative number without
    // overflow)
    /* assert u!=0 && v!=0; */
    if (u > 0) {
        u = -u;
    } // make u negative
    if (v > 0) {
        v = -v;
    } // make v negative
      // B1. [Find power of 2]
    int k = 0;
    while ((u & 1) == 0 && (v & 1) == 0 && k < 31) { // while u and v are
        // both even...
        u /= 2;
        v /= 2;
        k++; // cast out twos.
    }
    if (k == 31) {
        throw new ArithmeticException("overflow: gcd is 2^31");
    }
    // B2. Initialize: u and v have been divided by 2^k and at least
    // one is odd.
    int t = ((u & 1) == 1) ? v : -(u / 2)/* B3 */;
    // t negative: u was odd, v may be even (t replaces v)
    // t positive: u was even, v is odd (t replaces u)
    do {
        /* assert u<0 && v<0; */
        // B4/B3: cast out twos from t.
        while ((t & 1) == 0) { // while t is even..
            t /= 2; // cast out twos
        }
        // B5 [reset max(u,v)]
        if (t > 0) {
            u = -t;
        } else {
            v = t;
        }
        // B6/B3. at this point both u and v should be odd.
        t = (v - u) / 2;
        // |u| larger: t positive (replace u)
        // |v| larger: t negative (replace v)
    } while (t != 0);
    return -u * (1 << k); // gcd is u*2^k
}

From source file:Main.java

/**
 * Compute the square root of x to a given scale, x >= 0. Use Newton's
 * algorithm./*from   w ww .j  a v  a  2s.co m*/
 * 
 * @param x
 *            the value of x
 * @return the result value
 */
public static BigDecimal sqrt(BigDecimal x) {
    // Check that x >= 0.
    if (x.signum() < 0) {
        throw new ArithmeticException("x < 0");
    }

    // n = x*(10^(2*SCALE))
    BigInteger n = x.movePointRight(SCALE << 1).toBigInteger();

    // The first approximation is the upper half of n.
    int bits = (n.bitLength() + 1) >> 1;
    BigInteger ix = n.shiftRight(bits);
    BigInteger ixPrev;

    // Loop until the approximations converge
    // (two successive approximations are equal after rounding).
    do {
        ixPrev = ix;

        // x = (x + n/x)/2
        ix = ix.add(n.divide(ix)).shiftRight(1);

        Thread.yield();
    } while (ix.compareTo(ixPrev) != 0);

    return new BigDecimal(ix, SCALE);
}

From source file:geogebra.kernel.implicit.PolynomialUtils.java

/**
 * calculates the quotient of p/d (no calculation of the remainder is done)
 * @param cp/*ww  w  .  ja v  a  2 s . c o m*/
 * @param cd
 * @return quotient of cp/cd
 */
public static double[] polynomialDivision(double[] cp, double[] cd) {
    double[] cq;
    cp = cp.clone();
    int degD = cd.length - 1;
    while (degD >= 0 && Kernel.isZero(cd[degD])) {
        degD--;
    }
    if (degD < 0) { // => division by zero
        throw new ArithmeticException("divide by zero polynomial");
    }
    if (cp.length - 1 < degD) {
        return new double[] { 0 };
    } else {
        cq = new double[cp.length - degD];
    }
    double lcd = cd[degD];
    int k = cp.length - 1;
    for (int i = cq.length - 1; i >= 0; i--) {
        cq[i] = cp[k] / lcd;
        for (int j = 0; j <= degD - 1; j++) {
            cp[j + i] = cp[j + i] - cq[i] * cd[j];
        }
        k--;
    }
    return cq;
}

From source file:Main.java

/**
 * Add two integers, checking for overflow.
 * /*ww  w.  jav  a2 s  . c  o m*/
 * @param x an addend
 * @param y an addend
 * @return the sum <code>x+y</code>
 * @throws ArithmeticException if the result can not be represented as an
 *         int
 * @since 1.1
 */
public static int addAndCheck(int x, int y) {
    long s = (long) x + (long) y;
    if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) {
        throw new ArithmeticException("overflow: add");
    }
    return (int) s;
}

From source file:Main.java

/**
 * Subtract two integers, checking for overflow.
 * //from w w w.  ja  va 2  s  .  c  o  m
 * @param x the minuend
 * @param y the subtrahend
 * @return the difference <code>x-y</code>
 * @throws ArithmeticException if the result can not be represented as an
 *         int
 * @since 1.1
 */
public static int subAndCheck(int x, int y) {
    long s = (long) x - (long) y;
    if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) {
        throw new ArithmeticException("overflow: subtract");
    }
    return (int) s;
}

From source file:Main.java

public static int[] test(int[] intArray) {
    int[] returnArrayTemp = new int[intArray.length];
    for (int n = 0, m = intArray.length; n < m; n++) {
        int i = intArray[n];
        if (i < 0) {
            throw new ArithmeticException("Cannot determine if a negative value is prime");
        } else if (i == 0) {
            throw new ArithmeticException("Cannot determine if zero is prime");
        } else if (i == 1) {
            throw new ArithmeticException("Cannot determine if one is prime");
        } else if (i == 2) {
            returnArrayTemp[n] = i;//from   w  w  w.ja  va 2  s . c  o m
        } else {
            if (unprotectedTest(i) == true) {
                returnArrayTemp[n] = i;
            }
        }
    }
    int[] returnArray = arrayCopy(returnArrayTemp);
    return returnArray;
}

From source file:Main.java

/**
 * Subtract two long integers, checking for overflow.
 * /*from   w ww .j  a  v a2  s  . c  o m*/
 * @param a first value
 * @param b second value
 * @return the difference <code>a-b</code>
 * @throws ArithmeticException if the result can not be represented as an
 *         long
 * @since 1.2
 */
public static long subAndCheck(long a, long b) {
    long ret;
    String msg = "overflow: subtract";
    if (b == Long.MIN_VALUE) {
        if (a < 0) {
            ret = a - b;
        } else {
            throw new ArithmeticException(msg);
        }
    } else {
        // use additive inverse
        ret = addAndCheck(a, -b, msg);
    }
    return ret;
}

From source file:Main.java

public static BigDecimal log10(BigDecimal b) {
    final int NUM_OF_DIGITS = SCALE + 2;
    // need to add one to get the right number of dp
    //  and then add one again to get the next number
    //  so I can round it correctly.

    MathContext mc = new MathContext(NUM_OF_DIGITS, RoundingMode.HALF_EVEN);
    //special conditions:
    // log(-x) -> exception
    // log(1) == 0 exactly;
    // log of a number lessthan one = -log(1/x)
    if (b.signum() <= 0) {
        throw new ArithmeticException("log of a negative number! (or zero)");
    } else if (b.compareTo(BigDecimal.ONE) == 0) {
        return BigDecimal.ZERO;
    } else if (b.compareTo(BigDecimal.ONE) < 0) {
        return (log10((BigDecimal.ONE).divide(b, mc))).negate();
    }// w w w  .  j  a va  2  s  . c o m

    StringBuilder sb = new StringBuilder();
    //number of digits on the left of the decimal point
    int leftDigits = b.precision() - b.scale();

    //so, the first digits of the log10 are:
    sb.append(leftDigits - 1).append(".");

    //this is the algorithm outlined in the webpage
    int n = 0;
    while (n < NUM_OF_DIGITS) {
        b = (b.movePointLeft(leftDigits - 1)).pow(10, mc);
        leftDigits = b.precision() - b.scale();
        sb.append(leftDigits - 1);
        n++;
    }

    BigDecimal ans = new BigDecimal(sb.toString());

    //Round the number to the correct number of decimal places.
    ans = ans.round(new MathContext(ans.precision() - ans.scale() + SCALE, RoundingMode.HALF_EVEN));
    return ans;
}

From source file:Main.java

public static BigDecimal log10(BigDecimal b) {
    final int NUM_OF_DIGITS = SCALE + 2;
    // need to add one to get the right number of dp
    // and then add one again to get the next number
    // so I can round it correctly.

    MathContext mc = new MathContext(NUM_OF_DIGITS, RoundingMode.HALF_EVEN);
    // special conditions:
    // log(-x) -> exception
    // log(1) == 0 exactly;
    // log of a number lessthan one = -log(1/x)
    if (b.signum() <= 0) {
        throw new ArithmeticException("log of a negative number! (or zero)");
    } else if (b.compareTo(BigDecimal.ONE) == 0) {
        return BigDecimal.ZERO;
    } else if (b.compareTo(BigDecimal.ONE) < 0) {
        return (log10((BigDecimal.ONE).divide(b, mc))).negate();
    }/* w w  w . j  ava2 s  .  c  o m*/

    StringBuilder sb = new StringBuilder();
    // number of digits on the left of the decimal point
    int leftDigits = b.precision() - b.scale();

    // so, the first digits of the log10 are:
    sb.append(leftDigits - 1).append(".");

    // this is the algorithm outlined in the webpage
    int n = 0;
    while (n < NUM_OF_DIGITS) {
        b = (b.movePointLeft(leftDigits - 1)).pow(10, mc);
        leftDigits = b.precision() - b.scale();
        sb.append(leftDigits - 1);
        n++;
    }

    BigDecimal ans = new BigDecimal(sb.toString());

    // Round the number to the correct number of decimal places.
    ans = ans.round(new MathContext(ans.precision() - ans.scale() + SCALE, RoundingMode.HALF_EVEN));
    return ans;
}