-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
1.0.2
-
sparc
-
generic
Name: ###@###.### Date: 09/05/96
float and double NaN are converted to minimum integral values
instead of zero as prescribed in the Java Language Specification:
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 (section 4.2.3), the
result of the first step of the conversion is an int or
long 0.
...
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 (section
5.1.3) of the result of the first step.
(Java Language Specification, section 5.1.3)
Tests: execution of the following test
public class test
{
public static void main(String argv[])
{
float zero = 0.0f;
float f = zero / zero;
double d = f;
System.out.println(f);
System.out.println((byte) f);
System.out.println((short) f);
System.out.println((char) f);
System.out.println((int) f);
System.out.println((long) f);
System.out.println(d);
System.out.println((byte) d);
System.out.println((short) d);
System.out.println((char) d);
System.out.println((int) d);
System.out.println((long) d);
}
}
produces such output:
NaN
-1
-1
2147483647
9223372036854775807
NaN
-1
-1
2147483647
9223372036854775807
instead of expected:
NaN
0
0
0
0
NaN
0
0
0
0
======================================================================
- duplicates
-
JDK-1266773 wrong narrowing primitive conversion
- Closed
- relates to
-
JDK-4007658 wrong conversion of floating-point to integral types in Windows
- Closed
-
JDK-1229191 (math) Converting floating-point numbers to ints doesn't always work properly
- Closed
-
JDK-1266803 wrong narrowing primitive conversion
- Closed