Skip to content

stdlib needs ryu for compact and performant float to string #13365

@timotheecour

Description

@timotheecour

as of #13364 json works with roundtrip correctness in mind, but sacrifices shortness of representation.
python's json doesnt' have this issue.

Example

import std/json
echo(%* 0.6) # 0.59999999999999998

Current Output

0.59999999999999998

Expected Output

0.6

Possible Solution

port/wrap/implement ryu https://github.com/ulfjack/ryu which has several benefits:

  • speed
  • compactness

Ryu generates the shortest decimal representation of a floating point number that maintains round-trip safety. That is, a correct parser can recover the exact original number. For example, consider the binary 32-bit floating point number 00111110100110011001100110011010. The stored value is exactly 0.300000011920928955078125. However, this floating point number is also the closest number to the decimal number 0.3, so that is what Ryu outputs.

example: python's print wrt float:

as a nice-to-have, we can also have echo behave same as in python:

  a = 0.1
  import numpy
  for i in range(0,10):
    print(a)
    a = numpy.nextafter(a, float('Inf'))
0.1
0.10000000000000002
0.10000000000000003
0.10000000000000005
0.10000000000000006
0.10000000000000007
0.10000000000000009
0.1000000000000001
0.10000000000000012
0.10000000000000013

this isn't the case currently in nim

  var a = 1.0
  for i in 0..<10:
    echo a
    a = nextafter(a, Inf)
1.0
1.0
1.0
1.000000000000001
1.000000000000001
1.000000000000001
1.000000000000001
1.000000000000002
1.000000000000002
1.000000000000002

links

alternative: the newer dragonbox

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions