Skip to content

math/big: incorrect Float formatting for negative/auto prec #71245

Open
@joeycumines

Description

Go version

go version go1.23.4 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/joeyc/Library/Caches/go-build'
GOENV='/Users/joeyc/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/joeyc/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/joeyc/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.23.4/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.23.4/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23.4'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/joeyc/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/Users/joeyc/dev/go-utilpkg/go.mod'
GOWORK='/Users/joeyc/dev/go-utilpkg/go.work'
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=/var/folders/_r/v0qs308n49952w5gddyqznbw0000gn/T/go-build2599410598=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

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

What did you see happen?

[0] 4.9406564584124654417656879e-324:
	float64 JSON: 5e-324
	strconv.FormatFloat g -1 64: 5e-324
	big.Float(53) g -1: 4.940656458412465e-324

[1] 9.8813129168249308835313759e-324:
	float64 JSON: 1e-323
	strconv.FormatFloat g -1 64: 1e-323
	big.Float(53) g -1: 9.88131291682493e-324

[2] 4.3749999999999916800000000e+17:
	float64 JSON: 437499999999999170
	strconv.FormatFloat g -1 64: 4.3749999999999917e+17
	big.Float(53) g -1: 4.3749999999999916e+17

[3] 4.9999999999999916800000000e+17:
	float64 JSON: 499999999999999170
	strconv.FormatFloat g -1 64: 4.9999999999999917e+17
	big.Float(53) g -1: 4.9999999999999916e+17

[4] 4.7619047619047596800000000e+17:
	float64 JSON: 476190476190475970
	strconv.FormatFloat g -1 64: 4.7619047619047597e+17
	big.Float(53) g -1: 4.7619047619047596e+17

[5] 3.7499999999999916800000000e+17:
	float64 JSON: 374999999999999170
	strconv.FormatFloat g -1 64: 3.7499999999999917e+17
	big.Float(53) g -1: 3.7499999999999916e+17

[6] 1.9047619047619038720000000e+18:
	float64 JSON: 1904761904761903900
	strconv.FormatFloat g -1 64: 1.9047619047619039e+18
	big.Float(53) g -1: 1.9047619047619038e+18

[7] 2.2250738585071811263987532e-308:
	float64 JSON: 2.225073858507181e-308
	strconv.FormatFloat g -1 64: 2.225073858507181e-308
	big.Float(53) g -1: 2.2250738585071811e-308

Bonus, float32 example:
strconv.FormatFloat g -1 32: 1.175494e-38
big.Float(24) g -1: 1.1754939e-38

What did you expect to see?

I expected to big.Float's string formatter to do what it says on the tin: "A negative precision selects the smallest number of decimal digits necessary to identify the value x uniquely using x.Prec() mantissa bits".

I also expected the formatter to correctly apply round-to-nearest-half-to-even rounding, which is what I believe decimal implementation (in math/big) is intended to perform.

Seems like a pretty clear bug - I tested these values with python3 and node REPLs, and they all align with strconv.FormatFloat and encoding/json.

Not super relevant, but my original intent was to use the big.Float.Append method to canonicalize JSON numbers (which this issue prevents).

Metadata

Assignees

Labels

BugReportIssues describing a possible bug in the Go implementation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions