Skip to content

fmt: sub-10 exponents in formatted floats are zero-padded to at least two digits #70862

@hudlow

Description

@hudlow

Go version

go version go1.23.4 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/zoomzoom/Library/Caches/go-build'
GOENV='/Users/zoomzoom/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/zoomzoom/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/zoomzoom/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/zoomzoom/brew/Cellar/go/1.23.4/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/Users/zoomzoom/brew/Cellar/go/1.23.4/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23.4'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/zoomzoom/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3247814003=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

Using %e, %E, %x, or %X to format a floating-point value that ends up with an exponent between 0 and 9.

https://go.dev/play/p/oaSxLmPizik

fmt.Printf("%e, %E, %x, %X, %b\n", 1e0, 1e0, 1e0, 1e0, 5e+15)
fmt.Printf("%e, %E, %x, %X, %b\n", 1e1, 1e1, 1e1, 1e1, 1e+16)
fmt.Printf("%e, %E, %x, %X, %b\n", 1e10, 1e10, 1e10, 1e10, 1e20)
fmt.Printf("%e, %E, %x, %X, %b\n", 1e100, 1e100, 1e100, 1e100, 1e100)

What did you see happen?

The exponent value is zero-padded to two digits. Interestingly, %b does not exhibit this behavior.

1.000000e+00, 1.000000E+00, 0x1p+00, 0X1P+00, 5000000000000000p+0
1.000000e+01, 1.000000E+01, 0x1.4p+03, 0X1.4P+03, 5000000000000000p+1
1.000000e+10, 1.000000E+10, 0x1.2a05f2p+33, 0X1.2A05F2P+33, 6103515625000000p+14
1.000000e+100, 1.000000E+100, 0x1.249ad2594c37dp+332, 0X1.249AD2594C37DP+332, 5147557589468029p+280

What did you expect to see?

The exponent value is not zero-padded.

I realize it may be a bit cheeky to file this as a bug, given that the behavior seems deliberately coded. But, I couldn't find any documentation saying that it is correct behavior, and it doesn't seem to make much sense given that it does not result in a uniform length for exponents since they can also be three digits long.

I am also genuinely curious if it is intentional that %b behaves differently in this respect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.NeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions