1 
2 /* @(#)s_tan.c 5.1 93/09/24 */
3 /*
4  * ====================================================
5  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6  *
7  * Developed at SunPro, a Sun Microsystems, Inc. business.
8  * Permission to use, copy, modify, and distribute this
9  * software is freely granted, provided that this notice
10  * is preserved.
11  * ====================================================
12  */
13 
14 /*
15 
16 FUNCTION
17         <<tan>>, <<tanf>>---tangent
18 
19 INDEX
20 tan
21 INDEX
22 tanf
23 
24 SYNOPSIS
25         #include <math.h>
26         double tan(double <[x]>);
27         float tanf(float <[x]>);
28 
29 DESCRIPTION
30 <<tan>> computes the tangent of the argument <[x]>.
31 Angles are specified in radians.
32 
33 <<tanf>> is identical, save that it takes and returns <<float>> values.
34 
35 RETURNS
36 The tangent of <[x]> is returned.
37 
38 PORTABILITY
39 <<tan>> is ANSI. <<tanf>> is an extension.
40 */
41 
42 /* tan(x)
43  * Return tangent function of x.
44  *
45  * kernel function:
46  *	__kernel_tan		... tangent function on [-pi/4,pi/4]
47  *	__rem_pio2	... argument reduction routine
48  *
49  * Method.
50  *      Let S,C and T denote the sin, cos and tan respectively on
51  *	[-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
52  *	in [-pi/4 , +pi/4], and let n = k mod 4.
53  *	We have
54  *
55  *          n        sin(x)      cos(x)        tan(x)
56  *     ----------------------------------------------------------
57  *	    0	       S	   C		 T
58  *	    1	       C	  -S		-1/T
59  *	    2	      -S	  -C		 T
60  *	    3	      -C	   S		-1/T
61  *     ----------------------------------------------------------
62  *
63  * Special cases:
64  *      Let trig be any of sin, cos, or tan.
65  *      trig(+-INF)  is NaN, with signals;
66  *      trig(NaN)    is that NaN;
67  *
68  * Accuracy:
69  *	TRIG(x) returns trig(x) nearly rounded
70  */
71 
72 #include "fdlibm.h"
73 
74 #ifdef _NEED_FLOAT64
75 
76 __float64
tan64(__float64 x)77 tan64(__float64 x)
78 {
79     __float64 y[2], z = _F_64(0.0);
80     __int32_t n, ix;
81 
82     /* High word of x. */
83     GET_HIGH_WORD(ix, x);
84 
85     /* |x| ~< pi/4 */
86     ix &= 0x7fffffff;
87     if (ix <= 0x3fe921fb)
88         return __kernel_tan(x, z, 1);
89 
90     /* tan(Inf or NaN) is NaN */
91     else if (ix >= 0x7ff00000)
92         return __math_invalid(x); /* NaN */
93 
94     /* argument reduction needed */
95     else {
96         n = __rem_pio2(x, y);
97         return __kernel_tan(y[0], y[1], 1 - ((n & 1) << 1)); /*   1 -- n even
98 							-1 -- n odd */
99     }
100 }
101 
102 _MATH_ALIAS_d_d(tan)
103 
104 #endif /* _NEED_FLOAT64 */
105