fix: correct %g and %e exponent for powers of 10#882
Open
He-Pin wants to merge 1 commit into
Open
Conversation
Motivation: %g % 1000000 produced "1000000" instead of "1e+06", and %.2g % 1000 produced "10e+02" instead of "1e+03". Root cause: std.log(x)/std.log(10) suffers from floating-point imprecision for powers of 10 (e.g. log(1000000)/log(10) = 5.999999... instead of 6), causing std.floor to round down incorrectly. Modification: Added 1e-12 epsilon before std.floor in exponent calculation in both render_float_sci and the %g format handler in the standard library. Regenerated astgen/stdast.go from the updated stdlib. Result: - "%g" % 1000000 → "1e+06" (was "1000000") - "%.2g" % 1000 → "1e+03" (was "10e+02") - "%e" % 1000000 → "1.000000e+06" (was "1.000000e+05" with wrong mantissa) - All other %g/%e cases unchanged. Upstream PR: google/jsonnet#1324
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.
Motivation
%g % 1000000produces"1000000"instead of"1e+06", and%.2g % 1000produces"10e+02"instead of"1e+03". Cprintfand Python both produce the correct output.Root cause:
std.log(x)/std.log(10)in the standard library suffers from floating-point imprecision for powers of 10:log(1000000)/log(10)=5.9999999999999991(should be 6)log(1000)/log(10)=2.9999999999999996(should be 3)std.floorthen rounds these down incorrectly.Modification
Added
1e-12epsilon beforestd.floorin the exponent calculation in two places incpp-jsonnet/stdlib/std.jsonnet:render_float_sci(used by%e/%E)%g/%Gformat handlerRegenerated
astgen/stdast.gofrom the updated standard library.The epsilon is ~1000x the floating-point error (~1e-15) but far too small to affect non-power-of-10 values.
Result
"%g" % 1000000"1000000""1e+06""1e+06""%.2g" % 1000"10e+02""1e+03""1e+03""%e" % 1000000"1.000000e+05"(wrong)"1.000000e+06""1.000000e+06""%G" % 1000000"10E+05""1E+06""1E+06"All other
%g/%ecases produce identical output before and after this fix.Test plan
go test ./...— all tests passprintfand Python%gfor: 0, ±1000000, 1e10, 1e-10, 123456, 999999, 3.14, various precision specifiersRelated
cpp-jsonnet/stdlib/std.jsonnet)cpp-jsonnetsubmodule reference should be updated