Revisit details of Float16 to decimal conversion algorithm

XMLWordPrintable

    • Type: CSR
    • Resolution: Approved
    • Priority: P4
    • 27
    • Component/s: core-libs
    • None
    • behavioral
    • minimal
    • Hide
      The output for magnitudes greater than or equal to 1000 changes from plain notation to scientific notation.
      Clients that rely on the details of the format of the output string will break.

      However, since this class is still incubating, the risks of impacting production code is considered minimal.
      Show
      The output for magnitudes greater than or equal to 1000 changes from plain notation to scientific notation. Clients that rely on the details of the format of the output string will break. However, since this class is still incubating, the risks of impacting production code is considered minimal.
    • Java API
    • JDK

      Summary

      The thresholds to select plain versus scientific notation in Float16 are currently the same as for double and float, namely 1e-3 and 1e7. Given the narrow range of Flaot16 values, the upper threshold should be lowered to 1e3.

      Problem

      All Float16 values greater than or equal to 1024 are integers, and are currently formatted in plain notation. However, since these integers spread out more and more over the Float16 range, it can happen that converting an integer like 65519 to Float16 and converting this back to a decimal that is formatted as a visually different integer, here 65500. This can cause confusion to users not too familiar with the intricacies of binary-to-decimal conversions.

      Solution

      The solution consists of using 1e3 as the upper threshold, thus forcing scientific notation for decimal values greater than or equal to 1000. Scientific notation is usually associated with imprecision, so "reading" 65519 and outputting it as 6.55E4 is arguably less confusing to most users.

      Specification

      --- a/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/Float16.java
      +++ b/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/Float16.java
      @@ -260,6 +260,9 @@ private Float16(short bits) {
            * Float#toString(float)} in the handling of special values
            * (signed zeros, infinities, and NaN) and the generation of a
            * decimal string that will convert back to the argument value.
      +     * However, the range for plain notation is defined to be the interval
      +     * [10<sup>-3</sup>, 10<sup>3</sup>) rather than the interval used
      +     * for {@code float} and {@code double}.
            *
            * @param   f16   the {@code Float16} to be converted.
            * @return a string representation of the argument.
      

            Assignee:
            Raffaello Giulietti
            Reporter:
            Raffaello Giulietti
            Joe Darcy
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: