Go version

go version go1.23.1 darwin/arm64; go version go1.23.1 darwin/amd64

Output of go env in your module/workspace:

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

What did you do?

The following code produces inconsistent results based on architecture:

package main

import (
    "fmt"
    "math"
)

func main() {
    n := float64(math.MaxInt64)
    fmt.Printf("n        = %f\n", n)
    fmt.Printf("int64(n) = %d\n", int64(n))
}

What did you see happen?

On ARM64, this produces

n        = 9223372036854775808.000000
int64(n) = 9223372036854775807

On x86-64, this produces

n        = 9223372036854775808.000000
int64(n) = -9223372036854775808

What did you expect to see?

The behavior here is consistent between architectures. The Go spec here doesn’t seem to make it 100% clear what should happen:

  1. When converting a floating-point number to an integer, the fraction is discarded (truncation towards zero).

However, inferring from elsewhere in the spec, it seems the above code should produce the following steps:

  1. Truncate the floating-point number towards zero producing an integer of infinite precision (producing 9223372036854775808).
  2. Convert said integer to int64 (overflow producing -9223372036854775808).

This gels best with the behavior in the following code:

package main

import (
    "fmt"
    "math"
)

func main() {
    n := math.MaxInt64
    fmt.Println(n + 1)
}

which produces -9223372036854775808 on both architectures.

ARM64 is apparently rounding and truncating in a single step (producing 9223372036854775807).

Alternatively, perhaps the spec should allow this behavior—in that case, we could update the spec to make this explicit.

Comment From: gabyhelp

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

Comment From: nicholasngai

Oop, I guess this is aggressively duplicated already lol. Closing.