Repository navigation
fix: count default precision digits for width in %f and %e - #30
Open
emincan2000 wants to merge 1 commit into
Open
emincan2000 wants to merge 1 commit into
emincan2000 wants to merge 1 commit into
Conversation
Without an explicit precision the decimals were left out of the length used for padding, so "%10f" of 3.5 printed 17 characters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When %f or %e has a width but no precision, the padding calculation leaves out the six default decimals and the dot. The width only covers the integer part, so printf("%10f", 3.5) produces " 3.500000" with 17 characters instead of " 3.500000". Formatting 123.456 with "%14e" pads the result to 21 characters. An explicit precision such as "%10.6f" already worked because the decimals were counted when the precision flag was set. This change counts them whenever the chosen precision is above zero.
I compared lwprintf_snprintf with glibc snprintf using 300000 random formats each for f, e, g and mixed f and e. The flags, widths and precisions were random too. Mismatches dropped from 25590 to 5262 for f and from 23545 to 6212 for e. The results for g are unchanged, and every format that matched glibc before still matches. The remaining mismatches involve ties that glibc rounds to even, the # flag and double rounding near the last digit. This change does not touch those cases.
I added five width tests to test_basic.c. They fail before the change and pass after it. The only other failures are the three 32 bit %p tests, which fail on a 64 bit host both before and after the change.