tanh.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package math32
  2. // The original C code, the long comment, and the constants
  3. // below were from http://netlib.sandia.gov/cephes/cmath/tanh.c,
  4. // available from http://www.netlib.org/cephes/single.tgz.
  5. // The go code is a simplified version of the original C.
  6. // tanhf.c
  7. //
  8. // Hyperbolic tangent
  9. //
  10. //
  11. //
  12. // SYNOPSIS:
  13. //
  14. // float x, y, tanhf();
  15. //
  16. // y = tanhf( x );
  17. //
  18. //
  19. //
  20. // DESCRIPTION:
  21. //
  22. // Returns hyperbolic tangent of argument in the range MINLOG to
  23. // MAXLOG.
  24. //
  25. // A polynomial approximation is used for |x| < 0.625.
  26. // Otherwise,
  27. //
  28. // tanh(x) = sinh(x)/cosh(x) = 1 - 2/(exp(2x) + 1).
  29. //
  30. //
  31. //
  32. // ACCURACY:
  33. //
  34. // Relative error:
  35. // arithmetic domain # trials peak rms
  36. // IEEE -2,2 100000 1.3e-7 2.6e-8
  37. //
  38. //
  39. /*
  40. Cephes Math Library Release 2.2: June, 1992
  41. Copyright 1984, 1987, 1989, 1992 by Stephen L. Moshier
  42. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  43. */
  44. /* Single precision hyperbolic tangent
  45. * test interval: [-0.625, +0.625]
  46. * trials: 10000
  47. * peak relative error: 7.2e-8
  48. * rms relative error: 2.6e-8
  49. */
  50. func Tanh(x float32) float32 {
  51. const MAXLOG = 88.02969187150841
  52. z := Abs(x)
  53. switch {
  54. case z > 0.5*MAXLOG:
  55. if x < 0 {
  56. return -1
  57. }
  58. return 1
  59. case z >= 0.625:
  60. s := Exp(z + z)
  61. z = 1 - 2/(s+1)
  62. if x < 0 {
  63. z = -z
  64. }
  65. default:
  66. if x == 0 {
  67. return x
  68. }
  69. s := x * x
  70. z = ((((-5.70498872745E-3*s+2.06390887954E-2)*s-5.37397155531E-2)*s+1.33314422036E-1)*s-3.33332819422E-1)*s*x + x
  71. }
  72. return z
  73. }