tanhl.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* tanhl.c
  2. *
  3. * Hyperbolic tangent, long double precision
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * long double x, y, tanhl();
  10. *
  11. * y = tanhl( x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Returns hyperbolic tangent of argument in the range MINLOGL to
  18. * MAXLOGL.
  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. * IEEE -2,2 30000 1.3e-19 2.4e-20
  32. *
  33. */
  34. /*
  35. Cephes Math Library Release 2.7: May, 1998
  36. Copyright 1984, 1987, 1989, 1998 by Stephen L. Moshier
  37. */
  38. #include <math.h>
  39. #ifdef UNK
  40. static long double P[] = {
  41. -6.8473739392677100872869E-5L,
  42. -9.5658283111794641589011E-1L,
  43. -8.4053568599672284488465E1L,
  44. -1.3080425704712825945553E3L,
  45. };
  46. static long double Q[] = {
  47. /* 1.0000000000000000000000E0L,*/
  48. 9.6259501838840336946872E1L,
  49. 1.8218117903645559060232E3L,
  50. 3.9241277114138477845780E3L,
  51. };
  52. #endif
  53. #ifdef IBMPC
  54. static short P[] = {
  55. 0xd2a4,0x1b0c,0x8f15,0x8f99,0xbff1, XPD
  56. 0x5959,0x9111,0x9cc7,0xf4e2,0xbffe, XPD
  57. 0xb576,0xef5e,0x6d57,0xa81b,0xc005, XPD
  58. 0xe3be,0xbfbd,0x5cbc,0xa381,0xc009, XPD
  59. };
  60. static short Q[] = {
  61. /*0x0000,0x0000,0x0000,0x8000,0x3fff,*/
  62. 0x687f,0xce24,0xdd6c,0xc084,0x4005, XPD
  63. 0x3793,0xc95f,0xfa2f,0xe3b9,0x4009, XPD
  64. 0xd5a2,0x1f9c,0x0b1b,0xf542,0x400a, XPD
  65. };
  66. #endif
  67. #ifdef MIEEE
  68. static long P[] = {
  69. 0xbff10000,0x8f998f15,0x1b0cd2a4,
  70. 0xbffe0000,0xf4e29cc7,0x91115959,
  71. 0xc0050000,0xa81b6d57,0xef5eb576,
  72. 0xc0090000,0xa3815cbc,0xbfbde3be,
  73. };
  74. static long Q[] = {
  75. /*0x3fff0000,0x80000000,0x00000000,*/
  76. 0x40050000,0xc084dd6c,0xce24687f,
  77. 0x40090000,0xe3b9fa2f,0xc95f3793,
  78. 0x400a0000,0xf5420b1b,0x1f9cd5a2,
  79. };
  80. #endif
  81. extern long double MAXLOGL;
  82. #ifdef ANSIPROT
  83. extern long double fabsl ( long double );
  84. extern long double expl ( long double );
  85. extern long double polevll ( long double, void *, int );
  86. extern long double p1evll ( long double, void *, int );
  87. #else
  88. long double fabsl(), expl(), polevll(), p1evll();
  89. #endif
  90. long double tanhl(x)
  91. long double x;
  92. {
  93. long double s, z;
  94. #ifdef MINUSZERO
  95. if( x == 0.0L )
  96. return(x);
  97. #endif
  98. z = fabsl(x);
  99. if( z > 0.5L * MAXLOGL )
  100. {
  101. if( x > 0 )
  102. return( 1.0L );
  103. else
  104. return( -1.0L );
  105. }
  106. if( z >= 0.625L )
  107. {
  108. s = expl(2.0*z);
  109. z = 1.0L - 2.0/(s + 1.0L);
  110. if( x < 0 )
  111. z = -z;
  112. }
  113. else
  114. {
  115. s = x * x;
  116. z = polevll( s, P, 3 )/p1evll(s, Q, 3);
  117. z = x * s * z;
  118. z = x + z;
  119. }
  120. return( z );
  121. }