home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / xexp.c < prev    next >
C/C++ Source or Header  |  1998-06-16  |  1KB  |  63 lines

  1. /* _Exp function */
  2. #include "wctype.h"
  3. #include "xmath.h"
  4. _STD_BEGIN
  5.  
  6. /* coefficients */
  7. static const double p[] = {    /* courtesy Dr. Tim Prince */
  8.     1.0,
  9.     420.30235984910635,
  10.     15132.70094680474802};
  11. static const double q[] = {    /* courtesy Dr. Tim Prince */
  12.     30.01511290683317,
  13.     3362.72154416553028,
  14.     30265.40189360949691};
  15. static const double c1 = 22713.0 / 32768.0;
  16. static const double c2 = 1.428606820309417232e-6L;
  17. static const double hugexp = HUGE_EXP;
  18. static const double invln2 = 1.4426950408889634074L;
  19.  
  20. _CRTIMP2 short __cdecl _Exp(double *px, double y, short eoff)
  21.     {    /* compute y*e^(*px), (*px) finite, |y| not huge */
  22.     if (*px < -hugexp || y == 0)
  23.         {    /* certain underflow */
  24.         *px = 0;
  25.         return (0);
  26.         }
  27.     else if (hugexp < *px)
  28.         {    /* certain overflow */
  29.         *px = _Inf._D;
  30.         return (INF);
  31.         }
  32.     else
  33.         {    /* xexp won't overflow */
  34.         double g = *px * invln2;
  35.         short xexp = g + (g < 0 ? - 0.5 : + 0.5);
  36.  
  37.         g = xexp;
  38.         g = (*px - g * c1) - g * c2;
  39.         if (-_Eps._D < g && g < _Eps._D)
  40.             *px = y;
  41.         else
  42.             {    /* g*g worth computing */
  43.             const double z = g * g;
  44.             const double w = (q[0] * z + q[1]) * z + q[2];
  45.  
  46.             g *= (z + p[1]) * z + p[2];
  47.             *px = (w + g) / (w - g) * 2 * y;
  48.             --xexp;
  49.             }
  50.         return (_Dscale(px, (long)xexp + eoff));
  51.         }
  52.     }
  53. _STD_END
  54.  
  55. /*
  56.  * Copyright (c) 1994 by P.J. Plauger.  ALL RIGHTS RESERVED. 
  57.  * Consult your license regarding permissions and restrictions.
  58.  */
  59.  
  60. /*
  61. 941029 pjp: added _STD machinery
  62.  */
  63.