s_tanhl.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /* tanhl(x)
  13. * Return the Hyperbolic Tangent of x
  14. *
  15. * Method :
  16. * x -x
  17. * e - e
  18. * 0. tanhl(x) is defined to be -----------
  19. * x -x
  20. * e + e
  21. * 1. reduce x to non-negative by tanhl(-x) = -tanhl(x).
  22. * 2. 0 <= x <= 2**-55 : tanhl(x) := x*(one+x)
  23. * -t
  24. * 2**-55 < x <= 1 : tanhl(x) := -----; t = expm1l(-2x)
  25. * t + 2
  26. * 2
  27. * 1 <= x <= 23.0 : tanhl(x) := 1- ----- ; t=expm1l(2x)
  28. * t + 2
  29. * 23.0 < x <= INF : tanhl(x) := 1.
  30. *
  31. * Special cases:
  32. * tanhl(NaN) is NaN;
  33. * only tanhl(0)=0 is exact for finite argument.
  34. */
  35. #include <openlibm_math.h>
  36. #include "math_private.h"
  37. static const long double one=1.0, two=2.0, tiny = 1.0e-4900L;
  38. long double
  39. tanhl(long double x)
  40. {
  41. long double t,z;
  42. int32_t se;
  43. u_int32_t jj0,jj1,ix;
  44. /* High word of |x|. */
  45. GET_LDOUBLE_WORDS(se,jj0,jj1,x);
  46. ix = se&0x7fff;
  47. /* x is INF or NaN */
  48. if(ix==0x7fff) {
  49. /* for NaN it's not important which branch: tanhl(NaN) = NaN */
  50. if (se&0x8000) return one/x-one; /* tanhl(-inf)= -1; */
  51. else return one/x+one; /* tanhl(+inf)=+1 */
  52. }
  53. /* |x| < 23 */
  54. if (ix < 0x4003 || (ix == 0x4003 && jj0 < 0xb8000000u)) {/* |x|<23 */
  55. if ((ix|jj0|jj1) == 0)
  56. return x; /* x == +- 0 */
  57. if (ix<0x3fc8) /* |x|<2**-55 */
  58. return x*(one+tiny); /* tanh(small) = small */
  59. if (ix>=0x3fff) { /* |x|>=1 */
  60. t = expm1l(two*fabsl(x));
  61. z = one - two/(t+two);
  62. } else {
  63. t = expm1l(-two*fabsl(x));
  64. z= -t/(t+two);
  65. }
  66. /* |x| > 23, return +-1 */
  67. } else {
  68. z = one - tiny; /* raised inexact flag */
  69. }
  70. return (se&0x8000)? -z: z;
  71. }