static double sinh_cosh(double x, int cosh_flag) { /* Algorithm and coefficients from: "Software manual for the elementary functions" by W.J. Cody and W. Waite, Prentice-Hall, 1980 */ static double p[] = { -0.35181283430177117881e+6, -0.11563521196851768270e+5, -0.16375798202630751372e+3, -0.78966127417357099479e+0 }; static double q[] = { -0.21108770058106271242e+7, 0.36162723109421836460e+5, -0.27773523119650701167e+3, 1.0 }; int negative = x < 0; double y = negative ? -x : x; if (__IsNan(x)) { errno = EDOM; return x; } if (! cosh_flag && y <= 1.0) { /* ??? check for underflow ??? */ y = y * y; return x + x * y * POLYNOM3(y, p)/POLYNOM3(y,q); } if (y >= M_LN_MAX_D) { /* exp(y) would cause overflow */ #define LNV 0.69316101074218750000e+0 #define VD2M1 0.52820835025874852469e-4 double w = y - LNV; if (w < M_LN_MAX_D+M_LN2-LNV) { x = exp(w); x += VD2M1 * x; } else { errno = ERANGE; x = HUGE_VAL; } } else { double z = exp(y); x = 0.5 * (z + (cosh_flag ? 1.0 : -1.0)/z); } return negative ? -x : x; }
double atan(double x) { /* Algorithm and coefficients from: "Software manual for the elementary functions" by W.J. Cody and W. Waite, Prentice-Hall, 1980 */ static double p[] = { -0.13688768894191926929e+2, -0.20505855195861651981e+2, -0.84946240351320683534e+1, -0.83758299368150059274e+0 }; 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 neg = x < 0; int n; double g; if (__IsNan(x)) { errno = EDOM; return x; } if (neg) { x = -x; } if (x > 1.0) { x = 1.0/x; n = 2; } else n = 0; if (x > 0.26794919243112270647) { /* 2-sqtr(3) */ n = n + 1; x = (((0.73205080756887729353*x-0.5)-0.5)+x)/ (1.73205080756887729353+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; }
double exp(double x) { /* Algorithm and coefficients from: "Software manual for the elementary functions" by W.J. Cody and W. Waite, Prentice-Hall, 1980 */ static double p[] = { 0.25000000000000000000e+0, 0.75753180159422776666e-2, 0.31555192765684646356e-4 }; static double q[] = { 0.50000000000000000000e+0, 0.56817302698551221787e-1, 0.63121894374398503557e-3, 0.75104028399870046114e-6 }; double xn, g; int n; int negative = x < 0; if (__IsNan(x)) { errno = EDOM; return x; } if (x < M_LN_MIN_D) { errno = ERANGE; return 0.0; } if (x > M_LN_MAX_D) { errno = ERANGE; return HUGE_VAL; } if (negative) x = -x; /* ??? avoid underflow ??? */ n = x * M_LOG2E + 0.5; /* 1/ln(2) = log2(e), 0.5 added for rounding */ xn = n; { double x1 = (long) x; double x2 = x - x1; g = ((x1-xn*0.693359375)+x2) - xn*(-2.1219444005469058277e-4); } if (negative) { g = -g; n = -n; } xn = g * g; x = g * POLYNOM2(xn, p); n += 1; return (ldexp(0.5 + x/(POLYNOM3(xn, q) - x), n)); }
double log(double x) { /* Algorithm and coefficients from: "Software manual for the elementary functions" by W.J. Cody and W. Waite, Prentice-Hall, 1980 */ static double a[] = { -0.64124943423745581147e2, 0.16383943563021534222e2, -0.78956112887491257267e0 }; static double b[] = { -0.76949932108494879777e3, 0.31203222091924532844e3, -0.35667977739034646171e2, 1.0 }; double znum, zden, z, w; int exponent; if (__IsNan(x)) { errno = EDOM; return x; } if (x < 0) { errno = EDOM; return -HUGE_VAL; } else if (x == 0) { errno = ERANGE; return -HUGE_VAL; } if (x <= DBL_MAX) { } else return x; /* for infinity and Nan */ x = frexp(x, &exponent); if (x > M_1_SQRT2) { znum = (x - 0.5) - 0.5; zden = x * 0.5 + 0.5; } else { znum = x - 0.5; zden = znum * 0.5 + 0.5; exponent--; } z = znum/zden; w = z * z; x = z + z * w * (POLYNOM2(w,a)/POLYNOM3(w,b)); z = exponent; x += z * (-2.121944400546905827679e-4); return x + z * 0.693359375; }
double tanh(double x) { /* Algorithm and coefficients from: "Software manual for the elementary functions" by W.J. Cody and W. Waite, Prentice-Hall, 1980 */ static double p[] = { -0.16134119023996228053e+4, -0.99225929672236083313e+2, -0.96437492777225469787e+0 }; static double q[] = { 0.48402357071988688686e+4, 0.22337720718962312926e+4, 0.11274474380534949335e+3, 1.0 }; int negative = x < 0; if (__IsNan(x)) { errno = EDOM; return x; } if (negative) x = -x; if (x >= 0.5*M_LN_MAX_D) { x = 1.0; } #define LN3D2 0.54930614433405484570e+0 /* ln(3)/2 */ else if (x > LN3D2) { x = 0.5 - 1.0/(exp(x+x)+1.0); x += x; } else { /* ??? avoid underflow ??? */ double g = x*x; x += x * g * POLYNOM2(g, p)/POLYNOM3(g, q); } return negative ? -x : x; }
/****** BEGIN ************************************************************* */ float mymathAtan(float x) { static float p[] = { -0.13688768894191926929e+2, -0.20505855195861651981e+2, -0.84946240351320683534e+1, -0.83758299368150059274e+0 }; static float q[] = { 0.41066306682575781263e+2, 0.86157349597130242515e+2, 0.59578436142597344465e+2, 0.15024001160028576121e+2, 1.0 }; static float a[] = { 0.0, MATH_PI6, /* pi/6 */ MATH_PI2, /* pi/2 */ MATH_PI3 /* pi/3 */ }; int32_t neg = x < 0; int32_t n; float g; if (neg) { x = -x; } if (x > 1.0) { x = 1.0 / x; n = 2; } else n = 0; if (x > 0.26794919243112270647) { /* 2-sqtr(3) */ n = n + 1; x = (((0.73205080756887729353 * x - 0.5) - 0.5) + x) / (1.73205080756887729353 + x); } g = x * x; x += x * g * POLYNOM3(g, p) / POLYNOM4(g, q); if (n > 1) x = -x; x += a[n]; return neg ? -x : x; }