tanh.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * (c) copyright 1988 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 <float.h>
  9. #include <math.h>
  10. #include <errno.h>
  11. #include "localmath.h"
  12. double
  13. tanh(double x)
  14. {
  15. /* Algorithm and coefficients from:
  16. "Software manual for the elementary functions"
  17. by W.J. Cody and W. Waite, Prentice-Hall, 1980
  18. */
  19. static double p[] = {
  20. -0.16134119023996228053e+4,
  21. -0.99225929672236083313e+2,
  22. -0.96437492777225469787e+0
  23. };
  24. static double q[] = {
  25. 0.48402357071988688686e+4,
  26. 0.22337720718962312926e+4,
  27. 0.11274474380534949335e+3,
  28. 1.0
  29. };
  30. int negative = x < 0;
  31. if (__IsNan(x)) {
  32. errno = EDOM;
  33. return x;
  34. }
  35. if (negative) x = -x;
  36. if (x >= 0.5*M_LN_MAX_D) {
  37. x = 1.0;
  38. }
  39. #define LN3D2 0.54930614433405484570e+0 /* ln(3)/2 */
  40. else if (x > LN3D2) {
  41. x = 0.5 - 1.0/(exp(x+x)+1.0);
  42. x += x;
  43. }
  44. else {
  45. /* ??? avoid underflow ??? */
  46. double g = x*x;
  47. x += x * g * POLYNOM2(g, p)/POLYNOM3(g, q);
  48. }
  49. return negative ? -x : x;
  50. }