Skip to content

Floating point semantics: unstable semantics inside/outside of a loop and among architectures #61061

Closed as not planned
@RemiMattheyDoret

Description

@RemiMattheyDoret

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

$ go version go1.20.5 darwin/arm64

Does this issue reproduce with the latest release?

Yes (I have not tried the 1.21-rc though)

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

go env Output
$ go env

GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/remi/Library/Caches/go-build"
GOENV="/Users/remi/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/remi/go/pkg/mod"
GONOPROXY="github.com/edgelaboratories"
GONOSUMDB="github.com/edgelaboratories"
GOOS="darwin"
GOPATH="/Users/remi/go"
GOPRIVATE="github.com/edgelaboratories"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/homebrew/Cellar/go/1.20.5/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/homebrew/Cellar/go/1.20.5/libexec/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.20.5"
GCCGO="gccgo"
AR="ar"
CC="cc"
CXX="c++"
CGO_ENABLED="1"
GOMOD="/Users/remi/test/go.mod"
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 -fdebug-prefix-map=/var/folders/75/728c40g56lzgyzhddw9scyp80000gn/T/go-build3544878519=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Consider the following code

func main() {
	var (
		negativeTwoThirds = -2.0 / 3.0
		negativeSix       = -6.0
		negativeThree     = -3.0
	)

	for _, v := range []float64{negativeTwoThirds} {
		four := negativeSix * v // 4 = (-6.0) * (-2/3)

		print(negativeThree + four)
		printAddition(negativeThree, four)
	}

	four := negativeSix * negativeTwoThirds // 4 = (-6.0) * (-2/3)
	print(negativeThree + four)
}

func print(c float64) {
	fmt.Printf("%f (%b)\n", c, math.Float64bits(c))
}

func printAddition(a, b float64) {
	c := a + b
	fmt.Printf("%f (%b)\n", c, math.Float64bits(c))
}

demo

What did you expect to see?

I expected

1.000000 (11111111110000000000000000000000000000000000000000000000000000)
1.000000 (11111111110000000000000000000000000000000000000000000000000000)
1.000000 (11111111110000000000000000000000000000000000000000000000000000)

This is indeed what I observe on the demo as well as on a linux (with 11th Gen Intel® Core™ i7-1165G7; x86) I tried this code on.

What did you see instead?

On two different macbook (with Apple M1; ARM64), I however observe the following output

1.000000 (11111111101111111111111111111111111111111111111111111111111110)  // print inside the loop
1.000000 (11111111110000000000000000000000000000000000000000000000000000)  // printAddition inside the loop
1.000000 (11111111110000000000000000000000000000000000000000000000000000)  // print outside of the loop

Problem

We observe a rounding error but not on all machines and only in specific circumstances (seemingly, only when the addition is directly performed within a for loop). I see two discrepancies in floating point semantics

  • inside vs outside of the loop
  • between different machine architecture

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions