-
-
Notifications
You must be signed in to change notification settings - Fork 185
Introduce a heuristic to print floating point numbers more coincely #89
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
Conversation
|
I guess I do need to fix the tests somehow. I can't buld them because meson says |
marzer
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also need to regen toml.hpp ^_^
| const auto res = hexfloat | ||
| ? std::to_chars(buf, buf + sizeof(buf), val, std::chars_format::hex) | ||
| : std::to_chars(buf, buf + sizeof(buf), val); | ||
| ? std::to_chars(buf, buf + sizeof(buf), val, std::chars_format::hex, decimal_places) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to do this in the hexfloat case; full precision is assumed for hexfloats (that's sorta the whole point of them).
|
After looking at the tests, I'm not sure this is possible to do while still retaining the round-trip thing, because "0.3" is not written as "0.30000000000000004" anymore, basically. I guess this isn't going to work out after all and I'll have to maintain a fork with precision setting disabled after all. Feel free to close this, I dont' really understand how to fix this issue. Hence this change breaks all the tests, basically. Sorry for wasting your time. |
That's alright. It's kinda a damned-either-way scenario, really. Thanks for giving it a crack. |
Please do! Then I can finally remove that fork I've been using with a single line changed.. |
|
@vaartis I've implemented this in the v3 branch. val1 = 0.010284358729827818
val2 = 0.0102
val3 = 10.0102
val4 = 10.010284358729827818
val5 = 10.0using formatter = toml::toml_formatter;
std::cout << cfg << "\n\n";
std::cout << formatter{ cfg, formatter::default_flags | toml::format_flags::relaxed_float_precision };val1 = 0.010284358729827818
val2 = 0.0102
val3 = 10.0102
val4 = 10.010284358729828
val5 = 10.0
val1 = 0.0102844
val2 = 0.0102
val3 = 10.0102
val4 = 10.0103
val5 = 10.0Currently it applies to the entire formatter stream; there's no way to do it per-value. I could do that (using the same machinery I use for preserving ints as hex/dec/oct etc), though didn't bother as that seems pretty niche. Let me know if this works for you. |
|
Thank you for implementing this. I think it's probably enough for it to be per-stream, for my use-case it certainly should be. Sort of busy with other things lately, so can't say how soon I can actually test it (since this means i'll also have to port other things like source locations to v3), but judging from your description this seems to be what I need. |
|
Tested this one too and it seems to work fine 👍 |
|
Awesome, good to hear |
What does this change do?
Introduces a heuristic that, for floating pointer numbers that have <=3 positions after the floating point, prints them with just those, and otherwise it prints them with the usual
std::numeric_limits<T>::digits10 + 1precision.Is it related to an exisiting bug report or feature request?
Continuation of #88
Pre-merge checklist
origin/master(if necessary)