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

Floats near zero and infinity do not conform to the Java Language Specification

XMLWordPrintable

    • generic
    • generic



      Name: mc57594 Date: 12/02/99


      /*
        I found this bug on linux:
        java version "1.2"
        Classic VM (build Linux_JDK_1.2_pre_release-v2, native threads, sunwjit)

        but verified the bug on windows 95:
        java version "1.2.2"
        Classic VM (build JDK-1.2.2_W, native threads, symcjit)

        The java language specification states min and max values of
        floating point numbers. This program converts strings
        representing floats that are close to the max and min into Float
        and Double objects. The conversion allows floats that are, by
        the java language spec, too big, or too close to zero. My java
        compilers also accept some of these numbers as literals, which
        they shouldn't.

      */
      public class floatTest {
          public static void main(String[] args) {
              String[] GoodFloats = {
                "3.40282347e+38f", // max float < inf
                "1.40239846e-45f"}; // min positive float > 0

              String[] InfFloats = {
                "3.40282348e+38f",
                "3.40282357e+38f",
                "3.40282447e+38f",
                "3.40282347e+38f",
                "3.40292347e+38f",
                "3.40382347e+38f",
                "3.41282347e+38f",
                "3.50282347e+38f",
                "4.40282347e+38f",
                "3.40282347e+39f"};

              String[] ZeroFloats = {
                "1.40239845e-45f",
                "1.40239836e-45f",
                "1.40239746e-45f",
                "1.40238846e-45f",
                "1.40229846e-45f",
                "1.40139846e-45f",
                "1.39239846e-45f",
                "1.30239846e-45f",
                "0.40239846e-45f",
                "1.40239846e-46f"};

              String[] GoodDoubles = {
                "1.79769313486231570e+308", // max double < inf
                "4.94065645841246544e-324"}; // min positive double > 0

              String[] InfDoubles = {
                "1.79769313486231571e+308",
                "1.79769313486231580e+308",
                "1.79769313486231670e+308",
                "1.79769313486232570e+308",
                "1.79769313486241570e+308",
                "1.79769313486331570e+308",
                "1.79769313487231570e+308",
                "1.79769313496231570e+308",
                "1.79769313586231570e+308",
                "1.79769314486231570e+308",
                "1.79769323486231570e+308",
                "1.79769413486231570e+308",
                "1.79770313486231570e+308",
                "1.79779313486231570e+308",
                "1.79869313486231570e+308",
                "1.80769313486231570e+308",
                "1.89769313486231570e+308",
                "2.79769313486231570e+308",
                "1.79769313486231570e+309"};

              String[] ZeroDoubles = {
                "4.94065645841246543e-324",
                "4.94065645841246534e-324",
                "4.94065645841246444e-324",
                "4.94065645841245544e-324",
                "4.94065645841236544e-324",
                "4.94065645841146544e-324",
                "4.94065645840246544e-324",
                "4.94065645831246544e-324",
                "4.94065645741246544e-324",
                "4.94065644841246544e-324",
                "4.94065635841246544e-324",
                "4.94065545841246544e-324",
                "4.94064645841246544e-324",
                "4.94055645841246544e-324",
                "4.93965645841246544e-324",
                "4.93065645841246544e-324",
                "4.84065645841246544e-324",
                "3.94065645841246544e-324",
                "4.94065645841246544e-325"};
              
              try {
                  Float f;
                  Double d;
                  for (int i = 0; i < GoodFloats.length; i++) {
                      f = Float.valueOf(GoodFloats[i]);
                      if (f.isInfinite() || f.compareTo(new Float(0f)) == 0) {
                          System.out.println(GoodFloats[i] + " should be a valid float.");
                      }
                  }
                  for (int i = 0; i < InfFloats.length; i++) {
                      f = Float.valueOf(InfFloats[i]);
                      if (!f.isInfinite()) {
                          System.out.println(InfFloats[i] + " should represent infinity.");
                      }
                  }
                  for (int i = 0; i < ZeroFloats.length; i++) {
                      f = Float.valueOf(ZeroFloats[i]);
                      if (f.compareTo(new Float(0f)) != 0) {
                          System.out.println(ZeroFloats[i] + " should represent zero.");
                      }
                  }
                  for (int i = 0; i < GoodDoubles.length; i++) {
                      d = Double.valueOf(GoodDoubles[i]);
                      if (d.isInfinite() || d.compareTo(new Double(0d)) == 0) {
                          System.out.println(GoodDoubles[i] + " should be a valid double.");
                      }
                  }
                  for (int i = 0; i < InfDoubles.length; i++) {
                      d = Double.valueOf(InfDoubles[i]);
                      if (!d.isInfinite()) {
                          System.out.println(InfDoubles[i] + " should represent infinity.");
                      }
                  }
                  for (int i = 0; i < ZeroDoubles.length; i++) {
                      d = Double.valueOf(ZeroDoubles[i]);
                      if (d.compareTo(new Double(0d)) != 0) {
                          System.out.println(ZeroDoubles[i] + " should represent zero.");
                      }
                  }
              } catch(NumberFormatException e) {
                  System.out.println(e.getMessage());
              }
          }
      }
      (Review ID: 97786)
      ======================================================================

            darcy Joe Darcy
            mchamnessunw Mark Chamness (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: