Java Code Examples for org.apache.commons.math3.exception.util.LocalizedFormats#OVERFLOW_IN_SUBTRACTION

The following examples show how to use org.apache.commons.math3.exception.util.LocalizedFormats#OVERFLOW_IN_SUBTRACTION . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FastMath.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** 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;

}
 
Example 2
Source File: FastMath.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** 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;

}
 
Example 3
Source File: FastMath.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** 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;

}
 
Example 4
Source File: FastMath.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** 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;

}
 
Example 5
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: FastMath.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/** 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;

}
 
Example 7
Source File: FastMath.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/** 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;

}
 
Example 8
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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;
}
 
Example 9
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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) {
    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;
}
 
Example 10
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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) {
    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;
}
 
Example 12
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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;
}
 
Example 13
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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;
}
 
Example 14
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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;
}
 
Example 15
Source File: FastMath.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/** 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;

}
 
Example 16
Source File: FastMath.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/** 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;

}