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

math/cmplx: Pow(0, NaN) panics #30088

Closed
kortschak opened this issue Feb 5, 2019 · 5 comments
Closed

math/cmplx: Pow(0, NaN) panics #30088

kortschak opened this issue Feb 5, 2019 · 5 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@kortschak
Copy link
Contributor

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

$ go version
go version devel +4b3f04c Thu Jan 10 18:15:48 2019 +0000 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
GOARCH="amd64"
GOBIN=""
GOCACHE=".../.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="..."
GOPROXY=""
GORACE=""
GOROOT=".../go"
GOTMPDIR=""
GOTOOLDIR=".../go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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-build317177083=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Run the code here: https://play.golang.org/p/yfwQqWxgxup or https://play.golang.org/p/fYLIyw9slsj

https://play.golang.org/p/W6okRxbBrvO also look wrong, but does not panic.

What did you expect to see?

NaN+NaNi or similar.

What did you see instead?

panic: not reached

goroutine 1 [running]:
math/cmplx.Pow(0x0, 0x0, 0x0, 0x0, 0x1, 0x7ff80000, 0x0, 0x0, 0x434070, 0xeee60, ...)
	/usr/local/go/src/math/cmplx/pow.go:63 +0x3a0
main.main()
	/tmp/sandbox419620258/main.go:10 +0x60
@bcmills
Copy link
Contributor

bcmills commented Feb 5, 2019

CC @griesemer @rsc for math/cmplx

@bcmills
Copy link
Contributor

bcmills commented Feb 5, 2019

Yep, that code is definitely wrong: it assumes that real(y) is comparable with 0.

go/src/math/cmplx/pow.go

Lines 51 to 63 in 20c110e

r, i := real(y), imag(y)
switch {
case r == 0:
return 1
case r < 0:
if i == 0 {
return complex(math.Inf(1), 0)
}
return Inf()
case r > 0:
return 0
}
panic("not reached")

@andybons andybons added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 5, 2019
@andybons andybons added this to the Unplanned milestone Feb 5, 2019
@bcmills
Copy link
Contributor

bcmills commented Feb 5, 2019

For comparison, math.Pow(0, math.NaN()) returns NaN: https://play.golang.org/p/GoK_-oL24jN

@bcmills bcmills added the NeedsFix The path to resolution is known, but the work has not been done. label Feb 5, 2019
@bcmills bcmills modified the milestones: Unplanned, Go1.13 Feb 5, 2019
@gopherbot gopherbot removed the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 5, 2019
@bcmills
Copy link
Contributor

bcmills commented Feb 5, 2019

C defines the error behavior for cpow to be equivalent to cexp(y*clog(x)).

math/cmplx seems to result in NaN + NaNi in those cases: https://play.golang.org/p/qTvkddJzCtv

@gopherbot
Copy link

Change https://golang.org/cl/161197 mentions this issue: math/cmplx: avoid panic in Pow(x, NaN())

@golang golang locked and limited conversation to collaborators Feb 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants