Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# abs() method

Sakthi Kani
Ranch Hand
Posts: 98
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
Because of overflow.
Here is another apparant anomaly that is in fact not:

Parameswaran Thangavel
Ranch Hand
Posts: 485
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
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.