Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Math.abs()

  1. Jan 5, 2012 #1

    Office_Shredder

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

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

    Wondering if anyone can explain what abs does that makes it fail on the minimum integer value
     
    Last edited by a moderator: May 5, 2017
  2. jcsd
  3. Jan 5, 2012 #2

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Integer overflow. Check the magnitude of Integer.MIN_VALUE and Integer.MAX_VALUE.

    I'm surprised they chose that, instead of raising an exception. Ah well.
     
  4. Jan 5, 2012 #3

    Office_Shredder

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Thanks!
     
  5. Jan 5, 2012 #4

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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".
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java Math.abs()
  1. Java Resources (Replies: 4)

  2. [Java] PNGException (Replies: 2)

  3. JAVA GridLayout (Replies: 1)

  4. Java interpreter (Replies: 8)

  5. Java Cylinders (Replies: 2)

Loading...