List of usage examples for java.lang ArithmeticException ArithmeticException
public ArithmeticException(String s)
From source file:geogebra.common.kernel.implicit.PolynomialUtils.java
/** * calculates the quotient of p/d (no calculation of the remainder is done) * @param cp coefficients of dividend/* w ww .j a v a 2 s . c o m*/ * @param cd coefficients of divisor * @return quotient of cp/cd */ public static double[] polynomialDivision(double[] cp, double[] cd) { double[] cq; double[] cpclone; cpclone = new double[cp.length]; for (int i = 0; i < cp.length; i++) { cpclone[i] = cp[i]; } 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 (cpclone.length - 1 < degD) { return new double[] { 0 }; } cq = new double[cpclone.length - degD]; double lcd = cd[degD]; int k = cpclone.length - 1; for (int i = cq.length - 1; i >= 0; i--) { cq[i] = cpclone[k] / lcd; for (int j = 0; j <= degD - 1; j++) { cpclone[j + i] = cpclone[j + i] - cq[i] * cd[j]; } k--; } return cq; }
From source file:Main.java
/** * Multiply two long integers, checking for overflow. * // ww w. ja v a 2s.c om * @param a first value * @param b second value * @return the product <code>a * b</code> * @throws ArithmeticException if the result can not be represented as an * long * @since 1.2 */ public static long mulAndCheck(long a, long b) { long ret; String msg = "overflow: multiply"; if (a > b) { // use symmetry to reduce boundry cases ret = mulAndCheck(b, a); } else { if (a < 0) { if (b < 0) { // check for positive overflow with negative a, negative b if (a >= Long.MAX_VALUE / b) { ret = a * b; } else { throw new ArithmeticException(msg); } } else if (b > 0) { // check for negative overflow with negative a, positive b if (Long.MIN_VALUE / b <= a) { ret = a * b; } else { throw new ArithmeticException(msg); } } else { // assert b == 0 ret = 0; } } else if (a > 0) { // assert a > 0 // assert b > 0 // check for positive overflow with positive a, positive b if (a <= Long.MAX_VALUE / b) { ret = a * b; } else { throw new ArithmeticException(msg); } } else { // assert a == 0 ret = 0; } } return ret; }
From source file:Main.java
/** * Multiply two integers, checking for overflow. * /*from w ww . jav a 2 s .co m*/ * @param x a factor * @param y a factor * @return the product <code>x*y</code> * @throws ArithmeticException if the result can not be represented as an * int * @since 1.1 */ public static int mulAndCheck(int x, int y) { long m = ((long) x) * ((long) y); if (m < Integer.MIN_VALUE || m > Integer.MAX_VALUE) { throw new ArithmeticException("overflow: mul"); } return (int) m; }
From source file:Main.java
/** * Returns n!. Shorthand for <code>n</code> <a * href="http://mathworld.wolfram.com/Factorial.html"> Factorial</a>, the * product of the numbers <code>1,...,n</code>. * <p>//from w w w . j a v a2 s . co m * <Strong>Preconditions</strong>: * <ul> * <li> <code>n >= 0</code> (otherwise * <code>IllegalArgumentException</code> is thrown)</li> * <li> The result is small enough to fit into a <code>long</code>. The * largest value of <code>n</code> for which <code>n!</code> < * Long.MAX_VALUE</code> is 20. If the computed value exceeds <code>Long.MAX_VALUE</code> * an <code>ArithMeticException </code> is thrown.</li> * </ul> * </p> * * @param n argument * @return <code>n!</code> * @throws ArithmeticException if the result is too large to be represented * by a long integer. * @throws IllegalArgumentException if n < 0 */ public static long factorial(final int n) { long result = Math.round(factorialDouble(n)); if (result == Long.MAX_VALUE) { throw new ArithmeticException("result too large to represent in a long integer"); } return result; }
From source file:edu.rit.util.Mathe.java
/** * Compute the integer square root of the integer <TT>x</TT>. The value * floor(<TT>x</TT><SUP>1/2</SUP>) is returned. The answer is calculated * using an exact integer algorithm taken from: * <UL>//w w w.j a v a 2 s.co m * <LI> * J. Crenshaw. Integer square roots. * <A HREF="http://www.embedded.com/98/9802fe2.htm" * TARGET="_top">http://www.embedded.com/98/9802fe2.htm</A> * </UL> * * @param x Input. * @return Floor(<TT>x</TT><SUP>1/2</SUP>). * @exception ArithmeticException (unchecked exception) Thrown if <TT>x</TT> * < 0. */ public static int sqrt(int x) { if (x < 0) { throw new ArithmeticException("Mathe.sqrt(): x < 0"); } int rem = 0; int root = 0; for (int i = 0; i < 16; ++i) { root <<= 1; rem = (rem << 2) | (x >>> 30); x <<= 2; ++root; if (root <= rem) { rem -= root; ++root; } else { --root; } } return root >>> 1; }
From source file:Main.java
/** * Add two long integers, checking for overflow. * //from ww w . ja va2s.c o m * @param a an addend * @param b an addend * @param msg the message to use for any thrown exception. * @return the sum <code>a+b</code> * @throws ArithmeticException if the result can not be represented as an * long * @since 1.2 */ private static long addAndCheck(long a, long b, String msg) { long ret; if (a > b) { // use symmetry to reduce boundry cases ret = addAndCheck(b, a, msg); } else { // assert a <= b if (a < 0) { if (b < 0) { // check for negative overflow if (Long.MIN_VALUE - b <= a) { ret = a + b; } else { throw new ArithmeticException(msg); } } else { // oppisite sign addition is always safe ret = a + b; } } else { // assert a >= 0 // assert b >= 0 // check for positive overflow if (a <= Long.MAX_VALUE - b) { ret = a + b; } else { throw new ArithmeticException(msg); } } } return ret; }
From source file:MathUtils.java
public static long multiplyAndCheck(long a, long b) { long ret;//from w w w .j ava 2s . co m String msg = "overflow: multiply"; if (a > b) { // use symmetry to reduce boundry cases ret = multiplyAndCheck(b, a); } else { if (a < 0) { if (b < 0) { // check for positive overflow with negative a, negative b if (a >= Long.MAX_VALUE / b) { ret = a * b; } else { throw new ArithmeticException(msg); } } else if (b > 0) { // check for negative overflow with negative a, positive b if (Long.MIN_VALUE / b <= a) { ret = a * b; } else { throw new ArithmeticException(msg); } } else { // assert b == 0 ret = 0; } } else if (a > 0) { // assert a > 0 // assert b > 0 // check for positive overflow with positive a, positive b if (a <= Long.MAX_VALUE / b) { ret = a * b; } else { throw new ArithmeticException(msg); } } else { // assert a == 0 ret = 0; } } return ret; }
From source file:Main.java
/** * Returns an exact representation of the <a * href="http://mathworld.wolfram.com/BinomialCoefficient.html"> Binomial * Coefficient</a>, "<code>n choose k</code>", the number of * <code>k</code>-element subsets that can be selected from an * <code>n</code>-element set. * <p>// ww w . j a v a2 s.c o m * <Strong>Preconditions</strong>: * <ul> * <li> <code>0 <= k <= n </code> (otherwise * <code>IllegalArgumentException</code> is thrown)</li> * <li> The result is small enough to fit into a <code>long</code>. The * largest value of <code>n</code> for which all coefficients are * <code> < Long.MAX_VALUE</code> is 66. If the computed value exceeds * <code>Long.MAX_VALUE</code> an <code>ArithMeticException * </code> is * thrown.</li> * </ul></p> * * @param n the size of the set * @param k the size of the subsets to be counted * @return <code>n choose k</code> * @throws IllegalArgumentException if preconditions are not met. * @throws ArithmeticException if the result is too large to be represented * by a long integer. */ public static long binomialCoefficient(final int n, final int k) { if (n < k) { throw new IllegalArgumentException("must have n >= k for binomial coefficient (n,k)"); } if (n < 0) { throw new IllegalArgumentException("must have n >= 0 for binomial coefficient (n,k)"); } if ((n == k) || (k == 0)) { return 1; } if ((k == 1) || (k == n - 1)) { return n; } long result = Math.round(binomialCoefficientDouble(n, k)); if (result == Long.MAX_VALUE) { throw new ArithmeticException("result too large to represent in a long integer"); } return result; }
From source file:org.apache.hadoop.hdfs.util.ByteArrayManager.java
/** * @return the least power of two greater than or equal to n, i.e. return * the least integer x with x >= n and x a power of two. * * @throws HadoopIllegalArgumentException * if n <= 0./* w w w.j a va 2s .com*/ */ public static int leastPowerOfTwo(final int n) { if (n <= 0) { throw new HadoopIllegalArgumentException("n = " + n + " <= 0"); } final int highestOne = Integer.highestOneBit(n); if (highestOne == n) { return n; // n is a power of two. } final int roundUp = highestOne << 1; if (roundUp < 0) { final long overflow = ((long) highestOne) << 1; throw new ArithmeticException("Overflow: for n = " + n + ", the least power of two (the least" + " integer x with x >= n and x a power of two) = " + overflow + " > Integer.MAX_VALUE = " + Integer.MAX_VALUE); } return roundUp; }
From source file:cc.redberry.core.number.NumberUtils.java
/** * Computes the integer square root of a number. * * @param n The number./* w w w . j a v a 2s. co m*/ * @return The integer square root, i.e. the largest number whose square * doesn't exceed n. */ public static BigInteger sqrt(BigInteger n) { if (n.signum() >= 0) { final int bitLength = n.bitLength(); BigInteger root = BigInteger.ONE.shiftLeft(bitLength / 2); while (!isSqrtXXX(n, root)) root = root.add(n.divide(root)).divide(TWO); return root; } else throw new ArithmeticException("square root of negative number"); }