atanh.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* origin: FreeBSD /usr/src/lib/msun/src/e_atanh.c */
  2. /*
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Developed at SunSoft, 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. /* atanh(x)
  14. * Method :
  15. * 1.Reduced x to positive by atanh(-x) = -atanh(x)
  16. * 2.For x>=0.5
  17. * 1 2x x
  18. * atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * --------)
  19. * 2 1 - x 1 - x
  20. *
  21. * For x<0.5
  22. * atanh(x) = 0.5*log1p(2x+2x*x/(1-x))
  23. *
  24. * Special cases:
  25. * atanh(x) is NaN if |x| > 1 with signal;
  26. * atanh(NaN) is that NaN with no signal;
  27. * atanh(+-1) is +-INF with signal.
  28. *
  29. */
  30. #include "libm.h"
  31. static const double huge = 1e300;
  32. double atanh(double x)
  33. {
  34. double t;
  35. int32_t hx,ix;
  36. uint32_t lx;
  37. EXTRACT_WORDS(hx, lx, x);
  38. ix = hx & 0x7fffffff;
  39. if ((ix | ((lx|-lx)>>31)) > 0x3ff00000) /* |x| > 1 */
  40. return (x-x)/(x-x);
  41. if (ix == 0x3ff00000)
  42. return x/0.0;
  43. if (ix < 0x3e300000 && (huge+x) > 0.0) /* x < 2**-28 */
  44. return x;
  45. SET_HIGH_WORD(x, ix);
  46. if (ix < 0x3fe00000) { /* x < 0.5 */
  47. t = x+x;
  48. t = 0.5*log1p(t + t*x/(1.0-x));
  49. } else
  50. t = 0.5*log1p((x+x)/(1.0-x));
  51. if (hx >= 0)
  52. return t;
  53. return -t;
  54. }