Honour precision for %e/%E formatting (#13)#95
Open
CedricConday wants to merge 1 commit into
Open
Conversation
The `e`/`E` verbs ignored the precision argument and always emitted the
natural (shortest) significand, so `Text('e', 3)`, `Append(buf, 'e', 3)`
and `fmt.Sprintf("%.3e", d)` all produced full-width output instead of
the requested number of fractional digits.
Round the significand to `prec` digits after the leading digit using the
context's rounding mode, zero-padding when `prec` exceeds the available
digits and carrying into the exponent when rounding overflows the leading
digit (e.g. 9.99 -> 1.00e+1). This matches Go's `%.*e` formatting, in line
with the package's stated goal to "conform to standard Go formatting for
float types". A negative precision keeps the existing natural form, and
`g`/`G` is unaffected (it selects the shortest scientific form).
The three existing `Append(_, 'e', 0)` assertions were asserting the old
behaviour where precision was ignored; they now use -1 (the natural
sentinel). Adds TestDecimal64ExpPrecision covering rounding, zero-padding,
exponent carry, half-to-even, negatives and zero.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbUHs855FyBt2J7cG2Qmhh
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.
Closes #13 (the
%.10e/%.10Echeckbox;%.10f/%.10Fwas already done).Problem
The
e/Everbs ignored the precision argument entirely — they always emitted the natural (shortest) significand:This was inconsistent with
f/F, which already honourprec, and with the package's stated goal to "conform to standard Go formatting for float types" (DefaultFormatContext64doc comment).Fix
Round the significand to
precdigits after the leading digit, using the context's rounding mode, then emit exactly that many fractional digits:Rounding.round+roundStatusprimitives, so it respects the configured rounding mode.precexceeds the stored digits (%.12eof a 9-digit value).9.99 → 1.00e+1.g/Gis untouched: it selects the shortest scientific form, so it routes through the natural path unchanged.The library keeps its own exponent style (no zero-padding, and a zero exponent is omitted for non-zero values); only the mantissa precision changed.
Tests
Append(_, 'e', 0)assertions were asserting the old behaviour (precision ignored). They now use-1, the natural sentinel, so they still document the shortest form.TestDecimal64ExpPrecisioncovers rounding, zero-padding, exponent carry (999999999 → 1e+9), half-to-even on exact decimals (9.95 → 1.0e+1), negatives and zero (0.000e+00), viaText,Appendand thefmt.Formatter(%.*e) path.go test ./...),go vetandgofmtclean.🤖 Generated with Claude Code
https://claude.ai/code/session_01DbUHs855FyBt2J7cG2Qmhh