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

Problem with importing floating-point numbers

XMLWordPrintable

    • sparc
    • solaris_2.5.1

      Here is a piece of code that demonstrates the
      problem.
      The methods (other than main) are taken
      directly from
      the existing code base.

      Here's what it does. The floating-point
      representation
      for -1 as read from a Wintel file is stored
      in a byte array
      and is then passed to methods that convert it
      to a double.

      Under JDK 1.0.2, the result is correct (we do
      get -1).
      However, under JDK 1.1.1 & 1.1.2, we get the
      result NaN
      (Not A Number). Obviously something has
      changed,
      probably in Double.longBitsToDouble().

      If you have any questions or require
      additional information, please feel free to
      contact me at any time. I can be reached at
      (613) 728-0826 ext.1581 or via e-mail at
      ###@###.###.


      ----
      import java.lang.Double;

      public class DoubleTest
      {
          public static short getToken( byte[] byteArray, int index )
          {
              short value;

              value = (short)((byteArray[index] & 0xFF) + (byteArray[index + 1] << 8));
              return value;
          }

          /*
           * Returns a 4 byte integer.
           */
          public static int getLong( byte[] byteArray, int index )
          {
              return (getToken( byteArray, index ) & 0xFFFF) + (getToken( byteArray, index + 2 ) << 16);
          }

          /*
           * Returns an 8 byte float.
           */
          public static double getDouble( byte[] byteArray, int index )
          {
              long bits = (getLong( byteArray, index ) & 0xFFFFFFFF) + (getLong( byteArray, index + 4 ) << 32L);
              return (double)Double.longBitsToDouble( bits );
          }

          public static void main( String args[] )
          {
              byte[] byteArray = new byte[8]; // contains little endian format double

              byteArray[0] = 0x00;
              byteArray[1] = 0x00;
              byteArray[2] = 0x00;
              byteArray[3] = 0x00;
              byteArray[4] = 0x00;
              byteArray[5] = 0x00;
              byteArray[6] = (byte)0xF0;
              byteArray[7] = (byte)0xBF;

              System.out.println( "double = " + getDouble( byteArray, 0) );
          }
      }

            apalanissunw Anand Palaniswamy (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: