-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
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.59999999999999998Current 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
- https://forum.nim-lang.org/t/5983 Nim's float issue?
- https://github.com/ulfjack/ryu : Share a paper: How to Print Floating-Point Numbers Accurately - Nim forum
- ulfjack/ryu: Converts floating point numbers to decimal strings : ulfjack/ryu: Converts floating point numbers to decimal strings
- disruptek/ryu: ryu for nim : disruptek/ryu: ryu for nim (archived)
- Clyybber/nimryu: nim port of ryu : Clyybber/nimryu: nim port of ryu
- LemonBoy/dtoa.nim: Port of Milo Yip's fast dtoa() implementation : LemonBoy/dtoa.nim: Port of Milo Yip's fast dtoa() implementation
- Is there a reliable way to round? - Nim forum
- Fast and accurate float to string conversion (ryu) · Issue #104 · nim-lang/needed-libraries
- ⚙ D70631 Microsoft's floating-point to_chars powered by Ryu and Ryu Printf
- alaviss/nim-ryu: An implementation of the ryū float-to-string conversion algorithm. This version is written from scratch based on the paper.
- mir-algorithm/generic_128.d at master · libmir/mir-algorithm
- D Ryu implementation reference by 9il · Pull Request #194 · ulfjack/ryu