OSDN Git Service

Add Go frontend, libgo library, and Go testsuite.
[pf3gnuchains/gcc-fork.git] / libgo / go / math / tan.go
1 // Copyright 2009 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.
4
5 package math
6
7
8 /*
9         Floating point tangent.
10 */
11
12 // Tan returns the tangent of x.
13 func Tan(x float64) float64 {
14         // Coefficients are #4285 from Hart & Cheney. (19.74D)
15         const (
16                 P0 = -.1306820264754825668269611177e+5
17                 P1 = .1055970901714953193602353981e+4
18                 P2 = -.1550685653483266376941705728e+2
19                 P3 = .3422554387241003435328470489e-1
20                 P4 = .3386638642677172096076369e-4
21                 Q0 = -.1663895238947119001851464661e+5
22                 Q1 = .4765751362916483698926655581e+4
23                 Q2 = -.1555033164031709966900124574e+3
24         )
25
26         flag := false
27         sign := false
28         if x < 0 {
29                 x = -x
30                 sign = true
31         }
32         x = x * (4 / Pi) /* overflow? */
33         var e float64
34         e, x = Modf(x)
35         i := int32(e)
36
37         switch i & 3 {
38         case 1:
39                 x = 1 - x
40                 flag = true
41
42         case 2:
43                 sign = !sign
44                 flag = true
45
46         case 3:
47                 x = 1 - x
48                 sign = !sign
49         }
50
51         xsq := x * x
52         temp := ((((P4*xsq+P3)*xsq+P2)*xsq+P1)*xsq + P0) * x
53         temp = temp / (((xsq+Q2)*xsq+Q1)*xsq + Q0)
54
55         if flag {
56                 if temp == 0 {
57                         return NaN()
58                 }
59                 temp = 1 / temp
60         }
61         if sign {
62                 temp = -temp
63         }
64         return temp
65 }