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; }
double tan(double x) { /* Algorithm and coefficients from: "Software manual for the elementary functions" by W.J. Cody and W. Waite, Prentice-Hall, 1980 */ int negative = x < 0; int invert = 0; double y; static double p[] = { 1.0, -0.13338350006421960681e+0, 0.34248878235890589960e-2, -0.17861707342254426711e-4 }; static double q[] = { 1.0, -0.46671683339755294240e+0, 0.25663832289440112864e-1, -0.31181531907010027307e-3, 0.49819433993786512270e-6 }; if (__IsNan(x)) { errno = EDOM; return x; } if (negative) x = -x; /* ??? avoid loss of significance, error if x is too large ??? */ y = x * M_2_PI + 0.5; if (y >= DBL_MAX/M_PI_2) return 0.0; /* Use extended precision to calculate reduced argument. Here we used 12 bits of the mantissa for a1. Also split x in integer part x1 and fraction part x2. */ #define A1 1.57080078125 #define A2 -4.454455103380768678308e-6 { double x1, x2; modf(y, &y); if (modf(0.5*y, &x1)) invert = 1; x2 = modf(x, &x1); x = x1 - y * A1; x += x2; x -= y * A2; #undef A1 #undef A2 } /* ??? avoid underflow ??? */ y = x * x; x += x * y * POLYNOM2(y, p+1); y = POLYNOM4(y, q); if (negative) x = -x; return invert ? -y/x : x/y; }