xref: /original-bsd/lib/libm/common/sincos.c (revision 540a81df)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  * All recipients should regard themselves as participants in an ongoing
8  * research project and hence should feel obligated to report their
9  * experiences (good or bad) with these elementary function codes, using
10  * the sendbug(8) program, to the authors.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)sincos.c	5.4 (Berkeley) 06/01/90";
15 #endif /* not lint */
16 
17 #include "trig.h"
18 double
19 sin(x)
20 double x;
21 {
22 	double a,c,z;
23 
24         if(!finite(x))		/* sin(NaN) and sin(INF) must be NaN */
25 		return x-x;
26 	x=drem(x,PI2);		/* reduce x into [-PI,PI] */
27 	a=copysign(x,one);
28 	if (a >= PIo4) {
29 		if(a >= PI3o4)		/* ... in [3PI/4,PI] */
30 			x = copysign((a = PI-a),x);
31 		else {			/* ... in [PI/4,3PI/4]  */
32 			a = PIo2-a;		/* rtn. sign(x)*C(PI/2-|x|) */
33 			z = a*a;
34 			c = cos__C(z);
35 			z *= half;
36 			a = (z >= thresh ? half-((z-half)-c) : one-(z-c));
37 			return copysign(a,x);
38 		}
39 	}
40 
41 	if (a < small) {		/* rtn. S(x) */
42 		big+a;
43 		return x;
44 	}
45 	return x+x*sin__S(x*x);
46 }
47 
48 double
49 cos(x)
50 double x;
51 {
52 	double a,c,z,s = 1.0;
53 
54 	if(!finite(x))		/* cos(NaN) and cos(INF) must be NaN */
55 		return x-x;
56 	x=drem(x,PI2);		/* reduce x into [-PI,PI] */
57 	a=copysign(x,one);
58 	if (a >= PIo4) {
59 		if (a >= PI3o4) {	/* ... in [3PI/4,PI] */
60 			a = PI-a;
61 			s = negone;
62 		}
63 		else {			/* ... in [PI/4,3PI/4] */
64 			a = PIo2-a;
65 			return a+a*sin__S(a*a);	/* rtn. S(PI/2-|x|) */
66 		}
67 	}
68 	if (a < small) {
69 		big+a;
70 		return s;		/* rtn. s*C(a) */
71 	}
72 	z = a*a;
73 	c = cos__C(z);
74 	z *= half;
75 	a = (z >= thresh ? half-((z-half)-c) : one-(z-c));
76 	return copysign(a,s);
77 }
78