• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Math.abs(Integer.MIN_VALUE) return a negative vaue,why?

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class ADirtyOne1
{
public static void main(String args[])
{
System.out.println(Math.abs(Integer.MIN_VALUE));
}
}
I thought the output is 2147483648,at least ,it should be positive value.
but When compile and run the code ,the output is :-2147483648.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Integer.MIN_VALUE = -2147483648;
it seems that Math.abs(-2147483648) return 2147483648;
but Integer.MAX_VALUE = 2147483647;
The rule is simple:
Integer.MAX_VALUE + j --> Integer.MIN_VALUE + (j-1);
2147483647 + 3 --> -2147483648 + (3-1);
2147483647 + 3 --> -2147483646;

------------------
Alex J.Grig
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the API, abs() checks to see if the value is positive. If so it returns the number, if not, it returns -1 * the number. Getting into the bits, -1 * Integer.MIN_VALUE = Integer.MIN_VALUE.
Here's why...(I'll use only four bits to keep it simple) -1 in binary (with two's complement notation) is
1111. To negate, you should subtract 1, then flip all the bits.
1111 - 1 = 1110 -> (flip bits) -> 0001 //works!
The largest neg number we can represent with four bits is -8, in two's complement:
1000 -> (flip) -> 0111 + 1 = 1000
Study the last line and the rest follows, but since I used to hate when profs used that line on me, I'll finish. To negate, subtract 1 and flip bits:
1000 - 1 = 0111 -> (flip) -> 1000 //evaluates to -8!
If you still don't believe me, try this code:

public class Test {
public static void main (String args[]) {
System.out.println(Integer.MIN_VALUE * -1);
System.out.println(-2147483648 * -1);
}
}
System.out : -2147483648
System.out : -2147483648

Here's a cut 'n paste from the JDK 1.3 API
abs
public static int abs(int a)
Returns the absolute value of an int value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.
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.
Parameters:
a - an int value.
Returns:
the absolute value of the argument.
See Also:
Integer.MIN_VALUE
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you supply +5, you get 5
if you supply -5, you get 5.
In the same token, if you supply Integer.MAX_VALUE (a positive int equal to 2147483647), you get 2147483647.
But you if supply, Integer.MIN_VALUE(a negative int equal to -2147483648), you would hope to get 2147483648.
But, if you think about it, there is no POSITIVE INT 2147483648
in java. The maximum int value allowed is only 2147483647.
So, it wraps around to -2147483648.
Hence, your output.
Madan
[This message has been edited by Madan, Gopal (edited November 01, 2001).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic