Thilak
Thilak

Reputation: 75

Why is double to int conversion not working as expected in java?

I've started reading through the documentation of the Java8 and tried different sample codes. Found below strange behavior.

Sample1

Double di = new Double(Math.pow(2,32-1));
System.out.printf("%f\n",di.doubleValue()); //2147483648.000000
int a= di.intValue();
System.out.println(a); //2147483647

Sample2

Double di = new Double(Math.pow(2,32-1)) - 1.0;
System.out.printf("%f\n",di.doubleValue()); //2147483647.000000
int a= di.intValue();
System.out.println(a); //2147483647

How come in both the cases, the int value is returning same value?

Upvotes: 1

Views: 191

Answers (3)

Michał Krzywański
Michał Krzywański

Reputation: 16910

This is because maximum value of int type in Java is 2147483647. When you invoke Double::doubleValue it will perform narrowing conversion if the value that you are trying to convert is out of bound and it is stated by this method's docs :

Returns the value of this Double as an int after a narrowing primitive conversion.

And it even points to JLS 5.1.3 where this narrowing conversion is described.

For converting floating point values it takes two steps. The reason why you see this value is explained in the sentence for first step, dot three, option b :

The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type int or long.

A narrowing conversion of a floating-point number to an integral type T takes two steps:

  1. In the first step, the floating-point number is converted either to a long, if T is long, or to an int, if T is byte, short, char, or int, as follows:

    • If the floating-point number is NaN (§4.2.3), the result of the first step of the conversion is an int or long 0.

    • Otherwise, if the floating-point number is not an infinity, the floating-point value is rounded to an integer value V, rounding toward zero using IEEE 754 round-toward-zero mode (§4.2.3). Then there are two cases:

    • a If T is long, and this integer value can be represented as a long, then the result of the first step is the long value V.

    • b Otherwise, if this integer value can be represented as an int, then the result of the first step is the int value V.

    • Otherwise, one of the following two cases must be true:

    • a The value must be too small (a negative value of large magnitude or negative infinity), and the result of the first step is the smallest representable value of type int or long.

    • b The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type int or long.

  2. In the second step:

    • If T is int or long, the result of the conversion is the result of the first step.

    • If T is byte, char, or short, the result of the conversion is the result of a narrowing conversion to type T (§5.1.3) of the result of the first step.

So the result will be the largest representable value of type int, in this case.

Upvotes: 0

Josef Veselý
Josef Veselý

Reputation: 112

Please see https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3 THe important part highlighted in bold at the end:

A narrowing conversion of a floating-point number to an integral type T takes two steps:

In the first step, the floating-point number is converted either to a long, if T is long, or to an int, if T is byte, short, char, or int, as follows:

If the floating-point number is NaN (§4.2.3), the result of the first step of the conversion is an int or long 0.

Otherwise, if the floating-point number is not an infinity, the floating-point value is rounded to an integer value V, rounding toward zero using IEEE 754 round-toward-zero mode (§4.2.3). Then there are two cases:

If T is long, and this integer value can be represented as a long, then the result of the first step is the long value V.

Otherwise, if this integer value can be represented as an int, then the result of the first step is the int value V.

Otherwise, one of the following two cases must be true:

The value must be too small (a negative value of large magnitude or negative infinity), and the result of the first step is the smallest representable value of type int or long.

The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type int or long.

Saying that, your double value is 2147483648 (you can try with higher number). Highest representable value int int is 2147483647. That's why you end up with 2147483647.

Upvotes: 1

lotor
lotor

Reputation: 1090

int value cannot exceed Integer.MAX_VALUE, which is exactly 2147483647. You voluntary abandon all the tails when call intValue().

Upvotes: 0

Related Questions