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

The following examples show how to use org.apache.commons.math3.exception.util.LocalizedFormats#OVERFLOW_IN_ADDITION . 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: ArithmeticUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Subtract two long integers, checking for overflow.
 *
 * @param a Value.
 * @param b Value.
 * @return the difference {@code a - b}.
 * @throws MathArithmeticException if the result can not be represented as a
 * {@code long}.
 * @since 1.2
 */
public static long subAndCheck(long a, long b) throws MathArithmeticException {
    long ret;
    if (b == Long.MIN_VALUE) {
        if (a < 0) {
            ret = a - b;
        } else {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, a, -b);
        }
    } else {
        // use additive inverse
        ret = addAndCheck(a, -b, LocalizedFormats.OVERFLOW_IN_ADDITION);
    }
    return ret;
}
 
Example 2
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Subtract two long integers, checking for overflow.
 *
 * @param a Value.
 * @param b Value.
 * @return the difference {@code a - b}.
 * @throws MathArithmeticException if the result can not be represented as a
 * {@code long}.
 * @since 1.2
 */
public static long subAndCheck(long a, long b) throws MathArithmeticException {
    long ret;
    if (b == Long.MIN_VALUE) {
        if (a < 0) {
            ret = a - b;
        } else {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, a, -b);
        }
    } else {
        // use additive inverse
        ret = addAndCheck(a, -b, LocalizedFormats.OVERFLOW_IN_ADDITION);
    }
    return ret;
}
 
Example 3
Source File: FastMath.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** 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;

}
 
Example 4
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Subtract two long integers, checking for overflow.
 *
 * @param a Value.
 * @param b Value.
 * @return the difference {@code a - b}.
 * @throws MathArithmeticException if the result can not be represented as a
 * {@code long}.
 * @since 1.2
 */
public static long subAndCheck(long a, long b) throws MathArithmeticException {
    long ret;
    if (b == Long.MIN_VALUE) {
        if (a < 0) {
            ret = a - b;
        } else {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, a, -b);
        }
    } else {
        // use additive inverse
        ret = addAndCheck(a, -b, LocalizedFormats.OVERFLOW_IN_ADDITION);
    }
    return ret;
}
 
Example 5
Source File: FastMath.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** 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;

}
 
Example 6
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Subtract two long integers, checking for overflow.
 *
 * @param a Value.
 * @param b Value.
 * @return the difference {@code a - b}.
 * @throws MathArithmeticException if the result can not be represented as a
 * {@code long}.
 * @since 1.2
 */
public static long subAndCheck(long a, long b) {
    long ret;
    if (b == Long.MIN_VALUE) {
        if (a < 0) {
            ret = a - b;
        } else {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, a, -b);
        }
    } else {
        // use additive inverse
        ret = addAndCheck(a, -b, LocalizedFormats.OVERFLOW_IN_ADDITION);
    }
    return ret;
}
 
Example 7
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Subtract two long integers, checking for overflow.
 *
 * @param a Value.
 * @param b Value.
 * @return the difference {@code a - b}.
 * @throws MathArithmeticException if the result can not be represented as a
 * {@code long}.
 * @since 1.2
 */
public static long subAndCheck(long a, long b) throws MathArithmeticException {
    long ret;
    if (b == Long.MIN_VALUE) {
        if (a < 0) {
            ret = a - b;
        } else {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, a, -b);
        }
    } else {
        // use additive inverse
        ret = addAndCheck(a, -b, LocalizedFormats.OVERFLOW_IN_ADDITION);
    }
    return ret;
}
 
Example 8
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Subtract two long integers, checking for overflow.
 *
 * @param a Value.
 * @param b Value.
 * @return the difference {@code a - b}.
 * @throws MathArithmeticException if the result can not be represented as a
 * {@code long}.
 * @since 1.2
 */
public static long subAndCheck(long a, long b) {
    long ret;
    if (b == Long.MIN_VALUE) {
        if (a < 0) {
            ret = a - b;
        } else {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, a, -b);
        }
    } else {
        // use additive inverse
        ret = addAndCheck(a, -b, LocalizedFormats.OVERFLOW_IN_ADDITION);
    }
    return ret;
}
 
Example 9
Source File: FastMath.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** 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;

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

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

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

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

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

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