Imported Upstream version 2.9.0
[debian/cc1111] / device / lib / pic / libm / tancotf.c
1 /*  tancotf.c: Computes tan or cot of a 32-bit float as outlined in [1]
2
3     Copyright (C) 2001, 2002  Jesus Calvino-Fraga, jesusc@ieee.org 
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA */
18
19 /* [1] William James Cody and W.  M.  Waite.  _Software manual for the
20    elementary functions_, Englewood Cliffs, N.J.:Prentice-Hall, 1980. */
21
22 /* Version 1.0 - Initial release */
23
24 /*
25 ** $Id: tancotf.c 4776 2007-04-29 13:15:51Z borutr $
26 */
27
28 #include <math.h>
29 #include <errno.h>
30
31 #define P0  0.100000000E+1
32 #define P1 -0.958017723E-1
33 #define Q0  0.100000000E+1
34 #define Q1 -0.429135777E+0
35 #define Q2  0.971685835E-2
36
37 #define C1  1.5703125
38 #define C2  4.83826794897E-4
39
40 #define P(f,g) (P1*g*f+f)
41 #define Q(g) ((Q2*g+Q1)*g+Q0)
42
43 //A reasonable choice for YMAX is the integer part of B**(t/2)*PI/2:
44 #define YMAX 6433.0
45
46 float tancotf(const float x, const int iscotan)
47 {
48     float f, g, xn, xnum, xden;
49     int n;
50
51     if (fabsf(x) > YMAX)
52     {
53         errno = ERANGE;
54         return 0.0;
55     }
56
57     /*Round x*2*PI to the nearest integer*/
58     n=(x*TWO_O_PI+(x>0.0?0.5:-0.5)); /*works for +-x*/
59     xn=n;
60
61     xnum=(int)x;
62     xden=x-xnum;
63     f=((xnum-xn*C1)+xden)-xn*C2;
64
65     if (fabsf(f) < EPS)
66     {
67         xnum = f;
68         xden = 1.0;
69     }
70     else
71     {
72         g = f*f;
73         xnum = P(f,g);
74         xden = Q(g);
75     }
76
77     if(n&1)
78     //xn is odd
79     {
80         if(iscotan) return (-xnum/xden);
81                else return (-xden/xnum);
82     }
83     else
84     {
85         if(iscotan) return (xden/xnum);
86                else return (xnum/xden);
87     }
88 }
89