为什么 double 到 int 的转换在 java 中没有按预期工作?
Why is double to int conversion not working as expected in java?
我已经开始通读 Java8 的文档并尝试了不同的示例代码。在下面发现了奇怪的行为。
示例 1
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
示例 2
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
为什么在这两种情况下,int 值返回相同的值?
int值不能超过Integer.MAX_VALUE,也就是2147483647。
您在调用 intValue().
时自愿放弃所有尾巴
请参阅https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3最后以粗体突出显示的重要部分:
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.
也就是说,您的 double 值为 2147483648(您可以尝试使用更大的数字)。最高可表示值 int int 是 2147483647。这就是为什么你最终得到 2147483647.
这是因为 Java 中 int
类型的最大值是 2147483647
。当您调用 Double::doubleValue
时,如果您尝试转换的值超出范围并且由此方法的 docs 声明:
,它将执行缩小转换
Returns the value of this Double as an int after a narrowing primitive conversion.
它甚至指向 JLS 5.1.3,其中描述了这种缩小转换。
转换浮点值需要两个步骤。您看到此值的原因在第一步、第三点、选项 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:
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.
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.
所以在这种情况下,结果将是类型 int
的最大可表示值。
我已经开始通读 Java8 的文档并尝试了不同的示例代码。在下面发现了奇怪的行为。
示例 1
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
示例 2
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
为什么在这两种情况下,int 值返回相同的值?
int值不能超过Integer.MAX_VALUE,也就是2147483647。 您在调用 intValue().
时自愿放弃所有尾巴请参阅https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3最后以粗体突出显示的重要部分:
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.
也就是说,您的 double 值为 2147483648(您可以尝试使用更大的数字)。最高可表示值 int int 是 2147483647。这就是为什么你最终得到 2147483647.
这是因为 Java 中 int
类型的最大值是 2147483647
。当您调用 Double::doubleValue
时,如果您尝试转换的值超出范围并且由此方法的 docs 声明:
Returns the value of this Double as an int after a narrowing primitive conversion.
它甚至指向 JLS 5.1.3,其中描述了这种缩小转换。
转换浮点值需要两个步骤。您看到此值的原因在第一步、第三点、选项 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:
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.
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.
所以在这种情况下,结果将是类型 int
的最大可表示值。