Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

printf: floats and doubles do not round correctly #593

Open
64 opened this issue Jun 30, 2022 · 3 comments
Open

printf: floats and doubles do not round correctly #593

64 opened this issue Jun 30, 2022 · 3 comments
Labels

Comments

@64
Copy link
Member

64 commented Jun 30, 2022

See e.g https://github.com/managarm/frigg/blob/51d873bc18d4ca82220a0d59c48b40cad6b87495/include/frg/formatting.hpp#L310-L311 and

// TODO: We don't yet round properly
// sprintf(buf, "%5.2f", 1.2);
// assert(!strcmp(buf, " 1.20"));
// sprintf(buf, "%5.2f", 1.23);
// assert(!strcmp(buf, " 1.23"));
// sprintf(buf, "%5.2f", 1.234);
// assert(!strcmp(buf, " 1.23"));
// sprintf(buf, "%5.2f", 12.345);
// assert(!strcmp(buf, "12.35"));
// sprintf(buf, "%-5.2f", 1.2);
// assert(!strcmp(buf, "1.20 "));

To fix this, we could use a C++ float-to-string implementation like dragonbox.

@64 64 added the bug label Jun 30, 2022
@jk-jeon
Copy link

jk-jeon commented Jul 1, 2022

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.

@64
Copy link
Member Author

64 commented Jul 1, 2022

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?

@jk-jeon
Copy link

jk-jeon commented Jul 1, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants