|
@@ -6,7 +6,6 @@
|
|
|
*/
|
|
|
/* $Header$ */
|
|
|
|
|
|
-#include <errno.h>
|
|
|
#include <float.h>
|
|
|
#include <math.h>
|
|
|
#include "localmath.h"
|
|
@@ -14,91 +13,55 @@
|
|
|
double
|
|
|
atan(double x)
|
|
|
{
|
|
|
- /* The interval [0, infinity) is treated as follows:
|
|
|
- Define partition points Xi
|
|
|
- X0 = 0
|
|
|
- X1 = tan(pi/16)
|
|
|
- X2 = tan(3pi/16)
|
|
|
- X3 = tan(5pi/16)
|
|
|
- X4 = tan(7pi/16)
|
|
|
- X5 = infinity
|
|
|
- and evaluation nodes xi
|
|
|
- x2 = tan(2pi/16)
|
|
|
- x3 = tan(4pi/16)
|
|
|
- x4 = tan(6pi/16)
|
|
|
- x5 = infinity
|
|
|
- An argument x in [Xn-1, Xn] is now reduced to an argument
|
|
|
- t in [-X1, X1] by the following formulas:
|
|
|
-
|
|
|
- t = 1/xn - (1/(xn*xn) + 1)/((1/xn) + x)
|
|
|
-
|
|
|
- arctan(x) = arctan(xi) + arctan(t)
|
|
|
-
|
|
|
- For the interval [0, p/16] an approximation is used:
|
|
|
- arctan(x) = x * P(x*x)/Q(x*x)
|
|
|
+ /* Algorithm and coefficients from:
|
|
|
+ "Software manual for the elementary functions"
|
|
|
+ by W.J. Cody and W. Waite, Prentice-Hall, 1980
|
|
|
*/
|
|
|
- static struct precomputed {
|
|
|
- double X; /* partition point */
|
|
|
- double arctan; /* arctan of evaluation node */
|
|
|
- double one_o_x; /* 1 / xn */
|
|
|
- double one_o_xsq_p_1; /* 1 / (xn*xn) + 1 */
|
|
|
- } prec[5] = {
|
|
|
- { 0.19891236737965800691159762264467622,
|
|
|
- 0.0,
|
|
|
- 0.0, /* these don't matter */
|
|
|
- 0.0 } ,
|
|
|
- { 0.66817863791929891999775768652308076, /* tan(3pi/16) */
|
|
|
- M_PI_8,
|
|
|
- 2.41421356237309504880168872420969808,
|
|
|
- 6.82842712474619009760337744841939616 },
|
|
|
- { 1.49660576266548901760113513494247691, /* tan(5pi/16) */
|
|
|
- M_PI_4,
|
|
|
- 1.0,
|
|
|
- 2.0 },
|
|
|
- { 5.02733949212584810451497507106407238, /* tan(7pi/16) */
|
|
|
- M_3PI_8,
|
|
|
- 0.41421356237309504880168872420969808,
|
|
|
- 1.17157287525380998659662255158060384 },
|
|
|
- { DBL_MAX,
|
|
|
- M_PI_2,
|
|
|
- 0.0,
|
|
|
- 1.0 }};
|
|
|
|
|
|
- /* Hart & Cheney # 5037 */
|
|
|
-
|
|
|
- static double p[5] = {
|
|
|
- 0.7698297257888171026986294745e+03,
|
|
|
- 0.1557282793158363491416585283e+04,
|
|
|
- 0.1033384651675161628243434662e+04,
|
|
|
- 0.2485841954911840502660889866e+03,
|
|
|
- 0.1566564964979791769948970100e+02
|
|
|
+ static double p[] = {
|
|
|
+ -0.13688768894191926929e+2,
|
|
|
+ -0.20505855195861651981e+2,
|
|
|
+ -0.84946240351320683534e+1,
|
|
|
+ -0.83758299368150059274e+0
|
|
|
};
|
|
|
-
|
|
|
- static double q[6] = {
|
|
|
- 0.7698297257888171026986294911e+03,
|
|
|
- 0.1813892701754635858982709369e+04,
|
|
|
- 0.1484049607102276827437401170e+04,
|
|
|
- 0.4904645326203706217748848797e+03,
|
|
|
- 0.5593479839280348664778328000e+02,
|
|
|
- 0.1000000000000000000000000000e+01
|
|
|
+ static double q[] = {
|
|
|
+ 0.41066306682575781263e+2,
|
|
|
+ 0.86157349597130242515e+2,
|
|
|
+ 0.59578436142597344465e+2,
|
|
|
+ 0.15024001160028576121e+2,
|
|
|
+ 1.0
|
|
|
+ };
|
|
|
+ static double a[] = {
|
|
|
+ 0.0,
|
|
|
+ 0.52359877559829887307710723554658381, /* pi/6 */
|
|
|
+ M_PI_2,
|
|
|
+ 1.04719755119659774615421446109316763 /* pi/3 */
|
|
|
};
|
|
|
|
|
|
- int negative = x < 0.0;
|
|
|
- register struct precomputed *pr = prec;
|
|
|
+ int neg = x < 0;
|
|
|
+ int n;
|
|
|
+ double g;
|
|
|
|
|
|
- if (negative) {
|
|
|
+ if (neg) {
|
|
|
x = -x;
|
|
|
}
|
|
|
- while (x > pr->X) pr++;
|
|
|
- if (pr != prec) {
|
|
|
- x = pr->arctan +
|
|
|
- atan(pr->one_o_x - pr->one_o_xsq_p_1/(pr->one_o_x + x));
|
|
|
+ if (x > 1.0) {
|
|
|
+ x = 1.0/x;
|
|
|
+ n = 2;
|
|
|
}
|
|
|
- else {
|
|
|
- double xsq = x*x;
|
|
|
+ else n = 0;
|
|
|
|
|
|
- x = x * POLYNOM4(xsq, p)/POLYNOM5(xsq, q);
|
|
|
+ if (x > 0.26794919243112270647) { /* 2-sqtr(3) */
|
|
|
+ n = n + 1;
|
|
|
+ x = (((0.73205080756887729353*x-0.5)-0.5)+x)/
|
|
|
+ (1.73205080756887729353+x);
|
|
|
}
|
|
|
- return negative ? -x : x;
|
|
|
-}
|
|
|
|
|
|
+ /* ??? avoid underflow ??? */
|
|
|
+
|
|
|
+ g = x * x;
|
|
|
+ x += x * g * POLYNOM3(g, p) / POLYNOM4(g, q);
|
|
|
+ if (n > 1) x = -x;
|
|
|
+ x += a[n];
|
|
|
+ return neg ? -x : x;
|
|
|
+}
|