OSDN Git Service

8a2edd7e563fa22700941b98b49044d339b86d55
[pf3gnuchains/gcc-fork.git] / libgo / go / math / sin.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         Floating-point sine and cosine.
9
10         Coefficients are #5077 from Hart & Cheney. (18.80D)
11 */
12
13 func sinus(x float64, quad int) float64 {
14         const (
15                 P0 = .1357884097877375669092680e8
16                 P1 = -.4942908100902844161158627e7
17                 P2 = .4401030535375266501944918e6
18                 P3 = -.1384727249982452873054457e5
19                 P4 = .1459688406665768722226959e3
20                 Q0 = .8644558652922534429915149e7
21                 Q1 = .4081792252343299749395779e6
22                 Q2 = .9463096101538208180571257e4
23                 Q3 = .1326534908786136358911494e3
24         )
25         if x < 0 {
26                 x = -x
27                 quad = quad + 2
28         }
29         x = x * (2 / Pi) /* underflow? */
30         var y float64
31         if x > 32764 {
32                 var e float64
33                 e, y = Modf(x)
34                 e = e + float64(quad)
35                 f, _ := Modf(0.25 * e)
36                 quad = int(e - 4*f)
37         } else {
38                 k := int32(x)
39                 y = x - float64(k)
40                 quad = (quad + int(k)) & 3
41         }
42
43         if quad&1 != 0 {
44                 y = 1 - y
45         }
46         if quad > 1 {
47                 y = -y
48         }
49
50         yy := y * y
51         temp1 := ((((P4*yy+P3)*yy+P2)*yy+P1)*yy + P0) * y
52         temp2 := ((((yy+Q3)*yy+Q2)*yy+Q1)*yy + Q0)
53         return temp1 / temp2
54 }
55
56 // Cos returns the cosine of x.
57 func Cos(x float64) float64 {
58         if x < 0 {
59                 x = -x
60         }
61         return sinus(x, 1)
62 }
63
64 // Sin returns the sine of x.
65 func Sin(x float64) float64 { return sinus(x, 0) }