Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abs() method

 
Sakthi Kani
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually,

abs() method returns a non-negative value of the argument.
--------------------------------------------------
System.out.println(Math.abs(Byte.MIN_VALUE));
System.out.println(Math.abs(Short.MIN_VALUE));
System.out.println(Math.abs(Float.MIN_VALUE));
System.out.println(Math.abs(Double.MIN_VALUE));
----------------------------------------------------

These four lines are returning positive value.

----------------------------------------------------
System.out.println(Math.abs(Integer.MIN_VALUE));
System.out.println(Math.abs(Long.MIN_VALUE));
----------------------------------------------------
But in this two cases it is returning negative value.why???

Can anyone clear my doubt....
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because of overflow.
Here is another apparant anomaly that is in fact not:
http://qa.jtiger.org/GetQAndA.action?qids=15&showAnswers=true
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i gone through the link but i cant get it.
can u please explain me with some examples
 
Raghu Shree
Ranch Hand
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
The abs() method has following forms

int abs(int);
long abs(long);
float abs(float); and

Byte.MIN_VALUE return -128
Integer.MIN_VALUE returns -2147483648
Long.MIN_VALUE returns -9223372036854775808

abs(-128) overload abs(int) method so It doesn't return negative value/
abs(-2147483648) overload abs(int) method and return the value 2147483648. But this value greater than integer range. so It is return negative value.
abs( -9223372036854775808) overlaod abs(long) method and return the value -9223372036854775808. But this is larger than long range. So It also returns negative value.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic