为什么将除以零转换为整数基元会给出不同的结果?
Why casting division by zero to integer primitives gives different results?
System.out.println((byte) (1.0/0));
System.out.println((short) (1.0/0));
System.out.println((int) (1.0/0));
System.out.println((long) (1.0/0));
结果是:
-1
-1
2147483647
9223372036854775807
二进制格式:
1111 1111
1111 1111 1111 1111
0111 1111 1111 1111 1111 1111 1111 1111
0111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111
为什么将无穷大转换为 int 和长整数保持符号位为“0”,而将字节和短整数的符号位设置为“1”?
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.
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.
所以无限双精度值首先通过返回 Integer.MAX_VALUE
转换为 int
,然后 然后 进一步转换为 byte
/ short
,它采用适当数量的低字节(结果为 -1)。强制转换为 int
和 long
没有额外的步骤,但是 byte
和 short
从 first 到 int
和 然后 到 byte
/short
.
System.out.println((byte) (1.0/0));
System.out.println((short) (1.0/0));
System.out.println((int) (1.0/0));
System.out.println((long) (1.0/0));
结果是:
-1
-1
2147483647
9223372036854775807
二进制格式:
1111 1111
1111 1111 1111 1111
0111 1111 1111 1111 1111 1111 1111 1111
0111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111
为什么将无穷大转换为 int 和长整数保持符号位为“0”,而将字节和短整数的符号位设置为“1”?
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.
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.
所以无限双精度值首先通过返回 Integer.MAX_VALUE
转换为 int
,然后 然后 进一步转换为 byte
/ short
,它采用适当数量的低字节(结果为 -1)。强制转换为 int
和 long
没有额外的步骤,但是 byte
和 short
从 first 到 int
和 然后 到 byte
/short
.