Harmony
  1. Harmony
  2. HARMONY-329

[classlib][luni] Endless loop when using Double.parseDouble(String) with certain types of value

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 5.0M11
    • Component/s: Classlib
    • Labels:
      None
    • Estimated Complexity:
      Advanced

      Description

      Calling Double.parseDouble(..) with certain Strings (e.g. Double.parseDouble("-1.233999999999999965116738099630936817275852021384209929081813042837802886790127428328465579708849276001782791006814286802871737087810957327493372866733334925806221045495205250590286471187577636646208155890426896101636282423463443661040209738873506655844025580428394216030152374941053494694642722606658935546875E-112") causes an endless loop.

      Although this is not necessarily a case you would see in regular programming it happens when you do something like new BigDecimal(-1.234E-112).doubleValue(), which was the cause of the endless loop mentioned in HARMONY-319.

      The loop occurs in the native method "doubleAlgorithm (JNIEnv * env, U_64 * f, IDATA length, jint e, jdouble z)" in native-src/shared/math/dblparse.c.

        Activity

        Sian January created issue -
        Hide
        Alexey Petrenko added a comment -

        This issue is still reproducible on latest classlib.

        Show
        Alexey Petrenko added a comment - This issue is still reproducible on latest classlib.
        Hide
        Leo Li added a comment -

        It is a fault of the algorithm in the math lab used by harmony.
        In the file trunk/modules/luni/src/main/native/luni/shared/dblparse.c
        there is a comment of function doubleAlgorithm:
        /* The algorithm for the function doubleAlgorithm() below can be found

        • in:
          *
        • "How to Read Floating-Point Numbers Accurately", William D.
        • Clinger, Proceedings of the ACM SIGPLAN '90 Conference on
        • Programming Language Design and Implementation, June 20-22,
        • 1990, pp. 92-101.
          *
        • There is a possibility that the function will end up in an endless
        • loop if the given approximating floating-point number (a very small
        • floating-point whose value is very close to zero) straddles between
        • two approximating integer values. We modified the algorithm slightly
        • to detect the case where it oscillates back and forth between
        • incrementing and decrementing the floating-point approximation. It
        • is currently set such that if the oscillation occurs more than twice
        • then return the original approximation.
          */

        It seems that it will fail in endless loop in the testcase.
        Any suggestion?

        Show
        Leo Li added a comment - It is a fault of the algorithm in the math lab used by harmony. In the file trunk/modules/luni/src/main/native/luni/shared/dblparse.c there is a comment of function doubleAlgorithm: /* The algorithm for the function doubleAlgorithm() below can be found in: * "How to Read Floating-Point Numbers Accurately", William D. Clinger, Proceedings of the ACM SIGPLAN '90 Conference on Programming Language Design and Implementation, June 20-22, 1990, pp. 92-101. * There is a possibility that the function will end up in an endless loop if the given approximating floating-point number (a very small floating-point whose value is very close to zero) straddles between two approximating integer values. We modified the algorithm slightly to detect the case where it oscillates back and forth between incrementing and decrementing the floating-point approximation. It is currently set such that if the oscillation occurs more than twice then return the original approximation. */ It seems that it will fail in endless loop in the testcase. Any suggestion?
        Mikhail Loenko made changes -
        Field Original Value New Value
        Summary Endless loop when using Double.parseDouble(String) with certain types of value [classlib][luni] Endless loop when using Double.parseDouble(String) with certain types of value
        Tim Ellison made changes -
        Assignee Tim Ellison [ tellison ]
        Hide
        Tim Ellison added a comment -

        As the comment says, it may not complete because the successive approximation is not stable at the finest resolution, but since there is a limited resolution of doubles anyway we can trim it early to ensure better convergence.

        Fix applied at repo revision r793829.

        Please verify it fixes the problem ok for you.

        Show
        Tim Ellison added a comment - As the comment says, it may not complete because the successive approximation is not stable at the finest resolution, but since there is a limited resolution of doubles anyway we can trim it early to ensure better convergence. Fix applied at repo revision r793829. Please verify it fixes the problem ok for you.
        Tim Ellison made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 5.0M11 [ 12314031 ]
        Resolution Fixed [ 1 ]

          People

          • Assignee:
            Tim Ellison
            Reporter:
            Sian January
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development