/libm/upstream-freebsd/lib/msun/src/s_ctanh.c

https://gitlab.com/generic-library/bionic · C · 145 lines · 42 code · 13 blank · 90 comment · 5 complexity · cfc31227893ba7fa6e77c589f306b1a9 MD5 · raw file

  1. /*-
  2. * Copyright (c) 2011 David Schultz
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice unmodified, this list of conditions, and the following
  10. * disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /*
  27. * Hyperbolic tangent of a complex argument z = x + I y.
  28. *
  29. * The algorithm is from:
  30. *
  31. * W. Kahan. Branch Cuts for Complex Elementary Functions or Much
  32. * Ado About Nothing's Sign Bit. In The State of the Art in
  33. * Numerical Analysis, pp. 165 ff. Iserles and Powell, eds., 1987.
  34. *
  35. * Method:
  36. *
  37. * Let t = tan(x)
  38. * beta = 1/cos^2(y)
  39. * s = sinh(x)
  40. * rho = cosh(x)
  41. *
  42. * We have:
  43. *
  44. * tanh(z) = sinh(z) / cosh(z)
  45. *
  46. * sinh(x) cos(y) + I cosh(x) sin(y)
  47. * = ---------------------------------
  48. * cosh(x) cos(y) + I sinh(x) sin(y)
  49. *
  50. * cosh(x) sinh(x) / cos^2(y) + I tan(y)
  51. * = -------------------------------------
  52. * 1 + sinh^2(x) / cos^2(y)
  53. *
  54. * beta rho s + I t
  55. * = ----------------
  56. * 1 + beta s^2
  57. *
  58. * Modifications:
  59. *
  60. * I omitted the original algorithm's handling of overflow in tan(x) after
  61. * verifying with nearpi.c that this can't happen in IEEE single or double
  62. * precision. I also handle large x differently.
  63. */
  64. #include <sys/cdefs.h>
  65. __FBSDID("$FreeBSD: head/lib/msun/src/s_ctanh.c 284427 2015-06-15 20:40:44Z tijl $");
  66. #include <complex.h>
  67. #include <math.h>
  68. #include "math_private.h"
  69. double complex
  70. ctanh(double complex z)
  71. {
  72. double x, y;
  73. double t, beta, s, rho, denom;
  74. uint32_t hx, ix, lx;
  75. x = creal(z);
  76. y = cimag(z);
  77. EXTRACT_WORDS(hx, lx, x);
  78. ix = hx & 0x7fffffff;
  79. /*
  80. * ctanh(NaN +- I 0) = d(NaN) +- I 0
  81. *
  82. * ctanh(NaN + I y) = d(NaN,y) + I d(NaN,y) for y != 0
  83. *
  84. * The imaginary part has the sign of x*sin(2*y), but there's no
  85. * special effort to get this right.
  86. *
  87. * ctanh(+-Inf +- I Inf) = +-1 +- I 0
  88. *
  89. * ctanh(+-Inf + I y) = +-1 + I 0 sin(2y) for y finite
  90. *
  91. * The imaginary part of the sign is unspecified. This special
  92. * case is only needed to avoid a spurious invalid exception when
  93. * y is infinite.
  94. */
  95. if (ix >= 0x7ff00000) {
  96. if ((ix & 0xfffff) | lx) /* x is NaN */
  97. return (CMPLX((x + 0) * (y + 0),
  98. y == 0 ? y : (x + 0) * (y + 0)));
  99. SET_HIGH_WORD(x, hx - 0x40000000); /* x = copysign(1, x) */
  100. return (CMPLX(x, copysign(0, isinf(y) ? y : sin(y) * cos(y))));
  101. }
  102. /*
  103. * ctanh(x + I NaN) = d(NaN) + I d(NaN)
  104. * ctanh(x +- I Inf) = dNaN + I dNaN
  105. */
  106. if (!isfinite(y))
  107. return (CMPLX(y - y, y - y));
  108. /*
  109. * ctanh(+-huge +- I y) ~= +-1 +- I 2sin(2y)/exp(2x), using the
  110. * approximation sinh^2(huge) ~= exp(2*huge) / 4.
  111. * We use a modified formula to avoid spurious overflow.
  112. */
  113. if (ix >= 0x40360000) { /* |x| >= 22 */
  114. double exp_mx = exp(-fabs(x));
  115. return (CMPLX(copysign(1, x),
  116. 4 * sin(y) * cos(y) * exp_mx * exp_mx));
  117. }
  118. /* Kahan's algorithm */
  119. t = tan(y);
  120. beta = 1.0 + t * t; /* = 1 / cos^2(y) */
  121. s = sinh(x);
  122. rho = sqrt(1 + s * s); /* = cosh(x) */
  123. denom = 1 + beta * s * s;
  124. return (CMPLX((beta * rho * s) / denom, t / denom));
  125. }
  126. double complex
  127. ctan(double complex z)
  128. {
  129. /* ctan(z) = -I * ctanh(I * z) = I * conj(ctanh(I * conj(z))) */
  130. z = ctanh(CMPLX(cimag(z), creal(z)));
  131. return (CMPLX(cimag(z), creal(z)));
  132. }