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: finite scaling of complex infinity gives complex NaN #38757

Closed
kortschak opened this issue Apr 30, 2020 · 2 comments
Closed

cmd/compile: finite scaling of complex infinity gives complex NaN #38757

kortschak opened this issue Apr 30, 2020 · 2 comments

Comments

@kortschak
Copy link
Contributor

@kortschak kortschak commented Apr 30, 2020

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

$ go version
go version go1.14.2 linux/amd64

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="on"
GOARCH="amd64"
GOBIN="/home/user/bin"
GOCACHE="/home/user/.cache/go-build"
GOENV="/home/user/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/user"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/user/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/user/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
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 -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build634029336=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Run the code at https://play.golang.org/p/0WqB9HZZFwA

What did you expect to see?

should be complex inf: (+Inf+Infi)

What did you see instead?

should be complex inf: (NaN+NaNi)
@kortschak kortschak changed the title cmd/compile: finite scaling of complex infinity give complex NaN cmd/compile: finite scaling of complex infinity gives complex NaN Apr 30, 2020
@bmkessler
Copy link
Contributor

@bmkessler bmkessler commented May 1, 2020

This is working as intended. The issue is the untyped constant 42 is converted to a complex number with zero imaginary part and the multiplication between Inf and 0.0 is NaN.

cmplx.Inf()*42 == complex(math.Inf(1), math.Inf(1)) * (42 + 0i)
... == (math.Inf(1)*42-math.Inf(1)*0) + (math.Inf(1)*0+math.Inf(1)*42) i
... == (math.Inf(1) - math.NaN()) + (math.NaN() + math.Inf()) i
... == math.Nan() +math.NaN() i

To scale by a real number rather than multiply by a complex number with zero imaginary part you need to write this as:

x := cmplx.Inf()
x = complex(42 * real(x), 42 * imag(x))

See #38780 for further discussion.

@kortschak
Copy link
Contributor Author

@kortschak kortschak commented May 1, 2020

Yeah, OK. The spec is terse on this. As mentioned in the linked issue, perhaps that could be improved.

@andybons andybons closed this May 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.