Remove K&R cruft.
[gnulib.git] / lib / tanl.c
1 /* s_tanl.c -- long double version of s_tan.c.
2  * Conversion to IEEE quad long double by Jakub Jelinek, jj@ultra.linux.cz.
3  */
4
5 /* @(#)s_tan.c 5.1 93/09/24 */
6 /*
7  * ====================================================
8  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
9  *
10  * Developed at SunPro, a Sun Microsystems, Inc. business.
11  * Permission to use, copy, modify, and distribute this
12  * software is freely granted, provided that this notice
13  * is preserved.
14  * ====================================================
15  */
16
17 /* tanl(x)
18  * Return tangent function of x.
19  *
20  * kernel function:
21  *      __kernel_tanl           ... tangent function on [-pi/4,pi/4]
22  *      __ieee754_rem_pio2l     ... argument reduction routine
23  *
24  * Method.
25  *      Let S,C and T denote the sin, cos and tan respectively on
26  *      [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
27  *      in [-pi/4 , +pi/4], and let n = k mod 4.
28  *      We have
29  *
30  *          n        sin(x)      cos(x)        tan(x)
31  *     ----------------------------------------------------------
32  *          0          S           C             T
33  *          1          C          -S            -1/T
34  *          2         -S          -C             T
35  *          3         -C           S            -1/T
36  *     ----------------------------------------------------------
37  *
38  * Special cases:
39  *      Let trig be any of sin, cos, or tan.
40  *      trig(+-INF)  is NaN, with signals;
41  *      trig(NaN)    is that NaN;
42  *
43  * Accuracy:
44  *      TRIG(x) returns trig(x) nearly rounded
45  */
46
47 #include <math.h>
48
49 #include "mathl.h"
50
51 #include "trigl.h"
52 #ifdef HAVE_SINL
53 #ifdef HAVE_COSL
54 #include "trigl.c"
55 #endif
56 #endif
57
58 /*
59  * ====================================================
60  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
61  *
62  * Developed at SunPro, a Sun Microsystems, Inc. business.
63  * Permission to use, copy, modify, and distribute this
64  * software is freely granted, provided that this notice
65  * is preserved.
66  * ====================================================
67  */
68
69 /*
70   Long double expansions contributed by
71   Stephen L. Moshier <moshier@na-net.ornl.gov>
72 */
73
74 /* __kernel_tanl( x, y, k )
75  * kernel tan function on [-pi/4, pi/4], pi/4 ~ 0.7854
76  * Input x is assumed to be bounded by ~pi/4 in magnitude.
77  * Input y is the tail of x.
78  * Input k indicates whether tan (if k=1) or
79  * -1/tan (if k= -1) is returned.
80  *
81  * Algorithm
82  *      1. Since tan(-x) = -tan(x), we need only to consider positive x.
83  *      2. if x < 2^-57, return x with inexact if x!=0.
84  *      3. tan(x) is approximated by a rational form x + x^3 / 3 + x^5 R(x^2)
85  *          on [0,0.67433].
86  *
87  *         Note: tan(x+y) = tan(x) + tan'(x)*y
88  *                        ~ tan(x) + (1+x*x)*y
89  *         Therefore, for better accuracy in computing tan(x+y), let
90  *              r = x^3 * R(x^2)
91  *         then
92  *              tan(x+y) = x + (x^3 / 3 + (x^2 *(r+y)+y))
93  *
94  *      4. For x in [0.67433,pi/4],  let y = pi/4 - x, then
95  *              tan(x) = tan(pi/4-y) = (1-tan(y))/(1+tan(y))
96  *                     = 1 - 2*(tan(y) - (tan(y)^2)/(1+tan(y)))
97  */
98
99
100 static const long double
101   pio4hi = 7.8539816339744830961566084581987569936977E-1L,
102   pio4lo = 2.1679525325309452561992610065108379921906E-35L,
103
104   /* tan x = x + x^3 / 3 + x^5 T(x^2)/U(x^2)
105      0 <= x <= 0.6743316650390625
106      Peak relative error 8.0e-36  */
107  TH =  3.333333333333333333333333333333333333333E-1L,
108  T0 = -1.813014711743583437742363284336855889393E7L,
109  T1 =  1.320767960008972224312740075083259247618E6L,
110  T2 = -2.626775478255838182468651821863299023956E4L,
111  T3 =  1.764573356488504935415411383687150199315E2L,
112  T4 = -3.333267763822178690794678978979803526092E-1L,
113
114  U0 = -1.359761033807687578306772463253710042010E8L,
115  U1 =  6.494370630656893175666729313065113194784E7L,
116  U2 = -4.180787672237927475505536849168729386782E6L,
117  U3 =  8.031643765106170040139966622980914621521E4L,
118  U4 = -5.323131271912475695157127875560667378597E2L;
119   /* 1.000000000000000000000000000000000000000E0 */
120
121
122 long double
123 kernel_tanl (long double x, long double y, int iy)
124 {
125   long double z, r, v, w, s, u, u1;
126   int flag, sign;
127
128         sign = 1;
129       if (x < 0)
130         {
131           x = -x;
132           y = -y;
133           sign = -1;
134         }
135
136   if (x < 0.000000000000000006938893903907228377647697925567626953125L) /* x < 2**-57 */
137     {
138       if ((int) x == 0)
139         {                       /* generate inexact */
140           if (iy == -1 && x == 0.0)
141             return 1.0L / fabs (x);
142           else
143             return (iy == 1) ? x : -1.0L / x;
144         }
145     }
146   if (x >= 0.6743316650390625) /* |x| >= 0.6743316650390625 */
147     {
148       flag = 1;
149
150       z = pio4hi - x;
151       w = pio4lo - y;
152       x = z + w;
153       y = 0.0;
154     }
155   z = x * x;
156   r = T0 + z * (T1 + z * (T2 + z * (T3 + z * T4)));
157   v = U0 + z * (U1 + z * (U2 + z * (U3 + z * (U4 + z))));
158   r = r / v;
159
160   s = z * x;
161   r = y + z * (s * r + y);
162   r += TH * s;
163   w = x + r;
164   if (flag)
165     {
166       v = (long double) iy;
167       w = (v - 2.0 * (x - (w * w / (w + v) - r)));
168       if (sign < 0)
169         w = -w;
170       return w;
171     }
172   if (iy == 1)
173     return w;
174   else
175     {                           /* if allow error up to 2 ulp,
176                                    simply return -1.0/(x+r) here */
177       /*  compute -1.0/(x+r) accurately */
178       u1 = (double) w;
179       v = r - (u1 - x);
180       z = -1.0 / w;
181       u = (double) z;
182       s = 1.0 + u * u1;
183       return u + z * (s + u * v);
184     }
185 }
186
187 long double
188 tanl (long double x)
189 {
190   long double y[2], z = 0.0L;
191   int n;
192
193   /* |x| ~< pi/4 */
194   if (x >= -0.7853981633974483096156608458198757210492 &&
195       x <= 0.7853981633974483096156608458198757210492)
196     return kernel_tanl (x, z, 1);
197
198   /* tanl(Inf or NaN) is NaN, tanl(0) is 0 */
199   else if (x + x == x || x != x)
200     return x - x;               /* NaN */
201
202   /* argument reduction needed */
203   else
204     {
205       n = ieee754_rem_pio2l (x, y);
206       /* 1 -- n even, -1 -- n odd */
207       return kernel_tanl (y[0], y[1], 1 - ((n & 1) << 1));
208     }
209 }
210
211 #if 0
212 int
213 main (void)
214 {
215   printf ("%.16Lg\n", tanl(0.7853981633974483096156608458198757210492));
216   printf ("%.16Lg\n", tanl(-0.7853981633974483096156608458198757210492));
217   printf ("%.16Lg\n", tanl(0.7853981633974483096156608458198757210492 *3));
218   printf ("%.16Lg\n", tanl(-0.7853981633974483096156608458198757210492 *31));
219   printf ("%.16Lg\n", tanl(0.7853981633974483096156608458198757210492 / 2));
220   printf ("%.16Lg\n", tanl(0.7853981633974483096156608458198757210492 * 3/2));
221   printf ("%.16Lg\n", tanl(0.7853981633974483096156608458198757210492 * 5/2));
222 }
223 #endif