s_tanhl.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* @(#)s_tanh.c 5.1 93/09/24 */
  2. /*
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Developed at SunPro, a Sun Microsystems, Inc. business.
  7. * Permission to use, copy, modify, and distribute this
  8. * software is freely granted, provided that this notice
  9. * is preserved.
  10. * ====================================================
  11. */
  12. /*
  13. * Copyright (c) 2008 Stephen L. Moshier <[email protected]>
  14. *
  15. * Permission to use, copy, modify, and distribute this software for any
  16. * purpose with or without fee is hereby granted, provided that the above
  17. * copyright notice and this permission notice appear in all copies.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  20. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  22. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  23. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  24. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  25. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  26. */
  27. /* tanhl(x)
  28. * Return the Hyperbolic Tangent of x
  29. *
  30. * Method :
  31. * x -x
  32. * e - e
  33. * 0. tanhl(x) is defined to be -----------
  34. * x -x
  35. * e + e
  36. * 1. reduce x to non-negative by tanhl(-x) = -tanhl(x).
  37. * 2. 0 <= x <= 2**-57 : tanhl(x) := x*(one+x)
  38. * -t
  39. * 2**-57 < x <= 1 : tanhl(x) := -----; t = expm1l(-2x)
  40. * t + 2
  41. * 2
  42. * 1 <= x <= 40.0 : tanhl(x) := 1- ----- ; t=expm1l(2x)
  43. * t + 2
  44. * 40.0 < x <= INF : tanhl(x) := 1.
  45. *
  46. * Special cases:
  47. * tanhl(NaN) is NaN;
  48. * only tanhl(0)=0 is exact for finite argument.
  49. */
  50. #include <openlibm_math.h>
  51. #include "math_private.h"
  52. static const long double one = 1.0, two = 2.0, tiny = 1.0e-4900L;
  53. long double
  54. tanhl(long double x)
  55. {
  56. long double t, z;
  57. u_int32_t jx, ix;
  58. ieee_quad_shape_type u;
  59. /* Words of |x|. */
  60. u.value = x;
  61. jx = u.parts32.mswhi;
  62. ix = jx & 0x7fffffff;
  63. /* x is INF or NaN */
  64. if (ix >= 0x7fff0000)
  65. {
  66. /* for NaN it's not important which branch: tanhl(NaN) = NaN */
  67. if (jx & 0x80000000)
  68. return one / x - one; /* tanhl(-inf)= -1; */
  69. else
  70. return one / x + one; /* tanhl(+inf)=+1 */
  71. }
  72. /* |x| < 40 */
  73. if (ix < 0x40044000)
  74. {
  75. if (u.value == 0)
  76. return x; /* x == +- 0 */
  77. if (ix < 0x3fc60000) /* |x| < 2^-57 */
  78. return x * (one + tiny); /* tanh(small) = small */
  79. u.parts32.mswhi = ix; /* Absolute value of x. */
  80. if (ix >= 0x3fff0000)
  81. { /* |x| >= 1 */
  82. t = expm1l (two * u.value);
  83. z = one - two / (t + two);
  84. }
  85. else
  86. {
  87. t = expm1l (-two * u.value);
  88. z = -t / (t + two);
  89. }
  90. /* |x| > 40, return +-1 */
  91. }
  92. else
  93. {
  94. z = one - tiny; /* raised inexact flag */
  95. }
  96. return (jx & 0x80000000) ? -z : z;
  97. }