xref: /csrg-svn/lib/libm/common/tan.c (revision 42653)
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[] = "@(#)tan.c	5.4 (Berkeley) 06/01/90";
15 #endif /* not lint */
16 
17 #include "trig.h"
18 double
19 tan(x)
20 double x;
21 {
22 	double a,z,ss,cc,c;
23 	int k;
24 
25 	if(!finite(x))		/* tan(NaN) and tan(INF) must be NaN */
26 		return x-x;
27 	x = drem(x,PI);			/* reduce x into [-PI/2, PI/2] */
28 	a = copysign(x,one);		/* ... = abs(x) */
29 	if (a >= PIo4) {
30 		k = 1;
31 		x = copysign(PIo2-a,x);
32 	}
33 	else {
34 		k = 0;
35 		if (a < small) {
36 			big+a;
37 			return x;
38 		}
39 	}
40 	z = x*x;
41 	cc = cos__C(z);
42 	ss = sin__S(z);
43 	z *= half;			/* Next get c = cos(x) accurately */
44 	c = (z >= thresh ? half-((z-half)-cc) : one-(z-cc));
45 	if (k == 0)
46 		return x+(x*(z-(cc-ss)))/c;	/* ... sin/cos */
47 #ifdef national
48 	else if (x == zero)
49 		return copysign(fmax,x);	/* no inf on 32k */
50 #endif	/* national */
51 	else
52 		return c/(x+x*ss);		/* ... cos/sin */
53 }
54