annotate libgo/go/math/tan.go @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 // Copyright 2011 The Go Authors. All rights reserved.
kono
parents:
diff changeset
2 // Use of this source code is governed by a BSD-style
kono
parents:
diff changeset
3 // license that can be found in the LICENSE file.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 package math
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 /*
kono
parents:
diff changeset
8 Floating-point tangent.
kono
parents:
diff changeset
9 */
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 // The original C code, the long comment, and the constants
kono
parents:
diff changeset
12 // below were from http://netlib.sandia.gov/cephes/cmath/sin.c,
kono
parents:
diff changeset
13 // available from http://www.netlib.org/cephes/cmath.tgz.
kono
parents:
diff changeset
14 // The go code is a simplified version of the original C.
kono
parents:
diff changeset
15 //
kono
parents:
diff changeset
16 // tan.c
kono
parents:
diff changeset
17 //
kono
parents:
diff changeset
18 // Circular tangent
kono
parents:
diff changeset
19 //
kono
parents:
diff changeset
20 // SYNOPSIS:
kono
parents:
diff changeset
21 //
kono
parents:
diff changeset
22 // double x, y, tan();
kono
parents:
diff changeset
23 // y = tan( x );
kono
parents:
diff changeset
24 //
kono
parents:
diff changeset
25 // DESCRIPTION:
kono
parents:
diff changeset
26 //
kono
parents:
diff changeset
27 // Returns the circular tangent of the radian argument x.
kono
parents:
diff changeset
28 //
kono
parents:
diff changeset
29 // Range reduction is modulo pi/4. A rational function
kono
parents:
diff changeset
30 // x + x**3 P(x**2)/Q(x**2)
kono
parents:
diff changeset
31 // is employed in the basic interval [0, pi/4].
kono
parents:
diff changeset
32 //
kono
parents:
diff changeset
33 // ACCURACY:
kono
parents:
diff changeset
34 // Relative error:
kono
parents:
diff changeset
35 // arithmetic domain # trials peak rms
kono
parents:
diff changeset
36 // DEC +-1.07e9 44000 4.1e-17 1.0e-17
kono
parents:
diff changeset
37 // IEEE +-1.07e9 30000 2.9e-16 8.1e-17
kono
parents:
diff changeset
38 //
kono
parents:
diff changeset
39 // Partial loss of accuracy begins to occur at x = 2**30 = 1.074e9. The loss
kono
parents:
diff changeset
40 // is not gradual, but jumps suddenly to about 1 part in 10e7. Results may
kono
parents:
diff changeset
41 // be meaningless for x > 2**49 = 5.6e14.
kono
parents:
diff changeset
42 // [Accuracy loss statement from sin.go comments.]
kono
parents:
diff changeset
43 //
kono
parents:
diff changeset
44 // Cephes Math Library Release 2.8: June, 2000
kono
parents:
diff changeset
45 // Copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
kono
parents:
diff changeset
46 //
kono
parents:
diff changeset
47 // The readme file at http://netlib.sandia.gov/cephes/ says:
kono
parents:
diff changeset
48 // Some software in this archive may be from the book _Methods and
kono
parents:
diff changeset
49 // Programs for Mathematical Functions_ (Prentice-Hall or Simon & Schuster
kono
parents:
diff changeset
50 // International, 1989) or from the Cephes Mathematical Library, a
kono
parents:
diff changeset
51 // commercial product. In either event, it is copyrighted by the author.
kono
parents:
diff changeset
52 // What you see here may be used freely but it comes with no support or
kono
parents:
diff changeset
53 // guarantee.
kono
parents:
diff changeset
54 //
kono
parents:
diff changeset
55 // The two known misprints in the book are repaired here in the
kono
parents:
diff changeset
56 // source listings for the gamma function and the incomplete beta
kono
parents:
diff changeset
57 // integral.
kono
parents:
diff changeset
58 //
kono
parents:
diff changeset
59 // Stephen L. Moshier
kono
parents:
diff changeset
60 // moshier@na-net.ornl.gov
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 // tan coefficients
kono
parents:
diff changeset
63 var _tanP = [...]float64{
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
64 -1.30936939181383777646e4, // 0xc0c992d8d24f3f38
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
65 1.15351664838587416140e6, // 0x413199eca5fc9ddd
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
66 -1.79565251976484877988e7, // 0xc1711fead3299176
111
kono
parents:
diff changeset
67 }
kono
parents:
diff changeset
68 var _tanQ = [...]float64{
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
69 1.00000000000000000000e0,
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
70 1.36812963470692954678e4, //0x40cab8a5eeb36572
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
71 -1.32089234440210967447e6, //0xc13427bc582abc96
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
72 2.50083801823357915839e7, //0x4177d98fc2ead8ef
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
73 -5.38695755929454629881e7, //0xc189afe03cbe5a31
111
kono
parents:
diff changeset
74 }
kono
parents:
diff changeset
75
kono
parents:
diff changeset
76 // Tan returns the tangent of the radian argument x.
kono
parents:
diff changeset
77 //
kono
parents:
diff changeset
78 // Special cases are:
kono
parents:
diff changeset
79 // Tan(±0) = ±0
kono
parents:
diff changeset
80 // Tan(±Inf) = NaN
kono
parents:
diff changeset
81 // Tan(NaN) = NaN
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 //extern tan
kono
parents:
diff changeset
84 func libc_tan(float64) float64
kono
parents:
diff changeset
85
kono
parents:
diff changeset
86 func Tan(x float64) float64 {
kono
parents:
diff changeset
87 return libc_tan(x)
kono
parents:
diff changeset
88 }
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 func tan(x float64) float64 {
kono
parents:
diff changeset
91 const (
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
92 PI4A = 7.85398125648498535156e-1 // 0x3fe921fb40000000, Pi/4 split into three parts
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
93 PI4B = 3.77489470793079817668e-8 // 0x3e64442d00000000,
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
94 PI4C = 2.69515142907905952645e-15 // 0x3ce8469898cc5170,
111
kono
parents:
diff changeset
95 )
kono
parents:
diff changeset
96 // special cases
kono
parents:
diff changeset
97 switch {
kono
parents:
diff changeset
98 case x == 0 || IsNaN(x):
kono
parents:
diff changeset
99 return x // return ±0 || NaN()
kono
parents:
diff changeset
100 case IsInf(x, 0):
kono
parents:
diff changeset
101 return NaN()
kono
parents:
diff changeset
102 }
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 // make argument positive but save the sign
kono
parents:
diff changeset
105 sign := false
kono
parents:
diff changeset
106 if x < 0 {
kono
parents:
diff changeset
107 x = -x
kono
parents:
diff changeset
108 sign = true
kono
parents:
diff changeset
109 }
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
110 var j uint64
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
111 var y, z float64
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
112 if x >= reduceThreshold {
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
113 j, z = trigReduce(x)
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
114 } else {
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
115 j = uint64(x * (4 / Pi)) // integer part of x/(Pi/4), as integer for tests on the phase angle
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
116 y = float64(j) // integer part of x/(Pi/4), as float
111
kono
parents:
diff changeset
117
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
118 /* map zeros and singularities to origin */
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
119 if j&1 == 1 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
120 j++
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
121 y++
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
122 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
123
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
124 z = ((x - y*PI4A) - y*PI4B) - y*PI4C
111
kono
parents:
diff changeset
125 }
kono
parents:
diff changeset
126 zz := z * z
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 if zz > 1e-14 {
kono
parents:
diff changeset
129 y = z + z*(zz*(((_tanP[0]*zz)+_tanP[1])*zz+_tanP[2])/((((zz+_tanQ[1])*zz+_tanQ[2])*zz+_tanQ[3])*zz+_tanQ[4]))
kono
parents:
diff changeset
130 } else {
kono
parents:
diff changeset
131 y = z
kono
parents:
diff changeset
132 }
kono
parents:
diff changeset
133 if j&2 == 2 {
kono
parents:
diff changeset
134 y = -1 / y
kono
parents:
diff changeset
135 }
kono
parents:
diff changeset
136 if sign {
kono
parents:
diff changeset
137 y = -y
kono
parents:
diff changeset
138 }
kono
parents:
diff changeset
139 return y
kono
parents:
diff changeset
140 }