tanh.cpp 651 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // exp(2 x) - 1
  2. // tanh(x) = --------------
  3. // exp(2 x) + 1
  4. #include "stdafx.h"
  5. #include "defs.h"
  6. void
  7. eval_tanh(void)
  8. {
  9. double d;
  10. push(cadr(p1));
  11. eval();
  12. p1 = pop();
  13. if (car(p1) == symbol(ARCTANH)) {
  14. push(cadr(p1));
  15. return;
  16. }
  17. if (isdouble(p1)) {
  18. d = tanh(p1->u.d);
  19. if (fabs(d) < 1e-10)
  20. d = 0.0;
  21. push_double(d);
  22. return;
  23. }
  24. if (iszero(p1)) {
  25. push(zero);
  26. return;
  27. }
  28. push_symbol(TANH);
  29. push(p1);
  30. list(2);
  31. }
  32. #if SELFTEST
  33. static char *s[] = {
  34. "tanh(x)",
  35. "tanh(x)",
  36. "tanh(0)",
  37. "0",
  38. "tanh(arctanh(x))",
  39. "x",
  40. };
  41. void
  42. test_tanh(void)
  43. {
  44. test(__FILE__, s, sizeof s / sizeof (char *));
  45. }
  46. #endif