Skip to content

Conversation

@vaartis
Copy link

@vaartis vaartis commented Feb 22, 2021

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 + 1 precision.

Is it related to an exisiting bug report or feature request?

Continuation of #88

Pre-merge checklist

  • I've read CONTRIBUTING.md
  • I've rebased my changes against the current HEAD of origin/master (if necessary)
  • I've added new test cases to verify my change
  • I've regenerated toml.hpp (how-to)
  • I've updated any affected documentation
  • I've rebuilt and run the tests with at least one of:
    • Clang 6 or higher
    • GCC 7 or higher
    • MSVC 19.20 (Visual Studio 2019) or higher
  • I've added my name to the list of contributors in README.md

@vaartis
Copy link
Author

vaartis commented Feb 22, 2021

I guess I do need to fix the tests somehow. I can't buld them because meson says unknown target 'tests/cpp17'...

@marzer marzer self-requested a review February 22, 2021 10:36
Copy link
Owner

@marzer marzer left a 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)
Copy link
Owner

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).

@vaartis
Copy link
Author

vaartis commented Feb 22, 2021

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.

@vaartis vaartis closed this Feb 22, 2021
@marzer
Copy link
Owner

marzer commented Feb 22, 2021

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.

@marzer
Copy link
Owner

marzer commented Nov 10, 2021

@vaartis if you would still like this feature I can probably implement it trivially as part of the v3 branch, since I now use the new format_flags for lots of other opt-in formatting stuff. Something like format_flags::relaxed_float_precision. Sound good?

@vaartis
Copy link
Author

vaartis commented Nov 11, 2021

@vaartis if you would still like this feature I can probably implement it trivially as part of the v3 branch, since I now use the new format_flags for lots of other opt-in formatting stuff. Something like format_flags::relaxed_float_precision. Sound good?

Please do! Then I can finally remove that fork I've been using with a single line changed..

@marzer marzer added the v3 label Dec 12, 2021
@marzer
Copy link
Owner

marzer commented Jan 8, 2022

@vaartis I've implemented this in the v3 branch.

val1 =   0.010284358729827818
val2 =   0.0102
val3 =  10.0102
val4 =  10.010284358729827818
val5 =  10.0
using 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.0

Currently 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.

@marzer marzer added implemented in v3 Fixes + features which were implemented in v3 release. and removed v3 labels Jan 8, 2022
@vaartis
Copy link
Author

vaartis commented Jan 8, 2022

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.

@vaartis vaartis deleted the configurable-setprecision branch May 11, 2022 21:41
@vaartis
Copy link
Author

vaartis commented May 11, 2022

Tested this one too and it seems to work fine 👍

@marzer
Copy link
Owner

marzer commented May 12, 2022

Awesome, good to hear

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

implemented in v3 Fixes + features which were implemented in v3 release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants