
<font color="#0000ff">/*</font><font color="#0000ff">  tancotf.c: Computes tan or cot of a 32-bit float as outlined in [1]</font>

<font color="#0000ff">    Copyright (C) 2001, 2002  Jesus Calvino-Fraga, jesusc@ieee.org </font>

<font color="#0000ff">    This library is free software; you can redistribute it and/or</font>
<font color="#0000ff">    modify it under the terms of the GNU Lesser General Public</font>
<font color="#0000ff">    License as published by the Free Software Foundation; either</font>
<font color="#0000ff">    version 2.1 of the License, or (at your option) any later version.</font>

<font color="#0000ff">    This library is distributed in the hope that it will be useful,</font>
<font color="#0000ff">    but WITHOUT ANY WARRANTY; without even the implied warranty of</font>
<font color="#0000ff">    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU</font>
<font color="#0000ff">    Lesser General Public License for more details.</font>

<font color="#0000ff">    You should have received a copy of the GNU Lesser General Public</font>
<font color="#0000ff">    License along with this library; if not, write to the Free Software</font>
<font color="#0000ff">    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA */</font>

<font color="#0000ff">/*</font><font color="#0000ff"> [1] William James Cody and W.  M.  Waite.  _Software manual for the</font>
<font color="#0000ff">   elementary functions_, Englewood Cliffs, N.J.:Prentice-Hall, 1980. */</font>

<font color="#0000ff">/*</font><font color="#0000ff"> Version 1.0 - Initial release */</font>

<font color="#a020f0">#include </font><font color="#ff00ff">&lt;math.h&gt;</font>
<font color="#a020f0">#include </font><font color="#ff00ff">&lt;errno.h&gt;</font>

<font color="#a020f0">#define P0  </font><font color="#ff00ff">0.100000000E+1</font>
<font color="#a020f0">#define P1 -</font><font color="#ff00ff">0.958017723E-1</font>
<font color="#a020f0">#define Q0  </font><font color="#ff00ff">0.100000000E+1</font>
<font color="#a020f0">#define Q1 -</font><font color="#ff00ff">0.429135777E+0</font>
<font color="#a020f0">#define Q2  </font><font color="#ff00ff">0.971685835E-2</font>

<font color="#a020f0">#define C1  </font><font color="#ff00ff">1.5703125</font>
<font color="#a020f0">#define C2  </font><font color="#ff00ff">4.83826794897E-4</font>

<font color="#a020f0">#define P(f,g) (P1*g*f+f)</font>
<font color="#a020f0">#define Q(g) ((Q2*g+Q1)*g+Q0)</font>

<font color="#0000ff">//A reasonable choice for YMAX is the integer part of B**(t/2)*PI/2:</font>
<font color="#a020f0">#define YMAX </font><font color="#ff00ff">6433.0</font>

<font color="#2e8b57"><b>float</b></font> tancotf(<font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>float</b></font> x, <font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>int</b></font> iscotan)
{
    <font color="#2e8b57"><b>float</b></font> f, g, xn, xnum, xden;
    <font color="#2e8b57"><b>int</b></font> n;

    <font color="#804040"><b>if</b></font> (fabsf(x) &gt; YMAX)
    {
        errno = <font color="#ff00ff">ERANGE</font>;
        <font color="#804040"><b>return</b></font> <font color="#ff00ff">0.0</font>;
    }

    <font color="#0000ff">/*</font><font color="#0000ff">Round x*2*PI to the nearest integer*/</font>
    n=(x*TWO_O_PI+(x&gt;<font color="#ff00ff">0.0</font>?<font color="#ff00ff">0.5</font>:-<font color="#ff00ff">0.5</font>)); <font color="#0000ff">/*</font><font color="#0000ff">works for +-x*/</font>
    xn=n;

    xnum=(<font color="#2e8b57"><b>int</b></font>)x;
    xden=x-xnum;
    f=((xnum-xn*C1)+xden)-xn*C2;

    <font color="#804040"><b>if</b></font> (fabsf(f) &lt; EPS)
    {
        xnum = f;
        xden = <font color="#ff00ff">1.0</font>;
    }
    <font color="#804040"><b>else</b></font>
    {
        g = f*f;
        xnum = P(f,g);
        xden = Q(g);
    }

    <font color="#804040"><b>if</b></font>(n&amp;<font color="#ff00ff">1</font>)
    <font color="#0000ff">//xn is odd</font>
    {
        <font color="#804040"><b>if</b></font>(iscotan) <font color="#804040"><b>return</b></font> (-xnum/xden);
               <font color="#804040"><b>else</b></font> <font color="#804040"><b>return</b></font> (-xden/xnum);
    }
    <font color="#804040"><b>else</b></font>
    {
        <font color="#804040"><b>if</b></font>(iscotan) <font color="#804040"><b>return</b></font> (xden/xnum);
               <font color="#804040"><b>else</b></font> <font color="#804040"><b>return</b></font> (xnum/xden);
    }
}

