xref: /original-bsd/lib/libm/common/sincos.c (revision 6884d44a)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)sincos.c	5.5 (Berkeley) 10/09/90";
10 #endif /* not lint */
11 
12 #include "trig.h"
13 double
14 sin(x)
15 double x;
16 {
17 	double a,c,z;
18 
19         if(!finite(x))		/* sin(NaN) and sin(INF) must be NaN */
20 		return x-x;
21 	x=drem(x,PI2);		/* reduce x into [-PI,PI] */
22 	a=copysign(x,one);
23 	if (a >= PIo4) {
24 		if(a >= PI3o4)		/* ... in [3PI/4,PI] */
25 			x = copysign((a = PI-a),x);
26 		else {			/* ... in [PI/4,3PI/4]  */
27 			a = PIo2-a;		/* rtn. sign(x)*C(PI/2-|x|) */
28 			z = a*a;
29 			c = cos__C(z);
30 			z *= half;
31 			a = (z >= thresh ? half-((z-half)-c) : one-(z-c));
32 			return copysign(a,x);
33 		}
34 	}
35 
36 	if (a < small) {		/* rtn. S(x) */
37 		big+a;
38 		return x;
39 	}
40 	return x+x*sin__S(x*x);
41 }
42 
43 double
44 cos(x)
45 double x;
46 {
47 	double a,c,z,s = 1.0;
48 
49 	if(!finite(x))		/* cos(NaN) and cos(INF) must be NaN */
50 		return x-x;
51 	x=drem(x,PI2);		/* reduce x into [-PI,PI] */
52 	a=copysign(x,one);
53 	if (a >= PIo4) {
54 		if (a >= PI3o4) {	/* ... in [3PI/4,PI] */
55 			a = PI-a;
56 			s = negone;
57 		}
58 		else {			/* ... in [PI/4,3PI/4] */
59 			a = PIo2-a;
60 			return a+a*sin__S(a*a);	/* rtn. S(PI/2-|x|) */
61 		}
62 	}
63 	if (a < small) {
64 		big+a;
65 		return s;		/* rtn. s*C(a) */
66 	}
67 	z = a*a;
68 	c = cos__C(z);
69 	z *= half;
70 	a = (z >= thresh ? half-((z-half)-c) : one-(z-c));
71 	return copysign(a,s);
72 }
73