• 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: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually,

abs() method returns a non-negative value of the argument.
--------------------------------------------------
System.out.println(Math.abs(Byte.MIN_VALUE));
System.out.println(Math.abs(Short.MIN_VALUE));
System.out.println(Math.abs(Float.MIN_VALUE));
System.out.println(Math.abs(Double.MIN_VALUE));
----------------------------------------------------

These four lines are returning positive value.

----------------------------------------------------
System.out.println(Math.abs(Integer.MIN_VALUE));
System.out.println(Math.abs(Long.MIN_VALUE));
----------------------------------------------------
But in this two cases it is returning negative value.why???

Can anyone clear my doubt....
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because of overflow.
Here is another apparant anomaly that is in fact not:
http://qa.jtiger.org/GetQAndA.action?qids=15&showAnswers=true
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i gone through the link but i cant get it.
can u please explain me with some examples
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
The abs() method has following forms

int abs(int);
long abs(long);
float abs(float); and

Byte.MIN_VALUE return -128
Integer.MIN_VALUE returns -2147483648
Long.MIN_VALUE returns -9223372036854775808

abs(-128) overload abs(int) method so It doesn't return negative value/
abs(-2147483648) overload abs(int) method and return the value 2147483648. But this value greater than integer range. so It is return negative value.
abs( -9223372036854775808) overlaod abs(long) method and return the value -9223372036854775808. But this is larger than long range. So It also returns negative value.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic