Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Makes Num stringification 2x faster (tested with rand.Str) - Fixes RT#127184 https://rt.perl.org/Ticket/Display.html?id=127184 - Fixes RT#132330 https://rt.perl.org/Ticket/Display.html?id=132330 (fixes Num.WHICH and problems with set()s mentioned in that ticket) Grisu3[^1] is a 2010 algorithm that stringifies doubles to shortest possible representation that would still result in the same value if it's parsed and stringified again. This sort of strigification is what fixes the reported bugs. As a bonus, the algo is faster than snprintf('%.15g') we used to use for stringification. Grisu3 handles ~99.5% of cases. In the remaining 0.5%, it knows it won't produce the shortest possible result and so a fallback to slower algorithm is made, which in our current case is snprintf('%.17g'), which is a suboptimal fallback, and in the future Dragon4 algo[^2] could be used instead. The change from .15g to .17g in the fallback is intentional, as 15 is the number of guaranteed significant digits, but 17 is the number of maximum available significant digits when the IEEE doubles differ by at most one unit[^7]. Based on my research, an improved fallback would be Dragon4 algo[^2], and Grisu3+Dragon4 is the current state of the art. (some references may state that Errol[^8] algo superseded Grisu3+Dragon4 combo, but based on authors' corrections[^9], it appears there was a benchmarking error and Errol is not in fact faster). The Grisu3+Dragon4 is used[^5] by Rust (see also some trial impls in [^6]) and Grisu3's author's C++ version of the code[^3] indicates it's used by V8 JavaScript engine as well. There exist a C# impl[^4] of the algo as well, used in an efficient JSON encoder. [1] "Printing Floating-Point Numbers Quickly and Accurately with Integers" by Florian Loitsch https://goo.gl/cbvogg [2] "How to Print Floating-Point Numbers Accurately" by Steel & White: https://lists.nongnu.org/archive/html/gcl-devel/2012-10/pdfkieTlklRzN.pdf [3] https://github.com/google/double-conversion [4] https://github.com/kring/grisu.net [5] rust-lang/rust#24612 [6] https://github.com/lifthrasiir/rust-strconv [7] http://www2.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1822.pdf [8] https://github.com/marcandrysco/Errol [9] https://github.com/marcandrysco/Errol#performance-evaluation-correction
- Loading branch information