Repository navigation
fix: remove trailing zeros and handle zero and zero precision for %g in string/base/format-interpolate - #16538
Open
rajanpanth wants to merge 1 commit into
Open
rajanpanth wants to merge 1 commit into
rajanpanth wants to merge 1 commit into
Conversation
…` in `string/base/format-interpolate` Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 of 7 tasks
Member
|
@Planeshifter Mind taking a look at these changes since you were the one which originally added this functionality? |
This branch has not been deployed
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 pull request:
%g/%Gspecifier instring/base/format-interpolate(and thusstring/format), which only removed trailing zeros from exponential output and from a bare.0. For example,format( '%g', 1.5 )returned'1.50000', rather than'1.5'as in C'sprintf, andformat( '%g', 100 )returned'100.000', rather than'100'.'0', rather than'0e+00'. Previously, every value with an absolute value less than1e-4, including zero, used exponential notation.format( '%.0g', 1.5 )threwRangeError: toPrecision() argument must be between 1 and 100.Xof the value rounded toPsignificant digits (fixed-point whenP > X >= -4), which is the rule in the C standard. As a result,format( '%g', 0.0001 )returns'0.0001'(previously'0.000100000') andformat( '%g', 0.00009999995 )returns'0.0001'(previously'1e-04').#).format( '%#.0f', -3 )now returns'-3.', as it already did for positive values, andformat( '%#.3g', -340.12 )returns'-340.'.printf)%g1.51.500001.5%g100100.000100%g00e+000%g0.00010.0001000000.0001%.3g0.10.1000.1%G0.50.5000000.5%10g1.51.500001.5%.0g1.52%#.3g-340.12-340-340.Related Issues
None.
Questions
No.
Other
%operator (which follows C'sprintffor floating-point conversions) for 5,950 combinations of 425 values (fixed and pseudo-randomly generated, spanning1e-21to1e100) and 14%gformat strings (precision0through17,#,+,-,0, width, and%G). Before this change, 630 mismatched; after this change, 2 mismatch.format( '%.0g', -2.5 )returns'-3', whereas C returns'-2'). C rounds exact ties to even, whileNumber.prototype.toExponentialandNumber.prototype.toFixedround ties away from zero. This also applies to%eand%f, predates this change, and is not addressed here.%3.Gand%#3.Gcases) pass unchanged. I ran the tests forstring/base/format-interpolateandstring/formatdirectly with Node.js andtapeon Windows (asmakewas not available in my environment); I did not run the full repository lint pipeline locally.Checklist
AI Assistance
If you answered "yes" above, how did you use AI assistance?
Disclosure
This PR was written with the help of Claude Code, which found the issue, drafted the implementation change and tests, and ran the comparison described above. I reviewed the changes and the test results before submitting.
@stdlib-js/reviewers
🤖 Generated with Claude Code