You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "derphead (JIRA)" <ji...@apache.org> on 2013/09/17 19:12:53 UTC
[jira] [Created] (MATH-1035) Better implementation of checked
addition
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