Uploaded image for project: 'Erlang/OTP'
  1. Erlang/OTP
  2. ERL-1469

Add `shortest` to float_to_list/2 and float_to_binary/2 options, using Ryu

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: kernel
    • Labels:
      None

      Description

      Right now, there is 2 different ways to get a float into a string in OTP. All the others boil down to these two.

      • float_to_list/2 and float_to_binary/2
      • io_lib_format:fwrite_g/1

      The first is a BIF that call to various libc sprintf and in certain case an optimised case written directly in kernel and support the classic %e and %f. The second is a pure erlang implementation of a shortest round_trip printing procedure (with a few added rules).

      There has been interesting work in the past few years to get an order of magnitude faster algorithms for shortest round trip float to string conversion in C in relatively simple ways. What we offer here is to use the Ryu https://github.com/ulfjack/ryu algorithm to add a `shortest` option to the BIFs. And then `io_lib_format:fwrite_g/1` can delegate to the BIFs. We hope that this would yield a faster algorithm and make it easier to follow what is happening upstream.

      On top of that, while `io_lib_format:fwrite_g/1` is technically a private and undocumented function, but is is used in the wild, in particular in the json library Jason. There is ground to make it public in a BIF, to allow better support.

      We offer to use Microsoft STL test suite for charconv, which is implemented with Ryu, to test this new implementation https://github.com/microsoft/STL/tree/master/tests/std/tests/P0067R5_charconv

      Left for latter is the possibility to make this `shortest` the default option in `float_to_list/1` which right now use

      {scientific,20}

      as the default.

      This would stay in line with being compatible with `list_to_float` and would not lose any capability as both the BIFs and `io_lib_format:fwrite_g/1` ignore locales already.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              otp_team_vm Team VM
              Reporter:
              dianaolympos DianaOlympos
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated: