Review: JDK 8 CR for Support Integer overflow updated

Jeff Hain jeffhain at rocketmail.com
Mon Feb 13 23:41:19 UTC 2012


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.
  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.)


- 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).

- 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)]

Jeff



________________________________
 De : Roger Riggs <Roger.Riggs at oracle.com>
À : core-Libs-Dev <core-libs-dev at openjdk.java.net> 
Envoyé le : Samedi 11 février 2012 18h31
Objet : Review: JDK 8 CR for Support Integer overflow updated
 
Updated the webrev for CR6708398:
        http://cr.openjdk.java.net/~rriggs/6708398.2
- 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