Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4028664

wrong narrowing primitive conversion of NaNs

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • P3
    • None
    • 1.1
    • hotspot
    • None
    • sparc
    • solaris_2.4

    Description



      Name: swC45995 Date: 01/29/97

      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

      ======================================================================

      Attachments

        Issue Links

          Activity

            People

              tlindholsunw Timothy Lindholm (Inactive)
              wensunw Wen Wen (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: