tanh.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* tanh.c
  2. *
  3. * Hyperbolic tangent
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * double x, y, tanh();
  10. *
  11. * y = tanh( x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Returns hyperbolic tangent of argument in the range MINLOG to
  18. * MAXLOG.
  19. *
  20. * A rational function is used for |x| < 0.625. The form
  21. * x + x**3 P(x)/Q(x) of Cody _& Waite is employed.
  22. * Otherwise,
  23. * tanh(x) = sinh(x)/cosh(x) = 1 - 2/(exp(2x) + 1).
  24. *
  25. *
  26. *
  27. * ACCURACY:
  28. *
  29. * Relative error:
  30. * arithmetic domain # trials peak rms
  31. * DEC -2,2 50000 3.3e-17 6.4e-18
  32. * IEEE -2,2 30000 2.5e-16 5.8e-17
  33. *
  34. */
  35. /*
  36. Cephes Math Library Release 2.8: June, 2000
  37. Copyright 1984, 1995, 2000 by Stephen L. Moshier
  38. */
  39. #include "mconf.h"
  40. #ifdef UNK
  41. static double P[] = {-9.64399179425052238628E-1, -9.92877231001918586564E1,
  42. -1.61468768441708447952E3};
  43. static double Q[] = {
  44. /* 1.00000000000000000000E0,*/
  45. 1.12811678491632931402E2, 2.23548839060100448583E3,
  46. 4.84406305325125486048E3};
  47. #endif
  48. #ifdef DEC
  49. static unsigned short P[] = {0140166, 0161335, 0053753, 0075126,
  50. 0141706, 0111520, 0070463, 0040552,
  51. 0142711, 0153001, 0101300, 0025430};
  52. static unsigned short Q[] = {
  53. /*0040200,0000000,0000000,0000000,*/
  54. 0041741, 0117624, 0051300, 0156060, 0043013, 0133720,
  55. 0071251, 0127717, 0043227, 0060201, 0021020, 0020136};
  56. #endif
  57. #ifdef IBMPC
  58. static unsigned short P[] = {0x6f4b, 0xaafd, 0xdc5b, 0xbfee, 0x682d, 0x0e26,
  59. 0xd26a, 0xc058, 0x0563, 0x3058, 0x3ac0, 0xc099};
  60. static unsigned short Q[] = {
  61. /*0x0000,0x0000,0x0000,0x3ff0,*/
  62. 0x1b86, 0x8a58, 0x33f2, 0x405c, 0x35fa, 0x0e55,
  63. 0x76fa, 0x40a1, 0x040c, 0x2442, 0xec10, 0x40b2};
  64. #endif
  65. #ifdef MIEEE
  66. static unsigned short P[] = {0xbfee, 0xdc5b, 0xaafd, 0x6f4b, 0xc058, 0xd26a,
  67. 0x0e26, 0x682d, 0xc099, 0x3ac0, 0x3058, 0x0563};
  68. static unsigned short Q[] = {0x405c, 0x33f2, 0x8a58, 0x1b86, 0x40a1, 0x76fa,
  69. 0x0e55, 0x35fa, 0x40b2, 0xec10, 0x2442, 0x040c};
  70. #endif
  71. #ifdef ANSIPROT
  72. extern double fabs(double);
  73. extern double exp(double);
  74. extern double polevl(double, void *, int);
  75. extern double p1evl(double, void *, int);
  76. #else
  77. double fabs(), exp(), polevl(), p1evl();
  78. #endif
  79. extern double MAXLOG;
  80. double tanh(x) double x;
  81. {
  82. double s, z;
  83. #ifdef MINUSZERO
  84. if (x == 0.0)
  85. return (x);
  86. #endif
  87. z = fabs(x);
  88. if (z > 0.5 * MAXLOG) {
  89. if (x > 0)
  90. return (1.0);
  91. else
  92. return (-1.0);
  93. }
  94. if (z >= 0.625) {
  95. s = exp(2.0 * z);
  96. z = 1.0 - 2.0 / (s + 1.0);
  97. if (x < 0)
  98. z = -z;
  99. } else {
  100. if (x == 0.0)
  101. return (x);
  102. s = x * x;
  103. z = polevl(s, P, 2) / p1evl(s, Q, 3);
  104. z = x * s * z;
  105. z = x + z;
  106. }
  107. return (z);
  108. }