This week's book giveaway is in the Big Data forum. We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line! See this thread for details.

public class ADirtyOne1 { public static void main(String args[]) { System.out.println(Math.abs(-34)); System.out.println(Integer.MIN_VALUE); System.out.println(Math.abs(Integer.MIN_VALUE));// line 3 System.out.println(Float.MIN_VALUE); System.out.println(Math.round(Float.MIN_VALUE)); //line 5 } In the above code the abs value of line 3 is negative. But the abs() should give the value without any negative sign. (Am I right).Can anyone claer my doubt and try to explain the round() in line 5.

... 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. ...

HIH ------------------ Valentin Crettaz Sun Certified Programmer for Java 2 Platform

"the abs value of line 3 is negative...' Quote from API: "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."

Uma Viswanathan
Ranch Hand

Joined: Jun 14, 2001
Posts: 126

posted

0

Sorry, i did not see Valentin's reply. By the time, i had started replying, there was no reply. By the time posted it, saw his reply...

Uma Viswanathan
Ranch Hand

Joined: Jun 14, 2001
Posts: 126

posted

0

Float.MIN_VALUE = 1.4E-45 which is equivalent to 0.000000.....4 When you round it, you will get 0. Hope this helps... Uma

Hi sudha, This is what API says about Math.abs() and Math.double(). 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. So, You are referring to the special case here. And regarding the round() public static int round(float a) Returns the closest int to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type int. In other words, the result is equal to the value of the expression: (int)Math.floor(a + 0.5f) So, applying this you will get the required result I guess people had already answered your question. HIH Chandra! ------------------ "Where there is a Will, there is a Way." Sun Certified Programmer for the Javaï¿½ 2 Platform

hi all, well if we just forget about API, my logical argument would be something like this.... if we give min value to abs() then say it is able to convert it to a positive value. now, the problem is we have one more number in negative range which is not there in positive range due to machine representations (in any language). other point is if we try to increment max value then it gets rounded (i dont mean round() method here or probably wrapping is a better term to use here) in java. if we combine these two points then, first it converts -ve min value to abs() positive value, as that number can't be represented it is wrapped to least -ve value and so we get -ve ans instead of +ve regards maulin