home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / cbw / part01 / approx.c next >
Encoding:
C/C++ Source or Header  |  1987-06-16  |  1.8 KB  |  122 lines

  1. /*
  2.  * Fast approximations of useful math functions.
  3.  *
  4.  * Robert W. Baldwin, May 1985.
  5.  */
  6.  
  7.  
  8. #include    <stdio.h>
  9. #include    <math.h>
  10. #include    "window.h"
  11. #include    "specs.h"
  12.  
  13.  
  14. #define STANDALONE    FALSE
  15.  
  16. #define    NFEXP        100        /* Number of entries in fexp_value table. */
  17. #define    DXFEXP        0.05    /* Interval width in fexp table. */
  18. #define    MAXFEXP        (NFEXP * DXFEXP)    /* Max value < this */
  19.  
  20.  
  21. /* Table of values for exp(-(x*x)/2) starting with zero at intervals of 0.1
  22.  * The values of the derivative of exp(-(x*x)/2) are in fexp_deriv.
  23.  */
  24. float    fexp_value[NFEXP];
  25. float    fexp_deriv[NFEXP];
  26.  
  27.  
  28. /* Table for fast square root computation.
  29.  */
  30. float    isqrt[BLOCKSIZE];
  31.  
  32.  
  33. #if STANDALONE
  34. main()
  35. {
  36.     int        i;
  37.     float    fi;
  38.  
  39.     printf("\t\t\t\tTable of exp(-(x*x)/2)");
  40.     printf("\nX\t\treal\t\tapprox");
  41.     printf("\n\n");
  42.  
  43.     approx_init();
  44.  
  45.     for (i = 0 ; i < (NFEXP + 5) ; i++)  {
  46.         fi = i;
  47.         fi = fi * DXFEXP;
  48.         fi += DXFEXP/2.0;
  49.         printf("%f\t", fi);
  50.         printf("%f\t", exp(-(fi*fi)/2));
  51.         printf("%f", fexp(fi));
  52.         printf("\n");
  53.         }
  54. }
  55. #endif
  56.  
  57.  
  58.  
  59. /* Initialize the approximation tables.
  60.  */
  61. approx_init()
  62. {
  63.     sqrt_tab();
  64.     fexp_tab();
  65. }
  66.  
  67.  
  68. /* Fill in the table of square roots.
  69.  */
  70. sqrt_tab()
  71. {
  72.     reg    int        i;
  73.         float    fi;
  74.  
  75.     for (i = 0 ; i < BLOCKSIZE ; i++)  {
  76.         fi = i;
  77.         isqrt[i] = sqrt(fi);
  78.         }
  79. }
  80.  
  81.  
  82. /* Fill in th approximation table for fexp.
  83.  */
  84. fexp_tab()
  85. {
  86.     int        i;
  87.     float    fi;
  88.     float    value;
  89.     float    deriv;
  90.  
  91.     for (i = 0 ; i < NFEXP ; i++)  {
  92.         fi = i;
  93.         fi = fi * DXFEXP;
  94.         value = exp(-(fi*fi)/2);
  95.         deriv = -fi * value;
  96.         fexp_value[i] = value;
  97.         fexp_deriv[i] = deriv;
  98.         }
  99. }
  100.  
  101.  
  102. /* Return a fast approximation to exp(-(x*x)/2).
  103.  */
  104. float    fexp(x)
  105. reg    float    x;
  106. {
  107. reg    int        index;
  108.     float    approx;
  109. reg    float    result;
  110.  
  111.     x = abs(x);
  112.     if (x >= MAXFEXP)
  113.         return(0.0);
  114.     index = x * (1.0 / DXFEXP);
  115.     approx = index;
  116.     approx = approx * DXFEXP;
  117.     result = fexp_value[index];
  118.     result += (x - approx) * fexp_deriv[index];
  119.  
  120.     return(result);
  121. }
  122.