/** Increment a number, detecting overflows. * @param n number to increment * @return n+1 if no overflows occur * @exception MathArithmeticException if an overflow occurs * @since 3.4 */ public static long incrementExact(final long n) throws MathArithmeticException { if (n == Long.MAX_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, n, 1); } return n + 1; }
} catch (MathArithmeticException mae) { mae.getContext().addMessage(LocalizedFormats.OVERFLOW); mae.getContext().addMessage(LocalizedFormats.BASE, k); mae.getContext().addMessage(LocalizedFormats.EXPONENT, e);
} catch (MathArithmeticException mae) { mae.getContext().addMessage(LocalizedFormats.OVERFLOW); mae.getContext().addMessage(LocalizedFormats.BASE, k); mae.getContext().addMessage(LocalizedFormats.EXPONENT, e);
/** Increment a number, detecting overflows. * @param n number to increment * @return n+1 if no overflows occur * @exception MathArithmeticException if an overflow occurs * @since 3.4 */ public static int incrementExact(final int n) throws MathArithmeticException { if (n == Integer.MAX_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, n, 1); } return n + 1; }
/** Decrement a number, detecting overflows. * @param n number to decrement * @return n-1 if no overflows occur * @exception MathArithmeticException if an overflow occurs * @since 3.4 */ public static long decrementExact(final long n) throws MathArithmeticException { if (n == Long.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_SUBTRACTION, n, 1); } return n - 1; }
/** Decrement a number, detecting overflows. * @param n number to decrement * @return n-1 if no overflows occur * @exception MathArithmeticException if an overflow occurs * @since 3.4 */ public static int decrementExact(final int n) throws MathArithmeticException { if (n == Integer.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_SUBTRACTION, n, 1); } return n - 1; }
/** Convert a long to interger, detecting overflows * @param n number to convert to int * @return integer with same valie as n if no overflows occur * @exception MathArithmeticException if n cannot fit into an int * @since 3.4 */ public static int toIntExact(final long n) throws MathArithmeticException { if (n < Integer.MIN_VALUE || n > Integer.MAX_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW); } return (int) n; }
/** Multiply two numbers, detecting overflows. * @param a first number to multiply * @param b second number to multiply * @return a*b if no overflows occur * @exception MathArithmeticException if an overflow occurs * @since 3.4 */ public static int multiplyExact(final int a, final int b) { if (((b > 0) && (a > Integer.MAX_VALUE / b || a < Integer.MIN_VALUE / b)) || ((b < -1) && (a > Integer.MIN_VALUE / b || a < Integer.MAX_VALUE / b)) || ((b == -1) && (a == Integer.MIN_VALUE))) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_MULTIPLICATION, a, b); } return a * b; }
/** Multiply two numbers, detecting overflows. * @param a first number to multiply * @param b second number to multiply * @return a*b if no overflows occur * @exception MathArithmeticException if an overflow occurs * @since 3.4 */ public static long multiplyExact(final long a, final long b) { if (((b > 0l) && (a > Long.MAX_VALUE / b || a < Long.MIN_VALUE / b)) || ((b < -1l) && (a > Long.MIN_VALUE / b || a < Long.MAX_VALUE / b)) || ((b == -1l) && (a == Long.MIN_VALUE))) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_MULTIPLICATION, a, b); } return a * b; }
/** Add two numbers, detecting overflows. * @param a first number to add * @param b second number to add * @return a+b if no overflows occur * @exception MathArithmeticException if an overflow occurs * @since 3.4 */ public static int addExact(final int a, final int b) throws MathArithmeticException { // compute sum final int sum = a + b; // check for overflow if ((a ^ b) >= 0 && (sum ^ b) < 0) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, a, b); } return sum; }
/** Add two numbers, detecting overflows. * @param a first number to add * @param b second number to add * @return a+b if no overflows occur * @exception MathArithmeticException if an overflow occurs * @since 3.4 */ public static long addExact(final long a, final long b) throws MathArithmeticException { // compute sum final long sum = a + b; // check for overflow if ((a ^ b) >= 0 && (sum ^ b) < 0) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, a, b); } return sum; }
/** * Multiply two integers, checking for overflow. * * @param x Factor. * @param y Factor. * @return the product {@code x * y}. * @throws MathArithmeticException if the result can not be * represented as an {@code int}. * @since 1.1 */ public static int mulAndCheck(int x, int y) throws MathArithmeticException { long m = ((long)x) * ((long)y); if (m < Integer.MIN_VALUE || m > Integer.MAX_VALUE) { throw new MathArithmeticException(); } return (int)m; }
/** * Subtract two integers, checking for overflow. * * @param x Minuend. * @param y Subtrahend. * @return the difference {@code x - y}. * @throws MathArithmeticException if the result can not be represented * as an {@code int}. * @since 1.1 */ public static int subAndCheck(int x, int y) throws MathArithmeticException { long s = (long)x - (long)y; if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_SUBTRACTION, x, y); } return (int)s; }
/** Subtract two numbers, detecting overflows. * @param a first number * @param b second number to subtract from a * @return a-b if no overflows occur * @exception MathArithmeticException if an overflow occurs * @since 3.4 */ public static int subtractExact(final int a, final int b) { // compute subtraction final int sub = a - b; // check for overflow if ((a ^ b) < 0 && (sub ^ b) >= 0) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_SUBTRACTION, a, b); } return sub; }
/** Subtract two numbers, detecting overflows. * @param a first number * @param b second number to subtract from a * @return a-b if no overflows occur * @exception MathArithmeticException if an overflow occurs * @since 3.4 */ public static long subtractExact(final long a, final long b) { // compute subtraction final long sub = a - b; // check for overflow if ((a ^ b) < 0 && (sub ^ b) >= 0) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_SUBTRACTION, a, b); } return sub; }
/** * Add two integers, checking for overflow. * * @param x an addend * @param y an addend * @return the sum {@code x+y} * @throws MathArithmeticException if the result can not be represented * as an {@code int}. * @since 1.1 */ public static int addAndCheck(int x, int y) throws MathArithmeticException { long s = (long)x + (long)y; if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, x, y); } return (int)s; }
/** * Add two long integers, checking for overflow. * * @param a Addend. * @param b Addend. * @param pattern Pattern to use for any thrown exception. * @return the sum {@code a + b}. * @throws MathArithmeticException if the result cannot be represented * as a {@code long}. * @since 1.2 */ private static long addAndCheck(long a, long b, Localizable pattern) throws MathArithmeticException { final long result = a + b; if (!((a ^ b) < 0 | (a ^ result) >= 0)) { throw new MathArithmeticException(pattern, a, b); } return result; }
/** * Returns the first argument with the sign of the second argument. * * @param magnitude Magnitude of the returned value. * @param sign Sign of the returned value. * @return a value with magnitude equal to {@code magnitude} and with the * same sign as the {@code sign} argument. * @throws MathArithmeticException if {@code magnitude == Long.MIN_VALUE} * and {@code sign >= 0}. */ public static long copySign(long magnitude, long sign) throws MathArithmeticException { if ((magnitude >= 0 && sign >= 0) || (magnitude < 0 && sign < 0)) { // Sign is OK. return magnitude; } else if (sign >= 0 && magnitude == Long.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW); } else { return -magnitude; // Flip sign. } } /**
/** * Returns the first argument with the sign of the second argument. * * @param magnitude Magnitude of the returned value. * @param sign Sign of the returned value. * @return a value with magnitude equal to {@code magnitude} and with the * same sign as the {@code sign} argument. * @throws MathArithmeticException if {@code magnitude == Short.MIN_VALUE} * and {@code sign >= 0}. */ public static short copySign(short magnitude, short sign) throws MathArithmeticException { if ((magnitude >= 0 && sign >= 0) || (magnitude < 0 && sign < 0)) { // Sign is OK. return magnitude; } else if (sign >= 0 && magnitude == Short.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW); } else { return (short) -magnitude; // Flip sign. } }
/** * Returns the first argument with the sign of the second argument. * * @param magnitude Magnitude of the returned value. * @param sign Sign of the returned value. * @return a value with magnitude equal to {@code magnitude} and with the * same sign as the {@code sign} argument. * @throws MathArithmeticException if {@code magnitude == Byte.MIN_VALUE} * and {@code sign >= 0}. */ public static byte copySign(byte magnitude, byte sign) throws MathArithmeticException { if ((magnitude >= 0 && sign >= 0) || (magnitude < 0 && sign < 0)) { // Sign is OK. return magnitude; } else if (sign >= 0 && magnitude == Byte.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW); } else { return (byte) -magnitude; // Flip sign. } }