mlibc
mlibc copied to clipboard
printf: floats and doubles do not round correctly
See e.g https://github.com/managarm/frigg/blob/51d873bc18d4ca82220a0d59c48b40cad6b87495/include/frg/formatting.hpp#L310-L311 and https://github.com/managarm/mlibc/blob/bbac41bcb782662646c7ccef88e8ebc08330741e/tests/ansi/sprintf.c#L71-L81
To fix this, we could use a C++ float-to-string implementation like dragonbox.
The author of Dragonbox here.
Dragonbox does a different thing from what printf does and I don't think it can help implementing printf. There is a known algorithm for printf-like behavior called Ryu-printf (which is a different algorithm from Ryu, but by the same author, and whose implementation is included in the same repo for Ryu's reference implementation), but it is relying on a gigantic table of size over 100kb. I have an implementation of Ryu-printf using 39kb, but it is still too huge for mlibc, I guess. I also have an algorithm idea which might require a smaller table, but haven't worked it out.
But I believe there are printf-like float-to-string conversion implementations out there which don't require ridiculously big tables, while slower than Ryu-printf. A typical such an implementation probably relies on some form of big integer calculation, so probably with some usage of heap memory. Not sure if that's something mlibc cares about, though.
Hi there, thanks for the reply.
A 40k table isn't too bad, especially in a DSO. Do you have the code for that available somewhere?
It's here. I'm pretty confident about the correctness of the implementation, but I don't quite recommend using it in a serious project like mlibc, because not like the dragonbox repo, this project is far from being completed, not maintained, and not under active development at this moment. I hope I will work on it at some point, but currently I'm too busy living and I don't expect that moment will come soon.