Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/types, types2: wrong result of float add float #48315

Closed
180909 opened this issue Sep 10, 2021 · 1 comment
Closed

go/types, types2: wrong result of float add float #48315

180909 opened this issue Sep 10, 2021 · 1 comment

Comments

@180909
Copy link
Contributor

180909 commented Sep 10, 2021

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

$ go version
go version go1.17.1 darwin/arm64

Does this issue reproduce with the latest release?

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="arm64"
GOBIN="/bin"
GOCACHE="/Users/wang/Library/Caches/go-build"
GOENV="/Users/wang/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/wang/go/pkg/mod"
GONOPROXY="gitlab.hexcloud.cn"
GONOSUMDB="gitlab.hexcloud.cn"
GOOS="darwin"
GOPATH="/Users/wang/go"
GOPRIVATE="gitlab.hexcloud.cn"
GOPROXY="https://goproxy.io,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.17.1"
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/fc/5_f38pyn6k915kw7_vvfjhrc0000gn/T/go-build63379070=/tmp/go-build -gno-record-gcc-switches -fno-common"
GOROOT/bin/go version: go version go1.17.1 darwin/arm64
GOROOT/bin/go tool compile -V: compile version go1.17.1
uname -v: Darwin Kernel Version 20.6.0: Wed Jun 23 00:26:27 PDT 2021; root:xnu-7195.141.2~5/RELEASE_ARM64_T8101
ProductName:	macOS
ProductVersion:	11.5.1
BuildVersion:	20G80
lldb --version: lldb-1205.0.28.2
Apple Swift version 5.4.2 (swiftlang-1205.0.28.2 clang-1205.0.19.57)

What did you do?

https://go2goplay.golang.org/p/y9mwmrSPe16

What did you expect to see?

3
foobar
3.33

What did you see instead?

3
foobar
3.3000000000000003

@martisch
Copy link
Contributor

martisch commented Sep 10, 2021

This does not seem specific to generic types. There is precision loss due to float64 (the default type for floats) having limited precision which is working as intended.

The same result is returned for

func add(a, b float64) float64 {
	return a + b
}

See https://floating-point-gui.de/.

For more precise results there is a standard library package math/big can be used and has a type to represent rational numbers.

@golang golang locked and limited conversation to collaborators Sep 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants