Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Math.abs Q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Math.abs Q" Watch "Math.abs Q" New topic
Author

Math.abs Q

Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
Why abs of Long.MIN_VALUE gives negative result?
Thank you in advance!



"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
PNS Subramanian
Ranch Hand

Joined: Jul 13, 2004
Posts: 150
Incidentally
Math.abs(Long.MIN_VALUE)+1 gives a negative value and
Math.abs(Long.MIN_VALUE)-1 gives a positive value.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
In 2's complement arithmetic, the smallest negative number of a given size is one larger in magnitude than the largest positive number of the same size. For example, a long runs from -9223372036854775808 to 9223372036854775807. If you take the absolute value of Long.MIN_VALUE, there is no correct answer representable as a Long.


From the Java 5 API for Math.abs():
Note that if the argument is equal to the value of Long.MIN_VALUE, the most negative representable long value, the result is that same value, which is negative.

[ December 04, 2004: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Math.abs Q
 
Similar Threads
Math.abs()
Jiris, Mock 1, Q17
Math.abs()
MATH Problem - Shor.MIN_VALUE
Where am I wrong??