tanh.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * (c) copyright 1989 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. *
  5. * Author: Ceriel J.H. Jacobs
  6. */
  7. /* $Id$ */
  8. #include <math.h>
  9. #include <errno.h>
  10. extern int errno;
  11. extern double exp();
  12. double
  13. tanh(x)
  14. double x;
  15. {
  16. /* Algorithm and coefficients from:
  17. "Software manual for the elementary functions"
  18. by W.J. Cody and W. Waite, Prentice-Hall, 1980
  19. */
  20. static double p[] = {
  21. -0.16134119023996228053e+4,
  22. -0.99225929672236083313e+2,
  23. -0.96437492777225469787e+0
  24. };
  25. static double q[] = {
  26. 0.48402357071988688686e+4,
  27. 0.22337720718962312926e+4,
  28. 0.11274474380534949335e+3,
  29. 1.0
  30. };
  31. int negative = x < 0;
  32. if (negative) x = -x;
  33. if (x >= 0.5*M_LN_MAX_D) {
  34. x = 1.0;
  35. }
  36. #define LN3D2 0.54930614433405484570e+0 /* ln(3)/2 */
  37. else if (x > LN3D2) {
  38. x = 0.5 - 1.0/(exp(x+x)+1.0);
  39. x += x;
  40. }
  41. else {
  42. /* ??? avoid underflow ??? */
  43. double g = x*x;
  44. x += x * g * POLYNOM2(g, p)/POLYNOM3(g, q);
  45. }
  46. return negative ? -x : x;
  47. }