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

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