-
-
Notifications
You must be signed in to change notification settings - Fork 348
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
Improve YAML formatting #1133
Improve YAML formatting #1133
Conversation
8934528
to
9916bf8
Compare
Codecov Report
@@ Coverage Diff @@
## main #1133 +/- ##
==========================================
+ Coverage 73.49% 73.52% +0.02%
==========================================
Files 365 365
Lines 48187 48242 +55
==========================================
+ Hits 35417 35470 +53
- Misses 12770 12772 +2
Continue to review full report at Codecov.
|
c7a6eca
to
3e35f46
Compare
Use fmt::format to detect rounded values.
3e35f46
to
d858677
Compare
d858677
to
7a01c02
Compare
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.
Thanks, @ischoegl. I think these changes to the output formatting generally look very good. However, I did notice that there were a few cases where the output is now outputting trailing zeros in some circumstances, and I don't think this is expected based on the comments in the formatDouble
method. A couple of examples can be seen in the test suite. In test/work/generated-ptcombust.yaml
, I see:
site-density: 1.629771953878800e+13
and in test/work/generated-h2o2-from-xml.yaml
, we have:
rate-constant: {A: 3.57e+04, b: 2.4, Ea: -1061.79321568240}
I'm not sure exactly how this is happening, but perhaps it's worth a few tests that this formatter is actually generating the desired output (recognizing that the lack of tests for this method is an oversight on my fault to begin with 😬).
@speth ... thank you for the review! Interesting find about the double trailing zeros. It is not entirely surprising as I was very careful to not round and inadvertently introduce error. I am now removing double '00' but also added the following explanation to string formatDouble(double x, long int precision)
{
// This function ensures that trailing zeros resulting from round-off error
// are removed. Values are only rounded if at least three digits are removed,
// or the displayed value has multiple trailing zeros. This choice is partially aesthetic (e.g. long blocks of values), but also tries to stay on the safe side while catching compounded round-off error (not just trailing I also added the requested unit test. It's somewhat tricky as the exact number representation may be impacted by the architecture, but I believe what I implemented should have the desired output. One curious finding was that removing every trailing zero breaks the test suite (i.e. removing a single zero may change the actual value!). Removing double trailing zeros appears to be safe. |
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.
Thank you, @ischoegl. This looks good to me.
Leverage
fmt::format
to appropriately round values.Changes proposed in this pull request
double
emitterIf applicable, fill in the issue number this pull request is fixing
Closes #1128 (together with improved formatting documentation introduced in #1112)
If applicable, provide an example illustrating new features this pull request is introducing
(A) Rounding of floating point values
Ensure that floating point values are properly rounded, e.g.
(B) Serialization of lines spanning multiple lines (aka 'Literal'
or folded strings)Strings that contain
'\n'
are written as multiple lines, e.g."spam\nand\neggs"
becomes:which leverages
YAML::Literal
. At the moment, there doesn't appear to be a way to skip the trailing\n
in the equivalent string that is written (i.e. there is no|-
rather than|
inyaml-cpp
for the first line as far as I can tell).FWIW, here's a demo illustrating different literal/folded multiline string versions in YAML.
yaml-cpp
appears to only support one version.Checklist
scons build
&scons test
) and unit tests address code coverage