Skip to content

fix: count default precision digits for width in %f and %e - #30

Open
emincan2000 wants to merge 1 commit into
MaJerle:developfrom
emincan2000:float-width-default-precision
Open

emincan2000 wants to merge 1 commit into
MaJerle:developfrom
emincan2000:float-width-default-precision

Conversation

@emincan2000

Copy link
Copy Markdown
Contributor

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.

Without an explicit precision the decimals were left out of the length
used for padding, so "%10f" of 3.5 printed 17 characters.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant