Understanding Java's Math.abs() Method: Solving the Minimum Integer Value Issue

  • Context: Java 
  • Thread starter Thread starter Office_Shredder
  • Start date Start date
  • Tags Tags
    Java
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 4K views
Messages
5,706
Reaction score
1,592
When you take the absolute value of an integer value:

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html#abs(int)

Note that if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative.

Wondering if anyone can explain what abs does that makes it fail on the minimum integer value
 
Last edited by a moderator:
Physics news on Phys.org
Hurkyl said:
I'm surprised they chose that, instead of raising an exception. Ah well.

At least they chose some definite value, which is an improvement on the C standard.

Maybe this is an example of the design principle, "don't throw exceptions for things that are impossible to fix cleanly even if you catch them".