home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_07_06 / v7n6104a.txt < prev   
Text File  |  1989-07-25  |  3KB  |  86 lines

  1. /* #include file to provide some non-standard definitions 
  2. ** particularly useful for float math */
  3. #define ROUND(x)    (int)(x+((x>0.)?.5:-.5))
  4. #ifndef PI
  5. #define PI         3.14159265359
  6. #endif
  7.  
  8. #ifdef __STDC__
  9. #include <math.h> /* needed for most traditional K&R compilers also */
  10. #include <float.h>
  11. #define ROUNDFCT    1f/FLT_EPSILON /* if no extra precision used */
  12. #define PIF        3.14159265f
  13. #define PI2F        PIF*.5f
  14. #if (FLT_ROUNDS>0)    /* rounding addition used to round to integral float */
  15. #define ROUNDF(x)     ((x)>0f)?((x)+ROUNDFCT)-ROUNDFCT:((x)-ROUNDFCT)+ROUNDFCT)
  16. #endif            /* depends on observance of parentheses */
  17.  
  18. #else            /* if not ANSI C */
  19. #define FLT_ROUNDS    1    /* check if so */
  20. #define ROUNDFCT    8388608
  21. /* add and subtract 2^23 for rounding;
  22. ** 2^52, 2^55 typical for double arith */
  23. #define PIF        PI    
  24. #define PI2F        PI*.5
  25. #define f             /* ignore float constant specifier */
  26. #endif
  27.  
  28. #ifndef min
  29. #define min(i,j)    ((i)<(j)?(i):(j)) /* typeless */
  30. #endif
  31. #ifndef max
  32. #define max(i,j)    ((i)>(j)?(i):(j))
  33. #endif
  34.  
  35. /* if unable to determine how to round without (long)*/
  36. #ifndef ROUNDF        
  37. #define ROUNDF(x)    ROUND(x) /* or floor(x+.5) */
  38. #endif
  39.  
  40. #define PCPVL(x,y)    ((x)-ROUNDF((x)/(y))*(y)) /* reduce to [-y/2,y/2] */
  41. #define SQ(x)        (x)*(x)
  42.  
  43. /* rational polynomial relative error 2e-8
  44. ** not recommended for systems with internal microcode for trig */
  45. #define PTANNUMF(x)    (x)*(886.77347f+SQ(x)*(-99.398953f+SQ(x)))
  46. #define PTANDENF(x)    (886.77345f+SQ(x)*(-394.98971f+SQ(x)*14.425694f))
  47. #define PTANF(x)    PTANNUMF(x)/PTANDENF(x) /* |x| < PI/2 */
  48. #define tanf(x)        PTANF(PCPVL(x,PIF)) /* |x| <= ROUNDFCT */
  49.  
  50. #define PSCDENF(x)    (SQ(PTANDENF(x))+SQ(PTANNUMF(x)))
  51. /* |x| <= PI for PSINF, PCOSF */
  52. #define PSINF(x)    PTANNUMF(x)*PTANDENF(x)*2.f/PSCDENF(x)
  53. /* this method is good when both sinf() and cosf() are wanted */
  54. #define sinf(x)        PSINF(PCPVL((x)*.5f,PIF)) /* |x| <= ROUNDFCT*2 */
  55. #define PCOSF(x)    (SQ(PTANDENF(x))-SQ(PTANNUMF(x)))/PSCDENF(x)
  56. #define cosf(x)        PCOSF(PCPVL((x)*.5f,PIF)) /* |x| <= ROUNDFCT*2 */
  57.  
  58. /* #ifdef PIPELINE
  59. ** Hoerner polynomial split in 2 to help pipeline processors **
  60. #define PATANF(x)    ((x)*(.9999999f+SQ(x)*(-.3333196f+SQ(x)*(.1996924f- \
  61.                 SQ(x)*.1401659f)))+((x)*SQ(SQ(SQ(x)))) \
  62.                 *(.0990610f+SQ(x)*(-.0593671f+SQ(x)* \
  63.                 (.0241662f-SQ(x)*.0046688f))))
  64. #else */
  65. #define PATANF(x)    (x)*(.9999999f+SQ(x)*(-.3333196f+SQ(x)*(.1996924f+ \
  66. /* |x| <= 1 */        SQ(x)*(-.1401659f+SQ(x)*(.0990610f+SQ(x)*(-.0593671f+ \
  67.                 SQ(x)*(.0241662f-SQ(x)*.0046688f)))))))
  68. /* #endif */
  69. #define atanf(x)    (((x)>1.f?PI2F:(x)>=-1.f?0.f:-PI2F)+PATANF( \
  70. /* all x */            (x)<=1.f&&(x)>=-1.f?(x):-1.f/(x)))
  71. #define atan2f(x,y)    ((y)!=0.f?(atanf((x)/(y))+((y)>0.f?0.f:(x)>=0.f?PIF: \
  72.                 -PIF)):((x)>=0.f?PI2F:-PI2F))
  73. /* better, if compiler deals efficiently with comparisons:
  74. #define atan2f(x,y)    ((fabsf(x)<=fabsf(y)?PATANF((x)/(y):( \
  75.             ((x)>=0.f)==((y)>=0.f)?PI2F:-PI2F)-PATANF((y)/(x))) \
  76.                 +((y)>=0.f?0.f:(x)>=0.f?PIF:-PIF))    */
  77.  
  78. #ifndef sqrtf
  79. #ifndef sqrt
  80.     double sqrt();
  81. #endif
  82. #define sqrtf(x)    sqrt(x)
  83. #endif
  84. #define asinf(x)    atanf((x)/sqrtf((1.f+(x))*(1.f-(x))))
  85. #define acosf(x)    (PI2F-asinf(x))
  86.