Review: JDK 8 CR for Support Integer overflow updated

Roger Riggs Roger.Riggs at
Tue Feb 14 21:01:19 UTC 2012

On 02/13/2012 06:41 PM, Jeff Hain wrote:
> Hello.
> - It could be great to have versions of these methods that don't throw an ArithmeticException in case of overflow, but would return the closest value (XXX.MAX_VALUE or XXX.MIN_VALUE).
>    A common use-case I see (and have) for this is when dealing with dates and durations (*), in which case Long.MAX_VALUE or such is so far that it's equal to use that instead of a theoretically higher deadline or timeout.
Saturating arithmetic can be useful but there have been few requests.
>    The throwing methods could also be named "xxxInRange" (multiplyInRange, etc.), and the others "xxxToRange"(but non-throwing "toInt" method could be named "toIntRange", not to repeat the "to").
>    (I don't really like the term "exact", which supposes modulo arithmetic is not exact in its kind, and it doesn't contain the supposition that the result should be in range.)
Choosing names can be difficult to get the right meaning.  I don't see 
these are an improvement.
The current xxxExact naming reflected similar use in BigDecimal.

In your subsequent email, I found that the asXXX and toXXX variants to 
be too similar to
make it clear which throws and which does not or other differences 
unless it were a pervasive
pattern that all developers would know and use.
> - Replacing current implementation of "Math.abs(int)" with "(value^(value>>31))-(value>>31)", and equivalent for "Math.abs(long)", seems to speeds things up nicely, which is interesting for so low-level treatments.
>    These optimizations could be piggybacked along (unless there is a reason not to do them, which would explain why they haven't been done yet).
Interesting, my impression from some trials was the Hotspot already had 
some speedup for abs().
> - multiplyToRange(int,int) could be coded efficiently as "return (int)(x * (double)y);"
> (*) JodaTime uses throwing versions, but they do exact time arithmetic; I m talking about low-level timing treatments, like scheduling and such, which don't provide the results but just use them.
>       [BTW if the maintainer reads this, JodaTime's FieldUtils.safeMultiply(long,long) doesn't handle (Long.MIN_VALUE,-1)]
Thanks, Roger

> Jeff
> ________________________________
>   De : Roger Riggs<Roger.Riggs at>
> À : core-Libs-Dev<core-libs-dev at>
> Envoyé le : Samedi 11 février 2012 18h31
> Objet : Review: JDK 8 CR for Support Integer overflow updated
> Updated the webrev for CR6708398:
> - Added a paragraph to the class javadoc for Math and StrictMath to
>     introduce the exact arithmetic methods and their uses.
> - Editorial correction to first sentence of each method to consistent use "Returns".
> - Added Tests for the StrictMath methods  (they are the same tests as for Math)
> Thanks, Roger

More information about the core-libs-dev mailing list