|
0
|
1 //=========================================================================== |
|
|
2 // |
|
|
3 // k_tan.c |
|
|
4 // |
|
|
5 // Part of the standard mathematical function library |
|
|
6 // |
|
|
7 //=========================================================================== |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
|
13 // compliance with the License. You may obtain a copy of the License at |
|
|
14 // http://sourceware.cygnus.com/ecos |
|
|
15 // |
|
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
18 // License for the specific language governing rights and limitations under |
|
|
19 // the License. |
|
|
20 // |
|
|
21 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
|
25 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved. |
|
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //=========================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author(s): jlarmour@cygnus.co.uk |
|
|
33 // Contributors: jlarmour@cygnus.co.uk |
|
|
34 // Date: 1998-02-13 |
|
|
35 // Purpose: |
|
|
36 // Description: |
|
|
37 // Usage: |
|
|
38 // |
|
|
39 //####DESCRIPTIONEND#### |
|
|
40 // |
|
|
41 //=========================================================================== |
|
|
42 |
|
|
43 // CONFIGURATION |
|
|
44 |
|
|
45 #include <pkgconf/libm.h> // Configuration header |
|
|
46 |
|
|
47 // Include the Math library? |
|
|
48 #ifdef CYGPKG_LIBM |
|
|
49 |
|
|
50 // Derived from code with the following copyright |
|
|
51 |
|
|
52 |
|
|
53 /* @(#)k_tan.c 1.3 95/01/18 */ |
|
|
54 /* |
|
|
55 * ==================================================== |
|
|
56 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
|
|
57 * |
|
|
58 * Developed at SunSoft, a Sun Microsystems, Inc. business. |
|
|
59 * Permission to use, copy, modify, and distribute this |
|
|
60 * software is freely granted, provided that this notice |
|
|
61 * is preserved. |
|
|
62 * ==================================================== |
|
|
63 */ |
|
|
64 |
|
|
65 /* __kernel_tan( x, y, k ) |
|
|
66 * kernel tan function on [-pi/4, pi/4], pi/4 ~ 0.7854 |
|
|
67 * Input x is assumed to be bounded by ~pi/4 in magnitude. |
|
|
68 * Input y is the tail of x. |
|
|
69 * Input k indicates whether tan (if k=1) or |
|
|
70 * -1/tan (if k= -1) is returned. |
|
|
71 * |
|
|
72 * Algorithm |
|
|
73 * 1. Since tan(-x) = -tan(x), we need only to consider positive x. |
|
|
74 * 2. if x < 2^-28 (hx<0x3e300000 0), return x with inexact if x!=0. |
|
|
75 * 3. tan(x) is approximated by a odd polynomial of degree 27 on |
|
|
76 * [0,0.67434] |
|
|
77 * 3 27 |
|
|
78 * tan(x) ~ x + T1*x + ... + T13*x |
|
|
79 * where |
|
|
80 * |
|
|
81 * |tan(x) 2 4 26 | -59.2 |
|
|
82 * |----- - (1+T1*x +T2*x +.... +T13*x )| <= 2 |
|
|
83 * | x | |
|
|
84 * |
|
|
85 * Note: tan(x+y) = tan(x) + tan'(x)*y |
|
|
86 * ~ tan(x) + (1+x*x)*y |
|
|
87 * Therefore, for better accuracy in computing tan(x+y), let |
|
|
88 * 3 2 2 2 2 |
|
|
89 * r = x *(T2+x *(T3+x *(...+x *(T12+x *T13)))) |
|
|
90 * then |
|
|
91 * 3 2 |
|
|
92 * tan(x+y) = x + (T1*x + (x *(r+y)+y)) |
|
|
93 * |
|
|
94 * 4. For x in [0.67434,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 #include "mathincl/fdlibm.h" |
|
|
100 static const double |
|
|
101 one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */ |
|
|
102 pio4 = 7.85398163397448278999e-01, /* 0x3FE921FB, 0x54442D18 */ |
|
|
103 pio4lo= 3.06161699786838301793e-17, /* 0x3C81A626, 0x33145C07 */ |
|
|
104 T[] = { |
|
|
105 3.33333333333334091986e-01, /* 0x3FD55555, 0x55555563 */ |
|
|
106 1.33333333333201242699e-01, /* 0x3FC11111, 0x1110FE7A */ |
|
|
107 5.39682539762260521377e-02, /* 0x3FABA1BA, 0x1BB341FE */ |
|
|
108 2.18694882948595424599e-02, /* 0x3F9664F4, 0x8406D637 */ |
|
|
109 8.86323982359930005737e-03, /* 0x3F8226E3, 0xE96E8493 */ |
|
|
110 3.59207910759131235356e-03, /* 0x3F6D6D22, 0xC9560328 */ |
|
|
111 1.45620945432529025516e-03, /* 0x3F57DBC8, 0xFEE08315 */ |
|
|
112 5.88041240820264096874e-04, /* 0x3F4344D8, 0xF2F26501 */ |
|
|
113 2.46463134818469906812e-04, /* 0x3F3026F7, 0x1A8D1068 */ |
|
|
114 7.81794442939557092300e-05, /* 0x3F147E88, 0xA03792A6 */ |
|
|
115 7.14072491382608190305e-05, /* 0x3F12B80F, 0x32F0A7E9 */ |
|
|
116 -1.85586374855275456654e-05, /* 0xBEF375CB, 0xDB605373 */ |
|
|
117 2.59073051863633712884e-05, /* 0x3EFB2A70, 0x74BF7AD4 */ |
|
|
118 }; |
|
|
119 |
|
|
120 double __kernel_tan(double x, double y, int iy) |
|
|
121 { |
|
|
122 double z,r,v,w,s; |
|
|
123 int ix,hx; |
|
|
124 hx = CYG_LIBM_HI(x); /* high word of x */ |
|
|
125 ix = hx&0x7fffffff; /* high word of |x| */ |
|
|
126 if(ix<0x3e300000) /* x < 2**-28 */ |
|
|
127 {if((int)x==0) { /* generate inexact */ |
|
|
128 if(((ix|CYG_LIBM_LO(x))|(iy+1))==0) return one/fabs(x); |
|
|
129 else return (iy==1)? x: -one/x; |
|
|
130 } |
|
|
131 } |
|
|
132 if(ix>=0x3FE59428) { /* |x|>=0.6744 */ |
|
|
133 if(hx<0) {x = -x; y = -y;} |
|
|
134 z = pio4-x; |
|
|
135 w = pio4lo-y; |
|
|
136 x = z+w; y = 0.0; |
|
|
137 } |
|
|
138 z = x*x; |
|
|
139 w = z*z; |
|
|
140 /* Break x^5*(T[1]+x^2*T[2]+...) into |
|
|
141 * x^5(T[1]+x^4*T[3]+...+x^20*T[11]) + |
|
|
142 * x^5(x^2*(T[2]+x^4*T[4]+...+x^22*[T12])) |
|
|
143 */ |
|
|
144 r = T[1]+w*(T[3]+w*(T[5]+w*(T[7]+w*(T[9]+w*T[11])))); |
|
|
145 v = z*(T[2]+w*(T[4]+w*(T[6]+w*(T[8]+w*(T[10]+w*T[12]))))); |
|
|
146 s = z*x; |
|
|
147 r = y + z*(s*(r+v)+y); |
|
|
148 r += T[0]*s; |
|
|
149 w = x+r; |
|
|
150 if(ix>=0x3FE59428) { |
|
|
151 v = (double)iy; |
|
|
152 return (double)(1-((hx>>30)&2))*(v-2.0*(x-(w*w/(w+v)-r))); |
|
|
153 } |
|
|
154 if(iy==1) return w; |
|
|
155 else { /* if allow error up to 2 ulp, |
|
|
156 simply return -1.0/(x+r) here */ |
|
|
157 /* compute -1.0/(x+r) accurately */ |
|
|
158 double a,t; |
|
|
159 z = w; |
|
|
160 CYG_LIBM_LO(z) = 0; |
|
|
161 v = r-(z - x); /* z+v = r+x */ |
|
|
162 t = a = -1.0/w; /* a = -1.0/w */ |
|
|
163 CYG_LIBM_LO(t) = 0; |
|
|
164 s = 1.0+t*z; |
|
|
165 return t+a*(s+t*v); |
|
|
166 } |
|
|
167 } |
|
|
168 |
|
|
169 #endif // ifdef CYGPKG_LIBM |
|
|
170 |
|
|
171 // EOF k_tan.c |