xref: /inferno-os/libmath/fdlibm/s_tan.c (revision d0e1d143ef6f03c75c008c7ec648859dd260cbab)
1 /* derived from /netlib/fdlibm */
2 
3 /* @(#)s_tan.c 1.3 95/01/18 */
4 /*
5  * ====================================================
6  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7  *
8  * Developed at SunSoft, a Sun Microsystems, Inc. business.
9  * Permission to use, copy, modify, and distribute this
10  * software is freely granted, provided that this notice
11  * is preserved.
12  * ====================================================
13  */
14 
15 /* tan(x)
16  * Return tangent function of x.
17  *
18  * kernel function:
19  *	__kernel_tan		... tangent function on [-pi/4,pi/4]
20  *	__ieee754_rem_pio2	... argument reduction routine
21  *
22  * Method.
23  *      Let S,C and T denote the sin, cos and tan respectively on
24  *	[-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
25  *	in [-pi/4 , +pi/4], and let n = k mod 4.
26  *	We have
27  *
28  *          n        sin(x)      cos(x)        tan(x)
29  *     ----------------------------------------------------------
30  *	    0	       S	   C		 T
31  *	    1	       C	  -S		-1/T
32  *	    2	      -S	  -C		 T
33  *	    3	      -C	   S		-1/T
34  *     ----------------------------------------------------------
35  *
36  * Special cases:
37  *      Let trig be any of sin, cos, or tan.
38  *      trig(+-INF)  is NaN, with signals;
39  *      trig(NaN)    is that NaN;
40  *
41  * Accuracy:
42  *	TRIG(x) returns trig(x) nearly rounded
43  */
44 
45 #include "fdlibm.h"
46 
47 	double tan(double x)
48 {
49 	double y[2],z=0.0;
50 	int n, ix;
51 
52     /* High word of x. */
53 	ix = __HI(x);
54 
55     /* |x| ~< pi/4 */
56 	ix &= 0x7fffffff;
57 	if(ix <= 0x3fe921fb) return __kernel_tan(x,z,1);
58 
59     /* tan(Inf or NaN) is NaN */
60 	else if (ix>=0x7ff00000) return x-x;		/* NaN */
61 
62     /* argument reduction needed */
63 	else {
64 	    n = __ieee754_rem_pio2(x,y);
65 	    return __kernel_tan(y[0],y[1],1-((n&1)<<1)); /*   1 -- n even
66 							-1 -- n odd */
67 	}
68 }
69