This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Math.abs Q

 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why abs of Long.MIN_VALUE gives negative result?
Thank you in advance!

 
PNS Subramanian
Ranch Hand
Posts: 150
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic