s_ctanh_template.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Complex hyperbolic tangent for float types.
  2. Copyright (C) 1997-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <complex.h>
  17. #include <fenv.h>
  18. #include <math.h>
  19. #include <math_private.h>
  20. #include <math-underflow.h>
  21. #include <float.h>
  22. CFLOAT
  23. M_DECL_FUNC (__ctanh) (CFLOAT x)
  24. {
  25. CFLOAT res;
  26. if (__glibc_unlikely (!isfinite (__real__ x) || !isfinite (__imag__ x)))
  27. {
  28. if (isinf (__real__ x))
  29. {
  30. __real__ res = M_COPYSIGN (1, __real__ x);
  31. if (isfinite (__imag__ x) && M_FABS (__imag__ x) > 1)
  32. {
  33. FLOAT sinix, cosix;
  34. M_SINCOS (__imag__ x, &sinix, &cosix);
  35. __imag__ res = M_COPYSIGN (0, sinix * cosix);
  36. }
  37. else
  38. __imag__ res = M_COPYSIGN (0, __imag__ x);
  39. }
  40. else if (__imag__ x == 0)
  41. {
  42. res = x;
  43. }
  44. else
  45. {
  46. if (__real__ x == 0)
  47. __real__ res = __real__ x;
  48. else
  49. __real__ res = M_NAN;
  50. __imag__ res = M_NAN;
  51. if (isinf (__imag__ x))
  52. feraiseexcept (FE_INVALID);
  53. }
  54. }
  55. else
  56. {
  57. FLOAT sinix, cosix;
  58. FLOAT den;
  59. const int t = (int) ((M_MAX_EXP - 1) * M_MLIT (M_LN2) / 2);
  60. /* tanh(x+iy) = (sinh(2x) + i*sin(2y))/(cosh(2x) + cos(2y))
  61. = (sinh(x)*cosh(x) + i*sin(y)*cos(y))/(sinh(x)^2 + cos(y)^2). */
  62. if (__glibc_likely (M_FABS (__imag__ x) > M_MIN))
  63. {
  64. M_SINCOS (__imag__ x, &sinix, &cosix);
  65. }
  66. else
  67. {
  68. sinix = __imag__ x;
  69. cosix = 1;
  70. }
  71. if (M_FABS (__real__ x) > t)
  72. {
  73. /* Avoid intermediate overflow when the imaginary part of
  74. the result may be subnormal. Ignoring negligible terms,
  75. the real part is +/- 1, the imaginary part is
  76. sin(y)*cos(y)/sinh(x)^2 = 4*sin(y)*cos(y)/exp(2x). */
  77. FLOAT exp_2t = M_EXP (2 * t);
  78. __real__ res = M_COPYSIGN (1, __real__ x);
  79. __imag__ res = 4 * sinix * cosix;
  80. __real__ x = M_FABS (__real__ x);
  81. __real__ x -= t;
  82. __imag__ res /= exp_2t;
  83. if (__real__ x > t)
  84. {
  85. /* Underflow (original real part of x has absolute value
  86. > 2t). */
  87. __imag__ res /= exp_2t;
  88. }
  89. else
  90. __imag__ res /= M_EXP (2 * __real__ x);
  91. }
  92. else
  93. {
  94. FLOAT sinhrx, coshrx;
  95. if (M_FABS (__real__ x) > M_MIN)
  96. {
  97. sinhrx = M_SINH (__real__ x);
  98. coshrx = M_COSH (__real__ x);
  99. }
  100. else
  101. {
  102. sinhrx = __real__ x;
  103. coshrx = 1;
  104. }
  105. if (M_FABS (sinhrx) > M_FABS (cosix) * M_EPSILON)
  106. den = sinhrx * sinhrx + cosix * cosix;
  107. else
  108. den = cosix * cosix;
  109. __real__ res = sinhrx * coshrx / den;
  110. __imag__ res = sinix * cosix / den;
  111. }
  112. math_check_force_underflow_complex (res);
  113. }
  114. return res;
  115. }
  116. declare_mgen_alias (__ctanh, ctanh)