1
0
mirror of https://github.com/golang/go synced 2024-10-05 08:21:22 -06:00
go/src/lib/math/sin.go

68 lines
1.3 KiB
Go
Raw Normal View History

2008-03-28 14:56:47 -06:00
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package math
2008-03-28 14:56:47 -06:00
/*
Coefficients are #3370 from Hart & Cheney (18.80D).
*/
2008-03-28 14:56:47 -06:00
const
(
p0 = .1357884097877375669092680e8;
p1 = -.4942908100902844161158627e7;
p2 = .4401030535375266501944918e6;
p3 = -.1384727249982452873054457e5;
p4 = .1459688406665768722226959e3;
q0 = .8644558652922534429915149e7;
q1 = .4081792252343299749395779e6;
q2 = .9463096101538208180571257e4;
q3 = .1326534908786136358911494e3;
2008-03-28 14:56:47 -06:00
piu2 = .6366197723675813430755350e0; // 2/pi
)
func Sinus(arg float64, quad int) float64 {
x := arg;
2008-03-28 14:56:47 -06:00
if(x < 0) {
x = -x;
quad = quad+2;
}
x = x * piu2; /* underflow? */
var y float64;
2008-03-28 14:56:47 -06:00
if x > 32764 {
var e float64;
e, y = sys.modf(x);
2008-07-08 21:48:41 -06:00
e = e + float64(quad);
temp1, f := sys.modf(0.25*e);
2008-03-28 14:56:47 -06:00
quad = int(e - 4*f);
} else {
k := int32(x);
2008-07-08 21:48:41 -06:00
y = x - float64(k);
quad = (quad + int(k)) & 3;
2008-03-28 14:56:47 -06:00
}
if quad&1 != 0 {
y = 1-y;
}
if quad > 1 {
y = -y;
}
ysq := y*y;
temp1 := ((((p4*ysq+p3)*ysq+p2)*ysq+p1)*ysq+p0)*y;
temp2 := ((((ysq+q3)*ysq+q2)*ysq+q1)*ysq+q0);
2008-03-28 14:56:47 -06:00
return temp1/temp2;
}
export func Cos(arg float64) float64 {
2008-03-28 14:56:47 -06:00
if arg < 0 {
arg = -arg;
}
return Sinus(arg, 1);
2008-03-28 14:56:47 -06:00
}
export func Sin(arg float64) float64 {
return Sinus(arg, 0);
2008-03-28 14:56:47 -06:00
}