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

printf - Update %g formatting to match GNU #3087

Merged
merged 4 commits into from
Mar 21, 2022
Merged

printf - Update %g formatting to match GNU #3087

merged 4 commits into from
Mar 21, 2022

Conversation

water-ghosts
Copy link
Contributor

Resolves #2775.

This format is unintuitive. Uutils previously had %g return either the standard %f output or scientific notation, whichever was shorter. This seems to be a common summary of the format, but it doesn't fully explain its behavior. For example, printf %g 1.234567 prints 1.23457, while %f prints 1.234567.

This PR changes the definition to apply three rules:

  1. Choose scientific notation or float notation based on the number's magnitude, not its length.
  2. Round to 6 significant figures.
  3. Remove trailing zeroes.

I’ve added several tests to validate these rules, and confirmed that GNU gives the same results for each test.

There are probably better ways to write some of this string parsing. I’m still a bit perplexed by Rust’s complex string rules, so I’d appreciate any feedback or cleanup!

@water-ghosts
Copy link
Contributor Author

Any idea why test_dd::test_final_stats_unspec is failing for the Windows build? I ran the test suite locally on Windows 10 and everything passed.

Copy link
Collaborator

@jfinkels jfinkels left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I restarted the CI tests, they seem to all pass, so that's great.

It's a bit tough to follow the logic, like you imply in the description. Perhaps some comments, documentation, and/or examples to go with the helper functions would make it easier to understand?

Copy link
Contributor

@sylvestre sylvestre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just like @jfinkels said (more comments :)

@sylvestre
Copy link
Contributor

@water-ghosts ping?

@water-ghosts
Copy link
Contributor Author

I've added comments and renamed some variables. Hopefully it's clearer :)

@sylvestre sylvestre requested a review from jfinkels March 21, 2022 08:46
Copy link
Collaborator

@jfinkels jfinkels left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

@sylvestre sylvestre merged commit 435c983 into uutils:main Mar 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

printf: %g floating-point conversion disagrees with GNU output
3 participants