I prefer the method naming safeAdd/safeNegate/safeMultiply (I think they are a lot clearer), but can live with the ones proposed (which are somewhat linked to BigInteger/BigDecimal).
I would like to see source code comments explaining why the algorithm works in the final version in OpenJDK. Thats because OpenJDK code is used by everyone to read and understand what is good Java code, and for debugging. Being given a clue to understand the algorithm is highly desirable. It would be desirable to see safe increment and decrement methods as well. These are faster than just an add/subtract (simpler implementation). Most of the methods do not specify the arguments in the exception, but the multiply methods do. It should be consistent one way or the other. It may be worth considering (by testing) if methods taking one long and one int are worthwhile. More efficient implementations of the algorithms may be possible, but at the expense of additional API size. Separately but related, there are missing methods in BigInteger, longValueExact() and intValueExact() Also see here if you haven't already: http://blogs.oracle.com/darcy/entry/jvmlang_numbers Stephen On 2 February 2012 20:15, Roger Riggs <roger.ri...@oracle.com> wrote: > There is a need for arithmetic operations that throw exceptions > when the results overflow the representation of int or long. > > The CR is 6708398: Support integer overflow > <http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6708398> > > Please review this webrev > <http://cr.openjdk.java.net/%7Erriggs/CR6708398/webrev/> to add static > methods in java.lang.Math > to support addExact(), subtractExact(), negateExact(), multiplyExact(), > and toIntExact() for int and long primitive types. > > Thanks, Roger Riggs