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

Some more issues about to_chars #166

Open
jk-jeon opened this issue Feb 20, 2024 · 6 comments
Open

Some more issues about to_chars #166

jk-jeon opened this issue Feb 20, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@jk-jeon
Copy link
Contributor

jk-jeon commented Feb 20, 2024

  1. It seems that std::to_chars returns errc::value_too_large rather than errc::result_out_of_range. Is this simply a mistake or do you have a reason for using errc::result_out_of_range instead? In the code I edited I used errc::result_out_of_range for consistency.

  2. to_chars with only buffer & value, with the format param as well, and with the precision param as well, all should be separate overloads. The reason why the first and the second should be separate overloads is because the first one should behave differently from the second one with fmt == chars_format::general; it needs to select whatever representation that is shortest, which does not always need to be equal to chars_format::general's output. The reason why the second and the third should be separate overloads is because by the spec, precision being negative should be treated as if precision == 6, because the spec of std::printf says that negative precision is ignored, which means it should fall back to the default, which is 6. I guess this is quite stupid, but well, it seems that's how the spec is written anyway.

EDIT: Ah, forgot to mention. When fixed format is chosen for the shortest representation, the output of Dragonbox may not be the correctly rounded one, because there can be trailing zeros in the integer part. (See fmtlib/fmt#3649 for a related discussion in libfmt.)

@pdimov
Copy link
Member

pdimov commented Feb 20, 2024

It seems that std::to_chars returns errc::value_too_large rather than errc::result_out_of_range. Is this simply a mistake or do you have a reason for using errc::result_out_of_range instead?

That's how std::to_chars is specified, and it's consistent with POSIX error handling. ERANGE is when a result can't fit in the range of the numeric output; EOVERFLOW is when there's a buffer overflow.

@jk-jeon
Copy link
Contributor Author

jk-jeon commented Feb 20, 2024

@pdimov I mean, boost::charconv::to_chars returns errc::result_out_of_range while std::to_chars seems to be supposed to return errc::value_too_large. Is there a reason for this divergence?

@pdimov
Copy link
Member

pdimov commented Feb 20, 2024

That should be a bug, then.

@mborland mborland added the bug Something isn't working label Feb 20, 2024
@mborland
Copy link
Member

Is there a reason for this divergence?

No; it will be fixed by linked PR.

@mborland
Copy link
Member

mborland commented Feb 20, 2024

  1. to_chars with only buffer & value, with the format param as well, and with the precision param as well, all should be separate overloads. The reason why the first and the second should be separate overloads is because the first one should behave differently from the second one with fmt == chars_format::general; it needs to select whatever representation that is shortest, which does not always need to be equal to chars_format::general's output.

I believe the difference is between formatting with 1e-4 as the crossover point between fixed and scientific like in your last issue right? Since the goal is the absolute minimum number of characters? https://godbolt.org/z/bnscGEbMn

The reason why the second and the third should be separate overloads is because by the spec, precision being negative should be treated as if precision == 6, because the spec of std::printf says that negative precision is ignored, which means it should fall back to the default, which is 6. I guess this is quite stupid, but well, it seems that's how the spec is written anyway.

That does seem to be the case: https://godbolt.org/z/6xPMKeTcM

@jk-jeon
Copy link
Contributor Author

jk-jeon commented Feb 21, 2024

I believe the difference is between formatting with 1e-4 as the crossover point between fixed and scientific like in your last issue right? Since the goal is the absolute minimum number of characters? https://godbolt.org/z/bnscGEbMn

Yeah, 1e-4 should be printed as 1e-4 because it's shorter than 0.0001, and similarly 1e-3 is preferred over 0.001. But those are "easy" cases. Real headaches are the cases like the ones described in the fmt issue I linked (like 123456792.0f, in case it was not clear).

mborland added a commit that referenced this issue Feb 22, 2024
Fix for issue #166 negative precision
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants