sudha siva

Ranch Hand

Posts: 30

posted 15 years ago

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.

Thanks

sudha

{

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.

Thanks

sudha

Valentin Crettaz

Gold Digger

Sheriff

Sheriff

Posts: 7610

posted 15 years ago

READ THE API !!!

From Math.abs():

HIH

------------------

Valentin Crettaz

Sun Certified Programmer for Java 2 Platform

From Math.abs():

...

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

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML

[Blog] [Blogroll] [My Reviews] My Linked In

Malar Ravi

Ranch Hand

Posts: 51

Uma Viswanathan

Ranch Hand

Posts: 126

Uma Viswanathan

Ranch Hand

Posts: 126

Uma Viswanathan

Ranch Hand

Posts: 126

Anonymous

Ranch Hand

Posts: 18944

posted 15 years ago

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.

And regarding the

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)

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

sudha siva

Ranch Hand

Posts: 30

posted 15 years ago

Hai Malar,

Sorry for the late reply. Here is one link for a list of mock exmas.

http://www.javaranch.com/maha/_Mock_Exams/_mock_exams.html

I'm getting questions from these list of mock exams.

Thanks

sudha

Sorry for the late reply. Here is one link for a list of mock exmas.

http://www.javaranch.com/maha/_Mock_Exams/_mock_exams.html

I'm getting questions from these list of mock exams.

Thanks

sudha

Maulin Vasavada

Ranch Hand

Posts: 1873

posted 15 years ago

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

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