1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
9 // The original C code, the long comment, and the constants
10 // below are from http://netlib.sandia.gov/cephes/c9x-complex/clog.c.
11 // The go code is a simplified version of the original C.
13 // Cephes Math Library Release 2.8: June, 2000
14 // Copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
16 // The readme file at http://netlib.sandia.gov/cephes/ says:
17 // Some software in this archive may be from the book _Methods and
18 // Programs for Mathematical Functions_ (Prentice-Hall or Simon & Schuster
19 // International, 1989) or from the Cephes Mathematical Library, a
20 // commercial product. In either event, it is copyrighted by the author.
21 // What you see here may be used freely but it comes with no support or
24 // The two known misprints in the book are repaired here in the
25 // source listings for the gamma function and the incomplete beta
29 // moshier@na-net.ornl.gov
31 // Complex circular tangent
41 // w = --------------------.
44 // On the real axis the denominator is zero at odd multiples
45 // of PI/2. The denominator is evaluated by its Taylor
46 // series near these points.
48 // ctan(z) = -i ctanh(iz).
53 // arithmetic domain # trials peak rms
54 // DEC -10,+10 5200 7.1e-17 1.6e-17
55 // IEEE -10,+10 30000 7.2e-16 1.2e-16
56 // Also tested by ctan * ccot = 1 and catan(ctan(z)) = z.
58 // Tan returns the tangent of x.
59 func Tan(x complex128
) complex128
{
60 d
:= math
.Cos(2*real(x
)) + math
.Cosh(2*imag(x
))
61 if math
.Fabs(d
) < 0.25 {
67 return cmplx(math
.Sin(2*real(x
))/d
, math
.Sinh(2*imag(x
))/d
)
70 // Complex hyperbolic tangent
74 // tanh z = (sinh 2x + i sin 2y) / (cosh 2x + cos 2y) .
79 // arithmetic domain # trials peak rms
80 // IEEE -10,+10 30000 1.7e-14 2.4e-16
82 // Tanh returns the hyperbolic tangent of x.
83 func Tanh(x complex128
) complex128
{
84 d
:= math
.Cosh(2*real(x
)) + math
.Cos(2*imag(x
))
88 return cmplx(math
.Sinh(2*real(x
))/d
, math
.Sin(2*imag(x
))/d
)
91 // Program to subtract nearest integer multiple of PI
92 func reducePi(x
float64) float64 {
94 // extended precision value of PI:
95 DP1
= 3.14159265160560607910E0
// ?? 0x400921fb54000000
96 DP2
= 1.98418714791870343106E-9 // ?? 0x3e210b4610000000
97 DP3
= 1.14423774522196636802E-17 // ?? 0x3c6a62633145c06e
105 t
= float64(int64(t
)) // int64(t) = the multiple
106 return ((x
- t
*DP1
) - t
*DP2
) - t
*DP3
109 // Taylor series expansion for cosh(2y) - cos(2x)
110 func tanSeries(z complex128
) float64 {
111 const MACHEP
= 1.0 / (1 << 53)
112 x
:= math
.Fabs(2 * real(z
))
113 y
:= math
.Fabs(2 * imag(z
))
142 if math
.Fabs(t
/d
) <= MACHEP
{
149 // Complex circular cotangent
158 // sin 2x - i sinh 2y
159 // w = --------------------.
162 // On the real axis, the denominator has zeros at even
163 // multiples of PI/2. Near these points it is evaluated
164 // by a Taylor series.
169 // arithmetic domain # trials peak rms
170 // DEC -10,+10 3000 6.5e-17 1.6e-17
171 // IEEE -10,+10 30000 9.2e-16 1.2e-16
172 // Also tested by ctan * ccot = 1 + i0.
174 // Cot returns the cotangent of x.
175 func Cot(x complex128
) complex128
{
176 d
:= math
.Cosh(2*imag(x
)) - math
.Cos(2*real(x
))
177 if math
.Fabs(d
) < 0.25 {
183 return cmplx(math
.Sin(2*real(x
))/d
, -math
.Sinh(2*imag(x
))/d
)