2 /* @(#)z_exp.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
8 ******************************************************************/
12 <<exp>>, <<expf>>---exponential
20 double exp(double <[x]>);
21 float expf(float <[x]>);
32 <<exp>> and <<expf>> calculate the exponential of <[x]>, that is,
34 e raised to the power <[x]> (where e
39 is the base of the natural system of logarithms, approximately 2.71828).
42 On success, <<exp>> and <<expf>> return the calculated value.
43 If the result underflows, the returned value is <<0>>. If the
44 result overflows, the returned value is <<HUGE_VAL>>. In
45 either case, <<errno>> is set to <<ERANGE>>.
48 <<exp>> is ANSI C. <<expf>> is an extension.
52 /*****************************************************************
53 * Exponential Function
56 * x - floating point value
62 * This routine returns e raised to the xth power.
64 *****************************************************************/
70 #ifndef _DOUBLE_IS_32BITS
72 static const double INV_LN2 = 1.4426950408889634074;
73 static const double LN2 = 0.6931471805599453094172321;
74 static const double p[] = { 0.25, 0.75753180159422776666e-2,
75 0.31555192765684646356e-4 };
76 static const double q[] = { 0.5, 0.56817302698551221787e-1,
77 0.63121894374398504557e-3,
78 0.75104028399870046114e-6 };
81 _DEFUN (exp, (double),
95 return (z_infinity.d);
102 /* Check for out of bounds. */
103 if (x > BIGX || x < SMALLX)
109 /* Check for a value too small to calculate. */
110 if (-z_rooteps < x && x < z_rooteps)
115 /* Calculate the exponent. */
117 N = (int) (x * INV_LN2 - 0.5);
119 N = (int) (x * INV_LN2 + 0.5);
121 /* Construct the mantissa. */
124 P = g * ((p[2] * z + p[1]) * z + p[0]);
125 Q = ((q[3] * z + q[2]) * z + q[1]) * z + q[0];
126 R = 0.5 + P / (Q - P);
128 /* Return the floating point value. */
130 return (ldexp (R, N));
133 #endif /* _DOUBLE_IS_32BITS */