atanh.c 577 B

1234567891011121314151617181920212223242526272829
  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. double_t y;
  9. /* |x| */
  10. u.i &= (uint64_t)-1/2;
  11. y = u.f;
  12. if (e < 0x3ff - 1) {
  13. if (e < 0x3ff - 32) {
  14. /* handle underflow */
  15. if (e == 0)
  16. FORCE_EVAL((float)y);
  17. } else {
  18. /* |x| < 0.5, up to 1.7ulp error */
  19. y = 0.5*log1p(2*y + 2*y*y/(1-y));
  20. }
  21. } else {
  22. /* avoid overflow */
  23. y = 0.5*log1p(2*(y/(1-y)));
  24. }
  25. return s ? -y : y;
  26. }