cacoshq.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Return arc hyperbole cosine for __float128 value.
  2. Copyright (C) 1997, 1998, 2006 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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include "quadmath-imp.h"
  18. __complex128
  19. cacoshq (__complex128 x)
  20. {
  21. __complex128 res;
  22. int rcls = fpclassifyq (__real__ x);
  23. int icls = fpclassifyq (__imag__ x);
  24. if (rcls <= QUADFP_INFINITE || icls <= QUADFP_INFINITE)
  25. {
  26. if (icls == QUADFP_INFINITE)
  27. {
  28. __real__ res = HUGE_VALQ;
  29. if (rcls == QUADFP_NAN)
  30. __imag__ res = nanq ("");
  31. else
  32. __imag__ res = copysignq ((rcls == QUADFP_INFINITE
  33. ? (__real__ x < 0.0
  34. ? M_PIq - M_PI_4q : M_PI_4q)
  35. : M_PI_2q), __imag__ x);
  36. }
  37. else if (rcls == QUADFP_INFINITE)
  38. {
  39. __real__ res = HUGE_VALQ;
  40. if (icls >= QUADFP_ZERO)
  41. __imag__ res = copysignq (signbitq (__real__ x) ? M_PIq : 0.0,
  42. __imag__ x);
  43. else
  44. __imag__ res = nanq ("");
  45. }
  46. else
  47. {
  48. __real__ res = nanq ("");
  49. __imag__ res = nanq ("");
  50. }
  51. }
  52. else if (rcls == QUADFP_ZERO && icls == QUADFP_ZERO)
  53. {
  54. __real__ res = 0.0;
  55. __imag__ res = copysignq (M_PI_2q, __imag__ x);
  56. }
  57. /* The factor 16 is just a guess. */
  58. else if (16.0Q * fabsq (__imag__ x) < fabsq (__real__ x))
  59. {
  60. /* Kahan's formula which avoid cancellation through subtraction in
  61. some cases. */
  62. res = 2.0Q * clogq (csqrtq ((x + 1.0Q) / 2.0Q)
  63. + csqrtq ((x - 1.0Q) / 2.0Q));
  64. if (signbitq (__real__ res))
  65. __real__ res = 0.0Q;
  66. }
  67. else
  68. {
  69. __complex128 y;
  70. __real__ y = (__real__ x - __imag__ x) * (__real__ x + __imag__ x) - 1.0;
  71. __imag__ y = 2.0 * __real__ x * __imag__ x;
  72. y = csqrtq (y);
  73. if (signbitq (x))
  74. y = -y;
  75. __real__ y += __real__ x;
  76. __imag__ y += __imag__ x;
  77. res = clogq (y);
  78. }
  79. return res;
  80. }