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

(math) Converting floating-point numbers to ints doesn't always work properly

XMLWordPrintable

    • beta
    • generic, x86, sparc
    • generic, solaris_2.4, solaris_2.5, solaris_2.5.1, windows_nt

      The Java interpreter, for the opcodes opc_f2i, opc_f2l, opc_d2i, opc_d2l, does not seem
      to have special code to check for the bouncary conditions where the floating operand
      is NaN or has a value too large or to small to be representable in the integer format.

      Related bugs 1240810, 1240807
      The SPARC architecture happens to handle some of these cases correctly, but not others.
      Porting the C code as written to other architectures may not work properly.

      Example: converting a NaN (float or double) to int, Java produces 0 (which is what the
      spec says) but produces 9223372036854775807 or -9223372036854775808 when converting a NaN to long (depending on whether the hardware thinks it has a
      "positive NaN" or a "negative NaN" in hand, though Java is supposed to make no
      such distinction).

      Here is some test code I was using (I am gratified to have found no difference in behavior
      among constants computed at compile time and values computed at run time):

      class Fltint {
        public static void main(String[] argv) {
          foo(0.0f/0.0f, 0.0d/0.0d, 1.0e25f, 1.0e25d, 1.0e50d);
        }
        static void foo(float pnan, double pdnan, float p25, double pd25, double pd50) {
          float vnan = 0.0f/0.0f;
          double vdnan = 0.0/0.0;
          float v25 = 1.0e25f;
          double vd25 = 1.0e25d;
          double vd50 = 1.0e50d;
          System.out.println(0.0f/0.0f + ", " + pnan + ", " + vnan);
          System.out.println(0.0d/0.0d + ", " + pdnan + ", " + vdnan);
          System.out.println((int)(0.0f/0.0f) + ", " + (int)pnan + ", " + (int)vnan);
          System.out.println((long)(0.0f/0.0f) + ", " + (long)pnan + ", " + (long)vnan);
          System.out.println((int)(0.0d/0.0d) + ", " + (int)pdnan + ", " + (int)vdnan);
          System.out.println((long)(0.0d/0.0d) + ", " + (long)pdnan + ", " + (long)vdnan);
          System.out.println((int)(-(0.0f/0.0f)) + ", " + (int)(-pnan) + ", " + (int)(-vnan));
          System.out.println((long)(-(0.0f/0.0f)) + ", " + (long)(-pnan) + ", " + (long)(-vnan));
          System.out.println((int)(-(0.0d/0.0d)) + ", " + (int)(-pdnan) + ", " + (int)(-vdnan));
          System.out.println((long)(-(0.0d/0.0d)) + ", " + (long)(-pdnan) + ", " + (long)(-vdnan));
          System.out.println((int)1.0e25f + ", " + (int)p25 + ", " + (int)v25);
          System.out.println((long)1.0e25f + ", " + (long)p25 + ", " + (long)v25);
          System.out.println((int)-1.0e25f + ", " + (int)-p25 + ", " + (int)-v25);
          System.out.println((long)-1.0e25f + ", " + (long)-p25 + ", " + (long)-v25);
          System.out.println((int)1.0e25d + ", " + (int)pd25 + ", " + (int)vd25);
          System.out.println((long)1.0e25d + ", " + (long)pd25 + ", " + (long)vd25);
          System.out.println((int)-1.0e25d + ", " + (int)-pd25 + ", " + (int)-vd25);
          System.out.println((long)-1.0e25d + ", " + (long)-pd25 + ", " + (long)-vd25);
          System.out.println((int)1.0e50d + ", " + (int)pd50 + ", " + (int)vd50);
          System.out.println((long)1.0e50d + ", " + (long)pd50 + ", " + (long)vd50);
          System.out.println((int)-1.0e50d + ", " + (int)-pd50 + ", " + (int)-vd50);
          System.out.println((long)-1.0e50d + ", " + (long)-pd50 + ", " + (long)-vd50);
        }
      }

      Dimtry.Khukhro@Eng 1998-10-14

      JDK1.2fcsN with jit on Ultra-1 still fails JCK tests
       vm/instr/d2i/d2i001/d2i00101/d2i00101.html
       vm/instr/f2i/f2i001/f2i00101/f2i00101.html

      Note that the tests pass on SparcStation-10, that is,
      JVM implementation still "relies on hardware idiosyncracies".

      The above test Fltint also fails.

            hongzh Hong Zhang
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: