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: Tan is incorrect for large values along the real axis #31566

Open
bmkessler opened this issue Apr 19, 2019 · 1 comment

Comments

Projects
None yet
3 participants
@bmkessler
Copy link
Contributor

commented Apr 19, 2019

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

$ go version go1.12 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="/home/bmkessler/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/bmkessler/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/bmkessler/dev/go/src/go.mod"
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-build347839563=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Attempt to calculate Tan for large values along the real axis

https://play.golang.org/p/7VcgDjysXb9

What did you expect to see?

cmplx.Tan(complex(1<<300, 0)) == (4.60901287677811+0i)

What did you see instead?

cmplx.Tan(complex(1<<300, 0)) == (-0-0i)

This is due to incorrect range reduction by Pi in reducePi.

Additionally, the threshold used in math/trig_reduce.go was not set conservatively enough, which affects both math and math/cmplx. The prior value ensured that catastrophic failure did not occur where the argument x would not actually be reduced below Pi/4. However, errors in reduction begin to occur at values much lower values when z = ((x - y*PI4A) - y*PI4B) - y*PI4C is not exact because y*PI4A cannot be exactly represented as a float64. For example:

math.Tan(1<<35) == 0.8427614121783361
But, the true value is 0.84276385870875983

An error in the sixth place.

@gopherbot

This comment has been minimized.

Copy link

commented Apr 19, 2019

Change https://golang.org/cl/172838 mentions this issue: math/cmplx: implement Payne-Hanek range reduction

@julieqiu julieqiu added this to the Go1.13 milestone Apr 22, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.