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: wrong value for Sqrt(-1-0i) #29219

Closed
ktye opened this issue Dec 13, 2018 · 6 comments
Closed

math/cmplx: wrong value for Sqrt(-1-0i) #29219

ktye opened this issue Dec 13, 2018 · 6 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@ktye
Copy link

ktye commented Dec 13, 2018

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

$ go version
go version go1.11.2 windows/amd64

Does this issue reproduce with the latest release?

yes
Also in playground.

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

go env Output
$ go env
set GOARCH=amd64
set GOBIN=
set GOCACHE=D:\Users\xxx\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=D:/go
set GOPROXY=
set GORACE=
set GOROOT=C:\local\go
set GOTMPDIR=
set GOTOOLDIR=C:\local\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=D:\Users\xxx\AppData\Local\Temp\go-build313574490=/tmp/go-build -gno-record-gcc-switches

What did you do?

https://play.golang.org/p/fB9f-yYvErS

Negating a complex128 with the value 1 results in (-1-0i), while negating complex(1,0) results in (-1+0i).
This might still be acceptable, but taking the sqrt of the former results in (0-1i)

What did you expect to see?

(0+1i)

What did you see instead?

(0-1i)

@bradfitz bradfitz changed the title wrong cmplx.Sqrt of -1-0i math/cmplx: wrong value for Sqrt(-1-0i) Dec 13, 2018
@bradfitz bradfitz added this to the Go1.13 milestone Dec 13, 2018
ktye added a commit to ktye/goissues that referenced this issue Dec 13, 2018
@bradfitz
Copy link
Contributor

/cc @griesemer @robpike

@bradfitz bradfitz added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 13, 2018
@bmkessler
Copy link
Contributor

Note that this is the correct behavior of the Sqrt function along the branch cut on the negative real axis. This issue was previously corrected in #6888.

For reference see "Branch Cuts for Complex Elementary Functions or Much Ado About Nothing's Sign Bit" W. Kahan:

https://people.freebsd.org/~das/kahan86branch.pdf

Eq. 4.2

Sqrt(-1+i0) = +0 + i = lim Sqrt(-1 + iy) at y= +0
Sqrt(-1-i0) = +0 - i = lim Sqrt(-1 + iy) at y =-0

@robpike
Copy link
Contributor

robpike commented Dec 13, 2018

Seems to me to be working as intended.

@ktye
Copy link
Author

ktye commented Dec 14, 2018

Thanks for the link. I agree, the behaviour of cmplx.Sqrt is correct.

I am more surprised how negation works:
x := -float64(0) does not negate, x is still 0.
y := -x negates, y is now -0
https://play.golang.org/p/HxPPvjBlbLS

This translates to complex numbers, where taking a square root has the aforementioned effect.

@bradfitz
Copy link
Contributor

I am more surprised how negation works:

What you're getting confused by is constants. See https://blog.golang.org/constants

Notably, there are no negative zero constants. It's not a IEEE 754 floating point number until it's in memory. The expression float64(0) is still a constant, and so is -float64(0), both of which are just zero, and not a floating point representation of zero.

I agree that can be confusing.

@ktye
Copy link
Author

ktye commented Dec 14, 2018

Thank you for the explanation.
I always had mixed feelings about constants...

@ktye ktye closed this as completed Dec 14, 2018
@golang golang locked and limited conversation to collaborators Dec 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants