jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abs() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abs() method" Watch "abs() method" New topic
Author

abs() method

sudha siva
Ranch Hand

Joined: Sep 18, 2001
Posts: 30
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
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
READ THE API !!!
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

Joined: Oct 15, 2001
Posts: 51
Hi Sudha, can you please tell from which site you are finding this kind of questions.
Thanks,
Malar.
Uma Viswanathan
Ranch Hand

Joined: Jun 14, 2001
Posts: 126
"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
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
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
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

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

Joined: Sep 18, 2001
Posts: 30
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
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abs() method