# Math.abs()

Bindesh Vijayan

Ranch Hand

Posts: 34

posted 13 years ago

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.

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

Posts: 3271

Bindesh Vijayan

Ranch Hand

Posts: 34

Corey McGlone

Ranch Hand

Posts: 3271

Siddhartha Bhattacharya

Ranch Hand

Posts: 36

posted 13 years ago

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).

<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

Posts: 36

srinivas bolloju

Ranch Hand

Posts: 112

posted 13 years ago

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

Posts: 104

Corey McGlone

Ranch Hand

Posts: 3271

posted 13 years ago

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

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

Posts: 41

Corey McGlone

Ranch Hand

Posts: 3271

posted 13 years ago

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

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

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

Posts: 233

posted 13 years ago

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

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.

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.