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: wrong result for Pow(±0, .5) contrary to documentation #23224

Closed
majnemer opened this issue Dec 22, 2017 · 4 comments
Closed

math: wrong result for Pow(±0, .5) contrary to documentation #23224

majnemer opened this issue Dec 22, 2017 · 4 comments

Comments

@majnemer
Copy link

Please answer these questions before submitting your issue. Thanks!

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

go version go1.9.2 linux/amd64

Does this issue reproduce with the latest release?

I believe go1.9.2 is the latest release.

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/usr/local/google/home/majnemer/go"
GORACE=""
GOROOT="/usr/lib/google-golang"
GOTOOLDIR="/usr/lib/google-golang/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build846653371=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

I ran the following program: https://play.golang.org/p/_lUAaRG9D-P

What did you expect to see?

The documentation for math.Pow says the following:

Pow(±0, y) = +0 for finite y > 0 and not an odd integer

.5 is finite and not an odd integer which means math.Pow should give back +0.

What did you see instead?

math.Pow gave back -0.

@majnemer majnemer changed the title Go gives wrong results for math.Pow(±0, .5) Go gives wrong result for math.Pow(±0, .5) Dec 22, 2017
@majnemer majnemer changed the title Go gives wrong result for math.Pow(±0, .5) math: wrong result for math.Pow(±0, .5) Dec 22, 2017
@gopherbot
Copy link

Change https://golang.org/cl/85415 mentions this issue: math: Fixed wrong result for sqrt(-0)

@odeke-em odeke-em changed the title math: wrong result for math.Pow(±0, .5) math: wrong result for Pow(±0, .5) contrary to documentation Dec 23, 2017
@odeke-em
Copy link
Member

Thank you for reporting this issue @majnemer, I'll loop in @griesemer.

@griesemer griesemer added this to the Go1.11 milestone Dec 24, 2017
@griesemer griesemer self-assigned this Dec 24, 2017
@majnemer
Copy link
Author

There is another, related bug.
Consider: Pow(-Inf, 0.5) == Pow(-0, -0.5) == Pow(±0, y) = +Inf

However, Go gives me back NaN.

I believe the original bug and the one I have just noted if we change the line:

return Sqrt(x)

into something along the lines of the following:

if IsInf(x, -1) {
  return Inf(1)
}
return Abs(Sqrt(x))

@gopherbot
Copy link

Change https://golang.org/cl/85660 mentions this issue: math: correct result for Pow(x, ±.5)

@golang golang locked and limited conversation to collaborators Jan 2, 2019
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

4 participants