asinh.c 697 B

12345678910111213141516171819202122232425262728
  1. #include "libm.h"
  2. /* asinh(x) = sign(x)*log(|x|+sqrt(x*x+1)) ~= x - x^3/6 + o(x^5) */
  3. double asinh(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 + 26) {
  12. /* |x| >= 0x1p26 or inf or nan */
  13. x = log(x) + 0.693147180559945309417232121458176568;
  14. } else if (e >= 0x3ff + 1) {
  15. /* |x| >= 2 */
  16. x = log(2*x + 1/(sqrt(x*x+1)+x));
  17. } else if (e >= 0x3ff - 26) {
  18. /* |x| >= 0x1p-26, up to 1.6ulp error in [0.125,0.5] */
  19. x = log1p(x + x*x/(sqrt(x*x+1)+1));
  20. } else {
  21. /* |x| < 0x1p-26, raise inexact if x != 0 */
  22. FORCE_EVAL(x + 0x1p120f);
  23. }
  24. return s ? -x : x;
  25. }