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

Float.valueOf(String) value not always exact.

    XMLWordPrintable

Details

    • 1.2beta4
    • generic
    • solaris_2.5.1
    • Not verified

    Description

      Run the following program. It shows that for the really big long value
      given, conversion to float using Float.valueOf() differs from the value
      of converting the same string to a Long, then casting to a float.
      I believe that the latter process gives the correct answer, where the
      former is wrong in the least significant bit.

      class doubleRoundBug {
          /*
           * try to demonstrate input error caused
           * by double rounding.
           */
          static String instring = "18014399583223809";

          public static void main( String ignore[] ){
      long lval = Long.valueOf( instring).longValue();
      float f1 = Float.valueOf( instring).floatValue();
      float f2 = (float)lval;
      if ( f1 == f2 ){
      System.out.println("OK");
      } else {
      System.out.println("String "+instring+" =>"+f1 );
      System.out.println("Long "+lval+" =>"+f2 );
      System.out.println(f1+" != "+f2);
      }
          }
      }

      Output is:

      String 18014399583223809 =>1.80143985E16
      Long 18014399583223809 =>1.80144007E16
      1.80143985E16 != 1.80144007E16

      richard.tuck@Eng 1997-08-11

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: