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

Support single precision floats in grisu formatting #1336

Closed
orivej opened this issue Sep 30, 2019 · 3 comments · Fixed by #1361
Closed

Support single precision floats in grisu formatting #1336

orivej opened this issue Sep 30, 2019 · 3 comments · Fixed by #1361

Comments

@orivej
Copy link
Contributor

orivej commented Sep 30, 2019

Currently fmt::format("{}", 0.1f) with grisu produces 0.10000000149011612 (as would have been expected from double(0.1f)) rather than 0.1.

Single precision formatting differs from double precision only in the calculation of the boundaries: https://github.com/google/double-conversion/blob/v3.1.5/double-conversion/fast-dtoa.cc#L525-L536

@ksharenkov
Copy link

ksharenkov commented Oct 2, 2019

As I see, floating format with "{}" and GRISU is not more compatible with std::defaultfloat in fmtlib 6.0.0

std::cout << fmt::format("{}\n", 1.0 / 3.0);
std::cout << 1.0 / 3.0 << "\n";

outputs

0.3333333333333333
0.333333

(fmtlib 6.0.0, GRISU, VisualStudio 2019 16.3)
It looks like compatibility problem.

@orivej
Copy link
Contributor Author

orivej commented Oct 2, 2019

Please use a separate issue to discuss that. #1033 is related, although it does not talk about the default cout format. {} is not supposed to output the same as cout <<, it is supposed to print the number with enough precision such that reading that number back produces the same number. 1/3 is most accurately approximated by a double 0.3333333333333333, it needs so many digits to distinguish it from the previous double 0.33333333333333326 and the next double 0.33333333333333337; 0.333333 represents a different number.

@vitaut
Copy link
Contributor

vitaut commented Oct 2, 2019

Single precision formatting differs from double precision only in the calculation of the boundaries

Right. This is also needed for compatibility with std::format. @orivej, could you by any chance submit a PR?

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

Successfully merging a pull request may close this issue.

3 participants