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.
5 /* @(#)s_tan.c 5.1 93/09/24 */
7 * ====================================================
8 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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
14 * ====================================================
23 * Return tangent function of x.
26 * __kernel_tanl ... tangent function on [-pi/4,pi/4]
27 * __ieee754_rem_pio2l ... argument reduction routine
30 * Let S,C and T denote the sin, cos and tan respectively on
31 * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
32 * in [-pi/4 , +pi/4], and let n = k mod 4.
35 * n sin(x) cos(x) tan(x)
36 * ----------------------------------------------------------
41 * ----------------------------------------------------------
44 * Let trig be any of sin, cos, or tan.
45 * trig(+-INF) is NaN, with signals;
46 * trig(NaN) is that NaN;
49 * TRIG(x) returns trig(x) nearly rounded
60 * ====================================================
61 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
63 * Developed at SunPro, a Sun Microsystems, Inc. business.
64 * Permission to use, copy, modify, and distribute this
65 * software is freely granted, provided that this notice
67 * ====================================================
71 Long double expansions contributed by
72 Stephen L. Moshier <moshier@na-net.ornl.gov>
75 /* __kernel_tanl( x, y, k )
76 * kernel tan function on [-pi/4, pi/4], pi/4 ~ 0.7854
77 * Input x is assumed to be bounded by ~pi/4 in magnitude.
78 * Input y is the tail of x.
79 * Input k indicates whether tan (if k=1) or
80 * -1/tan (if k= -1) is returned.
83 * 1. Since tan(-x) = -tan(x), we need only to consider positive x.
84 * 2. if x < 2^-57, return x with inexact if x!=0.
85 * 3. tan(x) is approximated by a rational form x + x^3 / 3 + x^5 R(x^2)
88 * Note: tan(x+y) = tan(x) + tan'(x)*y
89 * ~ tan(x) + (1+x*x)*y
90 * Therefore, for better accuracy in computing tan(x+y), let
93 * tan(x+y) = x + (x^3 / 3 + (x^2 *(r+y)+y))
95 * 4. For x in [0.67433,pi/4], let y = pi/4 - x, then
96 * tan(x) = tan(pi/4-y) = (1-tan(y))/(1+tan(y))
97 * = 1 - 2*(tan(y) - (tan(y)^2)/(1+tan(y)))
101 static const long double
102 pio4hi = 7.8539816339744830961566084581987569936977E-1L,
103 pio4lo = 2.1679525325309452561992610065108379921906E-35L,
105 /* tan x = x + x^3 / 3 + x^5 T(x^2)/U(x^2)
106 0 <= x <= 0.6743316650390625
107 Peak relative error 8.0e-36 */
108 TH = 3.333333333333333333333333333333333333333E-1L,
109 T0 = -1.813014711743583437742363284336855889393E7L,
110 T1 = 1.320767960008972224312740075083259247618E6L,
111 T2 = -2.626775478255838182468651821863299023956E4L,
112 T3 = 1.764573356488504935415411383687150199315E2L,
113 T4 = -3.333267763822178690794678978979803526092E-1L,
115 U0 = -1.359761033807687578306772463253710042010E8L,
116 U1 = 6.494370630656893175666729313065113194784E7L,
117 U2 = -4.180787672237927475505536849168729386782E6L,
118 U3 = 8.031643765106170040139966622980914621521E4L,
119 U4 = -5.323131271912475695157127875560667378597E2L;
120 /* 1.000000000000000000000000000000000000000E0 */
124 kernel_tanl (long double x, long double y, int iy)
126 long double z, r, v, w, s, u, u1;
127 int invert = 0, sign;
137 if (x < 0.000000000000000006938893903907228377647697925567626953125L) /* x < 2**-57 */
140 { /* generate inexact */
141 if (iy == -1 && x == 0.0)
142 return 1.0L / fabs (x);
144 return (iy == 1) ? x : -1.0L / x;
147 if (x >= 0.6743316650390625) /* |x| >= 0.6743316650390625 */
157 r = T0 + z * (T1 + z * (T2 + z * (T3 + z * T4)));
158 v = U0 + z * (U1 + z * (U2 + z * (U3 + z * (U4 + z))));
162 r = y + z * (s * r + y);
167 v = (long double) iy;
168 w = (v - 2.0 * (x - (w * w / (w + v) - r)));
176 { /* if allow error up to 2 ulp,
177 simply return -1.0/(x+r) here */
178 /* compute -1.0/(x+r) accurately */
184 return u + z * (s + u * v);
191 long double y[2], z = 0.0L;
194 /* tanl(NaN) is NaN */
199 if (x >= -0.7853981633974483096156608458198757210492 &&
200 x <= 0.7853981633974483096156608458198757210492)
201 return kernel_tanl (x, z, 1);
203 /* tanl(Inf) is NaN, tanl(0) is 0 */
205 return x - x; /* NaN */
207 /* argument reduction needed */
210 n = ieee754_rem_pio2l (x, y);
211 /* 1 -- n even, -1 -- n odd */
212 return kernel_tanl (y[0], y[1], 1 - ((n & 1) << 1));
220 printf ("%.16Lg\n", tanl(0.7853981633974483096156608458198757210492));
221 printf ("%.16Lg\n", tanl(-0.7853981633974483096156608458198757210492));
222 printf ("%.16Lg\n", tanl(0.7853981633974483096156608458198757210492 *3));
223 printf ("%.16Lg\n", tanl(-0.7853981633974483096156608458198757210492 *31));
224 printf ("%.16Lg\n", tanl(0.7853981633974483096156608458198757210492 / 2));
225 printf ("%.16Lg\n", tanl(0.7853981633974483096156608458198757210492 * 3/2));
226 printf ("%.16Lg\n", tanl(0.7853981633974483096156608458198757210492 * 5/2));