erfc.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //-----------------------------------------------------------------------------
  2. //
  3. // Author : philippe.billet@noos.fr
  4. //
  5. // erfc(x)
  6. //
  7. // GW Added erfc() from Numerical Recipes in C
  8. //
  9. //-----------------------------------------------------------------------------
  10. #include "stdafx.h"
  11. #include "defs.h"
  12. static void yyerfc(void);
  13. void
  14. eval_erfc(void)
  15. {
  16. push(cadr(p1));
  17. eval();
  18. yerfc();
  19. }
  20. void
  21. yerfc(void)
  22. {
  23. save();
  24. yyerfc();
  25. restore();
  26. }
  27. static void
  28. yyerfc(void)
  29. {
  30. double d;
  31. p1 = pop();
  32. if (isdouble(p1)) {
  33. d = erfc(p1->u.d);
  34. push_double(d);
  35. return;
  36. }
  37. push_symbol(ERFC);
  38. push(p1);
  39. list(2);
  40. return;
  41. }
  42. // from Numerical Recipes in C
  43. #ifndef LINUX
  44. double
  45. erfc(double x)
  46. {
  47. double t, z, ans;
  48. z = fabs(x);
  49. t = 1.0 / (1.0 + 0.5 * z);
  50. ans=t*exp(-z*z-1.26551223+t*(1.00002368+t*(0.37409196+t*(0.09678418+
  51. t*(-0.18628806+t*(0.27886807+t*(-1.13520398+t*(1.48851587+
  52. t*(-0.82215223+t*0.17087277)))))))));
  53. return x >= 0.0 ? ans : 2.0-ans;
  54. }
  55. #endif
  56. #if SELFTEST
  57. static char *s[] = {
  58. "erfc(a)",
  59. "erfc(a)",
  60. "erfc(0.0)",
  61. "1",
  62. "float(erfc(0))",
  63. "1",
  64. #if 0
  65. "float(erfc(1))",
  66. "0.157299",
  67. #endif
  68. };
  69. void
  70. test_erfc(void)
  71. {
  72. test(__FILE__, s, sizeof s / sizeof (char *));
  73. }
  74. #endif