Repository navigation
fix: take exponent after rounding in %e and %g - #29
Merged
Merged
Conversation
The number is normalized and the exponent counted before it is rounded to the precision. When rounding carries into a new digit the exponent was not updated, so 9.96 with "%.1e" printed "10.0e+00" instead of "1.0e+01". %g had the same problem when choosing between 'f' and 'e' style, e.g. 999999.7 with "%g" printed "1000000" instead of "1e+06".
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 rounding carries into a new digit,
%eprints a two digit mantissa because the exponent is counted before the number is rounded:%ghas the same issue when it picks betweenfandestyle, since the standard uses the exponent after rounding toP - 1digits:The fix rounds once in
estyle before the decision, and if the result reached 10 it bumps the exponent and continues with 1.0. The rest of the code is unchanged.I added these cases to
test_basic.c. I also compared against glibcsnprintfwith random values for%e,%.0e,%.1e,%.3e,%g,%.1g,%.2g,%.3g,%#g,%E,%Gand with widths. Every mismatch that went away was one of these carry cases, and no new mismatch appeared. The ones left are the round-half-to-even differences already mentioned in the comment inprv_calculate_dbl_num_data.I could only build the tests natively on 64-bit here, so the three
%ptests that assume a 32-bit pointer fail locally both with and without this change.