diff options
| author | Russ Cox <rsc@golang.org> | 2025-10-28 22:00:26 -0400 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2025-10-29 11:00:23 -0700 |
| commit | 9035f7aea538c25a11420bce7cbd8225efc204e7 (patch) | |
| tree | b051053ddcd426a692bbb7181377a52e434f40bf /src/runtime | |
| parent | 49c1da474d68045458b9462b743e3f0f7dcefdfb (diff) | |
| download | go-9035f7aea538c25a11420bce7cbd8225efc204e7.tar.xz | |
runtime: use internal/strconv
Runtime doing its own number formatting dates back to
when runtime was the bottom-most Go package.
Those days are long gone. Use internal/strconv to avoid
duplicating code and also to get better floating-point
formatting:
% go1.24.6 run x.go
+1.234568e+004
% go run x.go
12345.678
%
With accurate floating point it becomes necessary to
introduce separate printers for float32 vs float64 and
for complex64 vs complex128. Otherwise float32(93.7)
prints as 93.69999694824219.
Change-Id: I25ae3f09519342dc3d1dcabf4711651423e00128
Reviewed-on: https://go-review.googlesource.com/c/go/+/716002
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/runtime')
| -rw-r--r-- | src/runtime/panic_test.go | 8 | ||||
| -rw-r--r-- | src/runtime/print.go | 104 |
2 files changed, 23 insertions, 89 deletions
diff --git a/src/runtime/panic_test.go b/src/runtime/panic_test.go index 994abfdd45..4a30c30db6 100644 --- a/src/runtime/panic_test.go +++ b/src/runtime/panic_test.go @@ -18,10 +18,10 @@ func TestPanicWithDirectlyPrintableCustomTypes(t *testing.T) { wantPanicPrefix string }{ {"panicCustomBool", `panic: main.MyBool(true)`}, - {"panicCustomComplex128", `panic: main.MyComplex128(+3.210000e+001+1.000000e+001i)`}, - {"panicCustomComplex64", `panic: main.MyComplex64(+1.100000e-001+3.000000e+000i)`}, - {"panicCustomFloat32", `panic: main.MyFloat32(-9.370000e+001)`}, - {"panicCustomFloat64", `panic: main.MyFloat64(-9.370000e+001)`}, + {"panicCustomComplex128", `panic: main.MyComplex128(32.1+10i)`}, + {"panicCustomComplex64", `panic: main.MyComplex64(0.11+3i)`}, + {"panicCustomFloat32", `panic: main.MyFloat32(-93.7)`}, + {"panicCustomFloat64", `panic: main.MyFloat64(-93.7)`}, {"panicCustomInt", `panic: main.MyInt(93)`}, {"panicCustomInt8", `panic: main.MyInt8(93)`}, {"panicCustomInt16", `panic: main.MyInt16(93)`}, diff --git a/src/runtime/print.go b/src/runtime/print.go index 0b05aedad3..e32ecb9450 100644 --- a/src/runtime/print.go +++ b/src/runtime/print.go @@ -6,6 +6,7 @@ package runtime import ( "internal/goarch" + "internal/strconv" "unsafe" ) @@ -118,101 +119,34 @@ func printbool(v bool) { } } -func printfloat(v float64) { - switch { - case v != v: - printstring("NaN") - return - case v+v == v && v > 0: - printstring("+Inf") - return - case v+v == v && v < 0: - printstring("-Inf") - return - } - - const n = 7 // digits printed - var buf [n + 7]byte - buf[0] = '+' - e := 0 // exp - if v == 0 { - if 1/v < 0 { - buf[0] = '-' - } - } else { - if v < 0 { - v = -v - buf[0] = '-' - } - - // normalize - for v >= 10 { - e++ - v /= 10 - } - for v < 1 { - e-- - v *= 10 - } - - // round - h := 5.0 - for i := 0; i < n; i++ { - h /= 10 - } - v += h - if v >= 10 { - e++ - v /= 10 - } - } - - // format +d.dddd+edd - for i := 0; i < n; i++ { - s := int(v) - buf[i+2] = byte(s + '0') - v -= float64(s) - v *= 10 - } - buf[1] = buf[2] - buf[2] = '.' +func printfloat64(v float64) { + var buf [20]byte + gwrite(strconv.AppendFloat(buf[:0], v, 'g', -1, 64)) +} - buf[n+2] = 'e' - buf[n+3] = '+' - if e < 0 { - e = -e - buf[n+3] = '-' - } +func printfloat32(v float32) { + var buf [20]byte + gwrite(strconv.AppendFloat(buf[:0], float64(v), 'g', -1, 32)) +} - buf[n+4] = byte(e/100) + '0' - buf[n+5] = byte(e/10)%10 + '0' - buf[n+6] = byte(e%10) + '0' - gwrite(buf[:]) +func printcomplex128(c complex128) { + var buf [44]byte + gwrite(strconv.AppendComplex(buf[:0], c, 'g', -1, 128)) } -func printcomplex(c complex128) { - print("(", real(c), imag(c), "i)") +func printcomplex64(c complex64) { + var buf [44]byte + gwrite(strconv.AppendComplex(buf[:0], complex128(c), 'g', -1, 64)) } func printuint(v uint64) { - var buf [100]byte - i := len(buf) - for i--; i > 0; i-- { - buf[i] = byte(v%10 + '0') - if v < 10 { - break - } - v /= 10 - } - gwrite(buf[i:]) + var buf [20]byte + gwrite(strconv.AppendUint(buf[:0], v, 10)) } func printint(v int64) { - if v < 0 { - printstring("-") - v = -v - } - printuint(uint64(v)) + var buf [20]byte + gwrite(strconv.AppendInt(buf[:0], v, 10)) } var minhexdigits = 0 // protected by printlock |
