xref: /original-bsd/lib/libm/common/sincos.c (revision a141c157)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * All recipients should regard themselves as participants in an ongoing
18  * research project and hence should feel obligated to report their
19  * experiences (good or bad) with these elementary function codes, using
20  * the sendbug(8) program, to the authors.
21  */
22 
23 #ifndef lint
24 static char sccsid[] = "@(#)sincos.c	5.3 (Berkeley) 06/30/88";
25 #endif /* not lint */
26 
27 #include "trig.h"
28 double
29 sin(x)
30 double x;
31 {
32 	double a,c,z;
33 
34         if(!finite(x))		/* sin(NaN) and sin(INF) must be NaN */
35 		return x-x;
36 	x=drem(x,PI2);		/* reduce x into [-PI,PI] */
37 	a=copysign(x,one);
38 	if (a >= PIo4) {
39 		if(a >= PI3o4)		/* ... in [3PI/4,PI] */
40 			x = copysign((a = PI-a),x);
41 		else {			/* ... in [PI/4,3PI/4]  */
42 			a = PIo2-a;		/* rtn. sign(x)*C(PI/2-|x|) */
43 			z = a*a;
44 			c = cos__C(z);
45 			z *= half;
46 			a = (z >= thresh ? half-((z-half)-c) : one-(z-c));
47 			return copysign(a,x);
48 		}
49 	}
50 
51 	if (a < small) {		/* rtn. S(x) */
52 		big+a;
53 		return x;
54 	}
55 	return x+x*sin__S(x*x);
56 }
57 
58 double
59 cos(x)
60 double x;
61 {
62 	double a,c,z,s = 1.0;
63 
64 	if(!finite(x))		/* cos(NaN) and cos(INF) must be NaN */
65 		return x-x;
66 	x=drem(x,PI2);		/* reduce x into [-PI,PI] */
67 	a=copysign(x,one);
68 	if (a >= PIo4) {
69 		if (a >= PI3o4) {	/* ... in [3PI/4,PI] */
70 			a = PI-a;
71 			s = negone;
72 		}
73 		else {			/* ... in [PI/4,3PI/4] */
74 			a = PIo2-a;
75 			return a+a*sin__S(a*a);	/* rtn. S(PI/2-|x|) */
76 		}
77 	}
78 	if (a < small) {
79 		big+a;
80 		return s;		/* rtn. s*C(a) */
81 	}
82 	z = a*a;
83 	c = cos__C(z);
84 	z *= half;
85 	a = (z >= thresh ? half-((z-half)-c) : one-(z-c));
86 	return copysign(a,s);
87 }
88