tanhf.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* tanhf.c
  2. *
  3. * Hyperbolic tangent
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float x, y, tanhf();
  10. *
  11. * y = tanhf( x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Returns hyperbolic tangent of argument in the range MINLOG to
  18. * MAXLOG.
  19. *
  20. * A polynomial approximation is used for |x| < 0.625.
  21. * Otherwise,
  22. *
  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 100000 1.3e-7 2.6e-8
  32. *
  33. */
  34. /*
  35. Cephes Math Library Release 2.2: June, 1992
  36. Copyright 1984, 1987, 1989, 1992 by Stephen L. Moshier
  37. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  38. */
  39. /* Single precision hyperbolic tangent
  40. * test interval: [-0.625, +0.625]
  41. * trials: 10000
  42. * peak relative error: 7.2e-8
  43. * rms relative error: 2.6e-8
  44. */
  45. #include <math.h>
  46. extern float MAXLOGF;
  47. float expf( float );
  48. float tanhf( float xx )
  49. {
  50. float x, z;
  51. if( xx < 0 )
  52. x = -xx;
  53. else
  54. x = xx;
  55. if( x > 0.5 * MAXLOGF )
  56. {
  57. if( xx > 0 )
  58. return( 1.0 );
  59. else
  60. return( -1.0 );
  61. }
  62. if( x >= 0.625 )
  63. {
  64. x = expf(x+x);
  65. z = 1.0 - 2.0/(x + 1.0);
  66. if( xx < 0 )
  67. z = -z;
  68. }
  69. else
  70. {
  71. z = x * x;
  72. z =
  73. (((( -5.70498872745E-3 * z
  74. + 2.06390887954E-2) * z
  75. - 5.37397155531E-2) * z
  76. + 1.33314422036E-1) * z
  77. - 3.33332819422E-1) * z * xx
  78. + xx;
  79. }
  80. return( z );
  81. }