Skip to content

strconv: ParseFloat returns out of range error on invalid suffix #46628

@itchyny

Description

@itchyny

What version of Go are you using (go version)?

$ go version
go version go1.16.4 darwin/arm64

Does this issue reproduce with the latest release?

Yes, behavior inconsistency against the document.
This is an undocumented breaking change between 1.14 and 1.15.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/itchyny/Library/Caches/go-build"
GOENV="/Users/itchyny/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/itchyny/.share/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/itchyny/.share/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/homebrew/Cellar/go/1.16.4/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/homebrew/Cellar/go/1.16.4/libexec/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.16.4"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/ph/slcnxxv955n3zk1fzk50rmvr0000gn/T/go-build4154388589=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Execute the following code.

package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Println(strconv.ParseFloat("1e100xxx", 64))
	fmt.Println(strconv.ParseFloat("1e1000xxx", 64))
}

What did you expect to see?

Just as Go <= 1.14,

0 strconv.ParseFloat: parsing "1e100xxx": invalid syntax
0 strconv.ParseFloat: parsing "1e1000xxx": invalid syntax

The strconv.ParseFloat document says

If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax.

If s is syntactically well-formed but is more than 1/2 ULP away from the largest floating point number of the given size, ParseFloat returns f = ±Inf, err.Err = ErrRange.

The input 1e1000xxx here satisfies the first if statement just like 1e100xxx does, so the err.Err should be ErrSyntax, not ErrRange. They are not syntactically well-formed.

The change seems to be the side effect of 1d31f9b, which changed the error on unconsumed characters.

What did you see instead?

0 strconv.ParseFloat: parsing "1e100xxx": invalid syntax
+Inf strconv.ParseFloat: parsing "1e1000xxx": value out of range

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions