Java lang specification says (5.2.2 p.55):
"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."
But the program below prints anything but not 0:
> cat conv023.java
// Ident: @(#)conv023.java 1.1 96/02/09
// Copyright 09 Feb 1996 Sun Microsystems, Inc. All Rights Reserved
public class conv023 {
public static void main(String args[]) {
double zerod = 0.0d;
double d;
d = zerod / zerod;
if (Double.isNaN(d)) {
System.out.print("(byte) Double.NaN = ");
System.out.println((byte) d);
System.out.print("(short) Double.NaN = ");
System.out.println((short) d);
System.out.print("(int) Double.NaN = ");
System.out.println((int) d);
System.out.print("(long) Double.NaN = ");
System.out.println((long) d);
System.out.print('\\n');
}
}
}
> /usr/local/java/1.0/bin/javac conv023.java
> /usr/local/java/1.0/bin/java conv023
(byte) Double.NaN = -1
(short) Double.NaN = -1
(int) Double.NaN = 2147483647
(long) Double.NaN = 9223372036854775807
"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."
But the program below prints anything but not 0:
> cat conv023.java
// Ident: @(#)conv023.java 1.1 96/02/09
// Copyright 09 Feb 1996 Sun Microsystems, Inc. All Rights Reserved
public class conv023 {
public static void main(String args[]) {
double zerod = 0.0d;
double d;
d = zerod / zerod;
if (Double.isNaN(d)) {
System.out.print("(byte) Double.NaN = ");
System.out.println((byte) d);
System.out.print("(short) Double.NaN = ");
System.out.println((short) d);
System.out.print("(int) Double.NaN = ");
System.out.println((int) d);
System.out.print("(long) Double.NaN = ");
System.out.println((long) d);
System.out.print('\\n');
}
}
}
> /usr/local/java/1.0/bin/javac conv023.java
> /usr/local/java/1.0/bin/java conv023
(byte) Double.NaN = -1
(short) Double.NaN = -1
(int) Double.NaN = 2147483647
(long) Double.NaN = 9223372036854775807
- duplicates
-
JDK-4028664 wrong narrowing primitive conversion of NaNs
- Closed
-
JDK-1266773 wrong narrowing primitive conversion
- Closed
-
JDK-1229191 (math) Converting floating-point numbers to ints doesn't always work properly
- Closed