math/cmplx: Tan is incorrect for large values along the real axis #31566
Labels
FrozenDueToAge
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat 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 bothmath
andmath/cmplx
. The prior value ensured that catastrophic failure did not occur where the argumentx
would not actually be reduced belowPi/4
. However, errors in reduction begin to occur at values much lower values whenz = ((x - y*PI4A) - y*PI4B) - y*PI4C
is not exact becausey*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.
The text was updated successfully, but these errors were encountered: