OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [branches/] [newlib/] [newlib/] [newlib/] [libm/] [mathfp/] [s_atangent.c] - Blame information for rev 1766

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 56 joel
 
2
/* @(#)z_atangent.c 1.0 98/08/13 */
3
/******************************************************************
4
 * The following routines are coded directly from the algorithms
5
 * and coefficients given in "Software Manual for the Elementary
6
 * Functions" by William J. Cody, Jr. and William Waite, Prentice
7
 * Hall, 1980.
8
 ******************************************************************/
9
 
10
/*
11
FUNCTION
12
        <<atan>>, <<atanf>>, <<atan2>>, <<atan2f>>, <<atangent>>, <<atangentf>>---arc tangent
13
 
14
INDEX
15
   atan2
16
INDEX
17
   atan2f
18
INDEX
19
   atan
20
INDEX
21
   atanf
22
 
23
ANSI_SYNOPSIS
24
        #include <math.h>
25
        double atan(double <[x]>);
26
        float atan(float <[x]>);
27
        double atan2(double <[y]>,double <[x]>);
28
        float atan2f(float <[y]>,float <[x]>);
29
 
30
TRAD_SYNOPSIS
31
        #include <math.h>
32
        double atan2(<[y]>,<[x]>);
33
        double <[y]>;
34
        double <[x]>;
35
 
36
        float atan2f(<[y]>,<[x]>);
37
        float <[y]>;
38
        float <[x]>;
39
 
40
        #include <math.h>
41
        double atan(<[x]>);
42
        double <[x]>;
43
 
44
        float atanf(<[x]>);
45
        float <[x]>;
46
 
47
DESCRIPTION
48
 
49
<<atan2>> computes the inverse tangent (arc tangent) of y / x.
50
 
51
<<atan2f>> is identical to <<atan2>>, save that it operates on <<floats>>.
52
 
53
<<atan>> computes the inverse tangent (arc tangent) of the input value.
54
 
55
<<atanf>> is identical to <<atan>>, save that it operates on <<floats>>.
56
 
57
RETURNS
58
@ifinfo
59
<<atan>> returns a value in radians, in the range of -pi/2 to pi/2.
60
<<atan2>> returns a value in radians, in the range of -pi/2 to pi/2.
61
@end ifinfo
62
@tex
63
<<atan>> returns a value in radians, in the range of $-\pi/2$ to $\pi/2$.
64
<<atan2>> returns a value in radians, in the range of $-\pi/2$ to $\pi/2$.
65
@end tex
66
 
67
PORTABILITY
68
<<atan>> is ANSI C.  <<atanf>> is an extension.
69
<<atan2>> is ANSI C.  <<atan2f>> is an extension.
70
 
71
*/
72
 
73
/******************************************************************
74
 * Arctangent
75
 *
76
 * Input:
77
 *   x - floating point value
78
 *
79
 * Output:
80
 *   arctangent of x
81
 *
82
 * Description:
83
 *   This routine calculates arctangents.
84
 *
85
 *****************************************************************/
86
#include <float.h>
87
#include "fdlibm.h"
88
#include "zmath.h"
89
 
90
#ifndef _DOUBLE_IS_32BITS
91
 
92
static const double ROOT3 = 1.73205080756887729353;
93
static const double a[] = { 0.0, 0.52359877559829887308, 1.57079632679489661923,
94
                     1.04719755119659774615 };
95
static const double q[] = { 0.41066306682575781263e+2,
96
                     0.86157349597130242515e+2,
97
                     0.59578436142597344465e+2,
98
                     0.15024001160028576121e+2 };
99
static const double p[] = { -0.13688768894191926929e+2,
100
                     -0.20505855195861651981e+2,
101
                     -0.84946240351320683534e+1,
102
                     -0.83758299368150059274 };
103
 
104
double
105
_DEFUN (atangent, (double, double, double, int),
106
        double x _AND
107
        double v _AND
108
        double u _AND
109
        int arctan2)
110
{
111
  double f, g, R, P, Q, A, res;
112
  int N;
113
  int branch = 0;
114
  int expv, expu;
115
 
116
  /* Preparation for calculating arctan2. */
117
  if (arctan2)
118
    {
119
      if (u == 0.0)
120
        if (v == 0.0)
121
          {
122
            errno = ERANGE;
123
            return (z_notanum.d);
124
          }
125
        else
126
          {
127
            branch = 1;
128
            res = __PI_OVER_TWO;
129
          }
130
 
131
      if (!branch)
132
        {
133
          /* Get the exponent values of the inputs. */
134
          g = frexp (v, &expv);
135
          g = frexp (u, &expu);
136
 
137
          /* See if a divide will overflow. */
138
          if ((expv - expu) > DBL_MAX_EXP + 1023)
139
            {
140
               branch = 1;
141
               res = __PI_OVER_TWO;
142
            }
143
 
144
          /* Also check for underflow. */
145
          else if ((expv - expu) < -DBL_MIN_EXP)
146
            {
147
               branch = 2;
148
               res = 0.0;
149
            }
150
         }
151
    }
152
 
153
  if (!branch)
154
    {
155
      if (arctan2)
156
        f = fabs (v / u);
157
      else
158
        f = fabs (x);
159
 
160
      if (f > 1.0)
161
        {
162
          f = 1.0 / f;
163
          N = 2;
164
        }
165
      else
166
        N = 0;
167
 
168
      if (f > (2.0 - ROOT3))
169
        {
170
          A = ROOT3 - 1.0;
171
          f = (((A * f - 0.5) - 0.5) + f) / (ROOT3 + f);
172
          N++;
173
        }
174
 
175
      /* Check for values that are too small. */
176
      if (-z_rooteps < f && f < z_rooteps)
177
        res = f;
178
 
179
      /* Calculate the Taylor series. */
180
      else
181
        {
182
          g = f * f;
183
          P = (((p[3] * g + p[2]) * g + p[1]) * g + p[0]) * g;
184
          Q = (((g + q[3]) * g + q[2]) * g + q[1]) * g + q[0];
185
          R = P / Q;
186
 
187
          res = f + f * R;
188
        }
189
 
190
      if (N > 1)
191
        res = -res;
192
 
193
      res += a[N];
194
    }
195
 
196
  if (arctan2 && branch)
197
    {
198
      if (u < 0.0 || branch == 2)
199
        res = __PI - res;
200
      if (v < 0.0 || branch == 1)
201
        res = -res;
202
    }
203
  else if (x < 0.0)
204
    {
205
      res = -res;
206
    }
207
 
208
  return (res);
209
}
210
 
211
#endif /* _DOUBLE_IS_32BITS */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.