• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

abs() method

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sudha, can you please tell from which site you are finding this kind of questions.
Thanks,
Malar.
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic