atanh.c 431 B

123456789101112131415161718192021
  1. #include "libm.h"
  2. /* atanh(x) = log((1+x)/(1-x))/2 = log1p(2x/(1-x))/2 ~= x + x^3/3 + o(x^5) */
  3. double atanh(double x)
  4. {
  5. union {double f; uint64_t i;} u = {.f = x};
  6. unsigned e = u.i >> 52 & 0x7ff;
  7. unsigned s = u.i >> 63;
  8. /* |x| */
  9. u.i &= (uint64_t)-1/2;
  10. x = u.f;
  11. if (e < 0x3ff - 1) {
  12. /* |x| < 0.5, up to 1.7ulp error */
  13. x = 0.5*log1p(2*x + 2*x*x/(1-x));
  14. } else {
  15. x = 0.5*log1p(2*x/(1-x));
  16. }
  17. return s ? -x : x;
  18. }