[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Ryu algorithm (was Re: Formatting numbers without precision loss)
- From: Andrew Gierth <andrew@...>
- Date: Thu, 22 Jun 2023 03:04:14 +0100
If anyone's interested, here is a translation of the Ryu algorithm into
Lua 5.4. (It needs 64-bit bitwise operations, so it won't work on luajit.)
No warranty, yadda yadda. License is the Boost one.
This isn't necessarily useful as it stands, since the algorithm is
really intended for C and similar languages, but it does work, and it
may be of interest for people interested in studying how it works
(though you'll need to read and understand the paper as well).
This version is actually mostly taken from the adaptation I made for use
in PostgreSQL some years back, though that was almost a direct import of
Ulf Adams' code (and here I left out the fixed-point format output and
integer fast-path that I added to the PostgreSQL version). I included
test cases mostly taken from the PostgreSQL regression tests.
https://gist.github.com/RhodiumToad/87cde334e8dc4bb0366c649ecd227698
This version does assume round-to-nearest-even works on input (which I
disabled on the PostgreSQL version because some platform strtod()
functions don't get that right). The test cases should flag up errors if
your Lua tonumber() isn't respecting this. Changing the "acceptBounds"
flag can work around this (see comment).
If you want the C code, the original is in github.com/ulfjack/ryu.
--
Andrew.