Double.parseDouble with large strings fails

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      Double.parseDouble with large strings (about half a billion characters) can fail to parse correctly.
      In one case, it parses the value as 0 instead of infinity,
      and in another case it throws a ArrayIndexOutOfBoundsException.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the attached test case.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Infinity
      Infinity
      Infinity
      ACTUAL -
      Infinity
      0.0
      ArrayIndexOutOfBoundsException

      ---------- BEGIN SOURCE ----------
      public class Test {
          public static void main(String[] args) {
              String s = "9999999999e10000";
              System.out.println("parseDouble(" + s + "): " + Double.parseDouble(s) + " (expected: Infinity)");

              s = "9".repeat(Integer.MAX_VALUE - 100) + "e10000";
              System.out.println("parseDouble(999<...>99e10000): " + Double.parseDouble(s) + " (expected: Infinity)");

              int x = 1 + Integer.MAX_VALUE / 2;
              s = "9".repeat(x) + "e" + x;
              try {
                  double d = Double.parseDouble(s);
                  System.out.println(d);
              } catch (ArrayIndexOutOfBoundsException e) {
                  System.out.println("parseDouble(999<...>99e<...>): " + e);
                  e.printStackTrace(System.out);
              }
          }
      }
      ---------- END SOURCE ----------

            Assignee:
            Raffaello Giulietti
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: