[jira] Commented: (MATH-154) MathUtils addAndCheck and subAndCheck for long values
[ http://issues.apache.org/jira/browse/MATH-154?page=comments#action_12426209 ] Brent Worden commented on MATH-154: --- Can we change it to subtractAndCheck? Also, it might be useful to add two additional methods to just check for operation safety and not throw an exception. This would be useful in the RNG methods Remi proposed where all that was needed was whether or not an operation was valid. This would avoid creating an expensive exception for simple case logic. MathUtils addAndCheck and subAndCheck for long values - Key: MATH-154 URL: http://issues.apache.org/jira/browse/MATH-154 Project: Commons Math Issue Type: Improvement Affects Versions: Nightly Builds, 1.1 Final Reporter: Remi Arntzen Fix For: 1.2 Final public static long addAndCheck(long x, long y) { BigInteger s = BigInteger.valueOf(x).add(BigInteger.valueOf(y); if (s.bitLength() + 1 Long.SIZE) { throw new ArithmeticException(overflow: add); } return s.longValue(); } public static long subAndCheck(long x, long y) { BigInteger s = BigInteger.valueOf(x).subtract(BigInteger.valueOf(y)); if (s.bitLength() + 1 Long.SIZE) { throw new ArithmeticException(overflow: add); } return s.longValue(); } -- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa - For more information on JIRA, see: http://www.atlassian.com/software/jira - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
[jira] Commented: (MATH-154) MathUtils addAndCheck and subAndCheck for long values
[ http://issues.apache.org/jira/browse/MATH-154?page=comments#action_12426061 ] Phil Steitz commented on MATH-154: -- Looks like a good addition to MathUtils. An alternative to the first implementations would be to convert to floats; but I suspect the second versions will perform better, so I will commit this once we have: a) test cases (see testAddAndCheck in o.a.c.math.util.MathUtilsTest for an example) b) change the RuntimeExceptions to ArithmeticExceptions (as in first impl, consistent with int versions) Patches in diff format would be appreciated. Thanks! MathUtils addAndCheck and subAndCheck for long values - Key: MATH-154 URL: http://issues.apache.org/jira/browse/MATH-154 Project: Commons Math Issue Type: Improvement Affects Versions: Nightly Builds, 1.1 Final Reporter: Remi Arntzen Fix For: 1.2 Final public static long addAndCheck(long x, long y) { BigInteger s = BigInteger.valueOf(x).add(BigInteger.valueOf(y); if (s.bitLength() + 1 Long.SIZE) { throw new ArithmeticException(overflow: add); } return s.longValue(); } public static long subAndCheck(long x, long y) { BigInteger s = BigInteger.valueOf(x).subtract(BigInteger.valueOf(y)); if (s.bitLength() + 1 Long.SIZE) { throw new ArithmeticException(overflow: add); } return s.longValue(); } -- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa - For more information on JIRA, see: http://www.atlassian.com/software/jira - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
[jira] Commented: (MATH-154) MathUtils addAndCheck and subAndCheck for long values
[ http://issues.apache.org/jira/browse/MATH-154?page=comments#action_12425314 ] Remi Arntzen commented on MATH-154: --- a better implementation: long addAndCheck(long a, long b) { long max = Math.max(a, b); long min = Math.min(a, b); long sum = a + b; String overflow = Overflow: add; if (max == 0 || min == 0) ( return sum; } else if (max 0 min 0) { if (Long.MAX_VALUE - max min) { throw new RuntimeException(overflow); } } else if (max 0 min 0) { if (Long.MIN_VALUE - min max) { throw new RuntimeException(overflow); } } else { return sum; } } long subAndCheck(long a, long b) { long max = Math.max(a, b); long min = Math.min(a, b); long diff = a - b; String overflow = Overflow: add; if (max == 0 || min == 0) { return diff; } else if (max 0 min 0) { if (max == a) { if (max - Long.MAX_VALUE min) { throw new RuntimeException(overflow); } } else if (max == b) { if (min - Long.MIN_VALUE max) { throw new RuntimeException(overflow); } } } else { return sum; } } MathUtils addAndCheck and subAndCheck for long values - Key: MATH-154 URL: http://issues.apache.org/jira/browse/MATH-154 Project: Commons Math Issue Type: Improvement Affects Versions: Nightly Builds, 1.1 Final Reporter: Remi Arntzen public static long addAndCheck(long x, long y) { BigInteger s = BigInteger.valueOf(x).add(BigInteger.valueOf(y); if (s.bitLength() + 1 Long.SIZE) { throw new ArithmeticException(overflow: add); } return s.longValue(); } public static long subAndCheck(long x, long y) { BigInteger s = BigInteger.valueOf(x).subtract(BigInteger.valueOf(y)); if (s.bitLength() + 1 Long.SIZE) { throw new ArithmeticException(overflow: add); } return s.longValue(); } -- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa - For more information on JIRA, see: http://www.atlassian.com/software/jira - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]