This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Math.abs() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Math.abs()" Watch "Math.abs()" New topic
Author

Math.abs()

Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 34
This is a question from Vodoo exam,
class TestVodoo2 {
public static void main(String[] arg)
{
System.out.println("Abs()="+Math.abs(-17)+Math.abs(Integer.MIN_VALUE));
}
}
When we pass -17 to Abs() it returns 17
But when we pass MIN_VALUE we still get a negative value (-2147483648)
Why is it so?
Thanks.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Let me answer a question with a question...
What is the range of an int?
Answer that, and I'll bet you be able to understand this question. If you're still confused, let me know.
Corey


SCJP Tipline, etc.
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 34
Corey,
Thanks
The range is -2147483648 to 2147483647
But no, still far away from answer.
This time i tried the following:
System.out.println(Math.abs(Byte.MIN_VALUE));
answer:128
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
So, what do you think the result should be if you pass Integer.MIN_VALUE to the abs method? Is that value within the range on an int?
Siddhartha Bhattacharya
Ranch Hand

Joined: Jul 02, 2002
Posts: 36
See API documentation
<quote>
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.
</quote>
its can't be +2147483648 for sure since thats not in the range of an int( which is the return type of the function).
Siddhartha Bhattacharya
Ranch Hand

Joined: Jul 02, 2002
Posts: 36
System.out.println(Math.abs(Byte.MIN_VALUE));
Byte promoted to int.
so the result is abs(-128) which is 128.
still within the range of int.
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 34
Thanks again Corey & Siddharta.
I got it.
srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
can anybody pls provide me with the URL for Vodoo exam,vodoo page. Is it free ??


please use the [code][/code] tags when showing code. visit <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page</a> ,for more details
zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104
this must be the link for the Voodoo Exam
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Just to sum up the entire conversation, the problem is that, when using two's complement to store values, the range for each data type goes from -n to n-1. Therefore, there is one more negative number than there is a positive number. You can account for this when you realize that 0 is considered a positive number in two's complement (the sign bit is positive).
Therefore, if you try to take the absolute value of the most negative value, the answer will be a number which is outside of the range of the data type. This causes overflow.
In this case, we take Integer.MIN_VALUE, which is this:

And we try to take the absolute value of it. That means we need to flip all the bits and add 1. So, we first flip all the bits and we get this:

Now, we add 1, and we get:


Notice, we ended up with the same value we started with.
I hope that helps clear up any remaining confusion.
Corey
Val Lee
Ranch Hand

Joined: Nov 27, 2001
Posts: 41

the output shows Integer.MIN_VALUE and Long.MIN_VALUE is less than zero.
But Float.MIN_VALUE and Double.MIN_VALUE is more than zero,why?


Val SCJP2 1.2<br />====>SCJD
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Why they designed it this way, I'm not exactly sure, but as the API Spec for Float.MIN_VALUE states:

A constant holding the smallest positive nonzero value of type double. It is equal to the value returned by Double.longBitsToDouble(0x1L).

So, based on the API, that's exactly what Float.MIN_VALUE should return. In fact, due to the way that floats are stored, the max value and the minimum value are the same number (opposite signs, of course). For example, the range of a float is -1.7976931348623157E308 to 1.7976931348623157E308. Therefore, having two values that are really the same thing is somewhat pointless. Rather, it would be more useful to know how close and far away from zero a float can get.
That's my best guess as to why this was designed in this fashion.
I hope that helps,
Corey
Panagiotis Varlagas
Ranch Hand

Joined: Nov 27, 2000
Posts: 233
According to chapter 4.2.3. of the JLS, floating-point types in Java are "conceptually associated" with the values and operation specified in the IEEE 754 standard.
This standard dictates that floating point numbers are signed magnitude. So, for a given mantissa and the exponent, by toggling the sign bit between 0 and 1 you get two "sibling" floating-points numbers, one of them positive and the other one negative, the one being the exact opposite of the other. In this signed-magnitude world, we have (among other things) a positive and negative zero (+0, -0) with one being the opposite of the other.
In integer arithmetic things are not quite the same. Integers are represented in Java in a 2's-complement format. There are 2^32 integers, namely [-2^16, 2^16 - 1].
What we intuitely consider to be the opposite of
-2^16 is outside the integer range, so we sort-of "decide" (led by 2's-complement arithmetic) that -2^16's opposite is -2^16 itself. There is one other integer that has itself as its opposite and it is 0.
Hope this helps,
Panagiotis.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Math.abs()
 
Similar Threads
dear all, i created a set of mock exam, please try it.
abs() method of Math class
float or double
Integer.Min_value
few basic questions