commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "derphead (JIRA)" <j...@apache.org>
Subject [jira] [Created] (MATH-1035) Better implementation of checked addition
Date Tue, 17 Sep 2013 17:12:53 GMT
derphead created MATH-1035:
------------------------------

             Summary: Better implementation of checked addition
                 Key: MATH-1035
                 URL: https://issues.apache.org/jira/browse/MATH-1035
             Project: Commons Math
          Issue Type: Improvement
            Reporter: derphead
            Priority: Minor


The implementation of org.apache.commons.math3.util.ArithmeticUtils.addAndCheck(long, long,
Localizable) is inefficient and confusing. Here's a better way:

{code}
private static long addAndCheck(long a, long b, Localizable pattern) throws MathArithmeticException
{
    final long SIGN_BIT = 1L << 63;
    long result = a + b;
    // If a and b have opposite sign, the result will not overflow.
    // If a and b have the same sign, the result overflowed if it has different sign.
    if (((~(a ^ b)) & (a ^ result) & SIGN_BIT) != 0) {
        throw new MathArithmeticException(pattern, a, b);
    }
    return result;
}
{code}

This bug tracker is bad btw. It raped my browser.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Mime
View raw message