erf.cpp 1004 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //-----------------------------------------------------------------------------
  2. //
  3. // Author : philippe.billet@noos.fr
  4. //
  5. // Error function erf(x)
  6. // erf(-x)=erf(x)
  7. //
  8. //-----------------------------------------------------------------------------
  9. #include "stdafx.h"
  10. #include "defs.h"
  11. static void yyerf(void);
  12. void
  13. eval_erf(void)
  14. {
  15. push(cadr(p1));
  16. eval();
  17. yerf();
  18. }
  19. void
  20. yerf(void)
  21. {
  22. save();
  23. yyerf();
  24. restore();
  25. }
  26. static void
  27. yyerf(void)
  28. {
  29. double d;
  30. p1 = pop();
  31. if (isdouble(p1)) {
  32. d = 1.0 - erfc(p1->u.d);
  33. push_double(d);
  34. return;
  35. }
  36. if (isnegativeterm(p1)) {
  37. push_symbol(ERF);
  38. push(p1);
  39. negate();
  40. list(2);
  41. negate();
  42. return;
  43. }
  44. push_symbol(ERF);
  45. push(p1);
  46. list(2);
  47. return;
  48. }
  49. #if SELFTEST
  50. static char *s[] = {
  51. "erf(a)",
  52. "erf(a)",
  53. "erf(0.0) + 1", // add 1 to round off
  54. "1",
  55. "float(erf(0)) + 1", // add 1 to round off
  56. "1",
  57. #if 0
  58. "float(erf(1))",
  59. "0.842701",
  60. #endif
  61. };
  62. void
  63. test_erf(void)
  64. {
  65. test(__FILE__, s, sizeof s / sizeof (char *));
  66. }
  67. #endif