fix(stdlib): renormalize %e/%g mantissa carry and handle precision 0 - #1336
Open
jdymitarai wants to merge 1 commit into
Open
fix(stdlib): renormalize %e/%g mantissa carry and handle precision 0#1336jdymitarai wants to merge 1 commit into
jdymitarai wants to merge 1 commit into
Conversation
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.
Description
This PR fixes two related formatting bugs in
stdlib/std.jsonnetfor%eand%gconversions (reported in #1327):Mantissa Carry Renormalization:
When formatting floating-point numbers in scientific form (
%e/%E/%g/%G), rounding toprecdecimal places can round the mantissa up to10.0or higher (for example:"%.1e" % 9.99yielded"10.0e+00"instead of"1.0e+01", and"%.0e" % 9.5e10yielded"10e+10"instead of"1e+11").When
mantissa * 10^prec + 0.5 >= 10 * 10^prec, we now detect the carry, increment the exponent by 1, and divide the mantissa by 10 to maintain standard normalized scientific representation (1 <= mantissa < 10).%gPrecision of 0 & Boundary Transition:Per Python format specifications (which Jsonnet follows), a precision of 0 for
%gis treated as equivalent to a precision of 1. Previously, precision 0 resulted in passing negative precision torender_float_dec, leading to corrupted output such as"%.0g" % 1.0->"5e+00".In addition, when rounding produces an exponent carry (e.g.
"%.1g" % 9.9), the effective exponent is updated to1, correctly switching to scientific notation"1e+01"instead of decimal overflow.Fixes #1327.
Verification
test_suite/format.jsonnetfor%e,%E,%g, and%Gtesting mantissa carries, precision 0, and boundary exponent transitions.test_suite/format.jsonnetreturnstrue).