Originally posted by Shivaji Marathe:
Sonir :
What does Math.abs() do ?
Originally posted by sonir shah:
Returns an Absolute value.
Sonir
Originally posted by Shivaji Marathe:
Actually I am surprised that math.abs(Long.min_value ) prints a negative number. Is this a normal behavior or is this an " un documented feature?"
Originally posted by Maulin, Vasavada:
hi sonir,
the problem here is this.
as per API says, when the value is MIN_VALUE and the datatype is int or long then it crosses the limit if we make it +ve as there is one number less in +ve than in +ve. so, when we do that it wraps the number again and make it -ve.
thus, comparision to >0 returns false.
for byte & short there is no method in Math.abs() list. so short and byte are first converted to int and then it works. here Byte.MIN_VALUE is -128 but still it works as it is fully representable in int. same for short. and so making them abs() returns int > 0.
however, if we had Integer.MIN_VALUE or Long.MIN_VALUE then it would return the same number which is -ve due to the reason explained above.
this reasoning doesnt apply to float and double. as the Float.MIN_VALUE is +/-1.4E-45 and similar thing for double. so, when u apply abs() to Float.MIN_VALUE or Double.MIN_VALUE it gives u the +ve number and >0 comparison is true.
so, u get results u r having.
regards
maulin.