home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / math / cephes / cmath / tanh.c < prev   
Encoding:
C/C++ Source or Header  |  1992-11-17  |  2.3 KB  |  131 lines

  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        0,2         5000       2.9e-17     6.4e-18
  32.  *    IEEE      -2,2        30000       2.5e-16     5.8e-17
  33.  *
  34.  */
  35.  
  36. /*
  37. Cephes Math Library Release 2.1:  February, 1989
  38. Copyright 1984, 1987, 1989 by Stephen L. Moshier
  39. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  40. */
  41.  
  42. #include "mconf.h"
  43.  
  44. #ifdef UNK
  45. static double P[] = {
  46. -9.64399179425052238628E-1,
  47. -9.92877231001918586564E1,
  48. -1.61468768441708447952E3
  49. };
  50. static double Q[] = {
  51. /* 1.00000000000000000000E0,*/
  52.  1.12811678491632931402E2,
  53.  2.23548839060100448583E3,
  54.  4.84406305325125486048E3
  55. };
  56. #endif
  57. #ifdef DEC
  58. static short P[] = {
  59. 0140166,0161335,0053753,0075126,
  60. 0141706,0111520,0070463,0040552,
  61. 0142711,0153001,0101300,0025430
  62. };
  63. static short Q[] = {
  64. /*0040200,0000000,0000000,0000000,*/
  65. 0041741,0117624,0051300,0156060,
  66. 0043013,0133720,0071251,0127717,
  67. 0043227,0060201,0021020,0020136
  68. };
  69. #endif
  70.  
  71. #ifdef IBMPC
  72. static short P[] = {
  73. 0x6f4b,0xaafd,0xdc5b,0xbfee,
  74. 0x682d,0x0e26,0xd26a,0xc058,
  75. 0x0563,0x3058,0x3ac0,0xc099
  76. };
  77. static short Q[] = {
  78. /*0x0000,0x0000,0x0000,0x3ff0,*/
  79. 0x1b86,0x8a58,0x33f2,0x405c,
  80. 0x35fa,0x0e55,0x76fa,0x40a1,
  81. 0x040c,0x2442,0xec10,0x40b2
  82. };
  83. #endif
  84.  
  85. #ifdef MIEEE
  86. static short P[] = {
  87. 0xbfee,0xdc5b,0xaafd,0x6f4b,
  88. 0xc058,0xd26a,0x0e26,0x682d,
  89. 0xc099,0x3ac0,0x3058,0x0563
  90. };
  91. static short Q[] = {
  92. 0x405c,0x33f2,0x8a58,0x1b86,
  93. 0x40a1,0x76fa,0x0e55,0x35fa,
  94. 0x40b2,0xec10,0x2442,0x040c
  95. };
  96. #endif
  97.  
  98. extern double MAXLOG;
  99.  
  100. double tanh(x)
  101. double x;
  102. {
  103. double s, z;
  104. double fabs(), exp(), polevl(), p1evl();
  105.  
  106.  
  107. z = fabs(x);
  108. if( z > 0.5 * MAXLOG )
  109.     {
  110.     if( x > 0 )
  111.         return( 1.0 );
  112.     else
  113.         return( -1.0 );
  114.     }
  115. if( z >= 0.625 )
  116.     {
  117.     s = exp(2.0*z);
  118.     z =  1.0  - 2.0/(s + 1.0);
  119.     if( x < 0 )
  120.         z = -z;
  121.     }
  122. else
  123.     {
  124.     s = x * x;
  125.     z = polevl( s, P, 2 )/p1evl(s, Q, 3);
  126.     z = x * s * z;
  127.     z = x + z;
  128.     }
  129. return( z );
  130. }
  131.