This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Math.abs() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Math.abs()" Watch "Math.abs()" New topic
Author

Math.abs()

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Why is Math.abs(Integer.MIN_VALUE) not a positive value like Math.abs(Byte.MIN_VALUE) is?
class Q009 {
public static void main ( String args [ ] ) {
System.out.println ( ( Math.abs( Integer.MIN_VALUE ) ) );
System.out.println ( Integer.MIN_VALUE );

System.out.println ( ( Math.abs( Byte.MIN_VALUE ) ) );
System.out.println ( Byte.MIN_VALUE );
}
}
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Sorry, I forgot to state the result:
C:\Java\EigeneJavaProgramme>java Q009
-2147483648
-2147483648
128
-128
Why is Math.abs(Integer.MIN_VALUE) not positive?
Igor Ko
Ranch Hand

Joined: Jun 24, 2002
Posts: 90
I think explanation here is internal
representation of signed integer in computer.
For example byte range is
-128 -127 .... -1, 0, 1 ... 127.
Operation -x is bits inversion of x, plus one.
It works well, ... but min value -128 ==
1000 0000 in binary form, convert into itself:
bits inversion is 0111 1111, plus one is 1000 0000.
It's problem of such number representation -
no abs (positive number) for min negative number.
So you can't get abs of -128,
the "must be" result 128 is simple out of byte range.
It's the same for min value of other integer types:
short (two bytes), int (4 bytes) and long(8 bytes)
[ October 28, 2002: Message edited by: Igor K ]
[ October 28, 2002: Message edited by: Igor K ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
With other words: 2147483648 is not an int, therefore you get an overflow.
Try Math.abs((long)Integer.MIN_VALUE) instead, this will call the overloaded method with a long as return value.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Section 4.2.2 of the JLS mentions that "[t]he built-in integer operators do not indicate overflow or underflow in any way." So what the heck does that mean exactly, right?
A couple of sources you may want to take a look at:
  • http://math.hws.edu/javanotes/c9/s1.html - The last section of the page
  • Roedy Green's Overflow Java Gotcha Explanation which highlights the problem, but doesn't really explain the situation in detail (but it's still a nice list of gotchas to read up on).
  • Roedy Green's Java Glossary on overflow
  • Does any of that help to clarify the situation?


    [How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Math.abs()
     
    Similar Threads
    Math.abs() method seems functioning wrong.
    Math.abs(Integer.MIN_VALUE)) not returning abs val
    abs() method
    Queries
    MATH Problem - Shor.MIN_VALUE