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

cmd/compile: casting a negative float to uint64 has different behavior on arm64 and amd64 #52943

Closed
e1ijah1 opened this issue May 17, 2022 · 1 comment

Comments

@e1ijah1
Copy link

e1ijah1 commented May 17, 2022

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

$ go version
go1.18.2 linux/arm64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_arm64"
GOVCS=""
GOVERSION="go1.18.2"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3141436172=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import "runtime"

func main() {
	var f64 float64 = -2.99

	println(runtime.GOARCH)
	println("casting float64 to uint64 given negative num", f64, uint64(f64))
}

amd64 result:

amd64
casting float64 to uint64 given negative num -2.990000e+000 18446744073709551614

arm64 result:

arm64
casting float64 to uint64 given negative num -2.990000e+000 0

What did you expect to see?

The behavior of casting a negative float to unsigned int should be the same on different platforms, just like rust.

What did you see instead?

var x float64 = -2.99
uint64(x) == 0
@cherrymui
Copy link
Member

The spec says (https://go.dev/ref/spec#Conversions)

In all non-constant conversions involving floating-point or complex values, if the result type cannot represent the value the conversion succeeds but the result value is implementation-dependent.

This works as intended. Thanks.

@golang golang locked and limited conversation to collaborators May 17, 2023
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