math: rewrite inverse hyperbolic functions to be simpler/smaller
[musl] / src / math / acosh.c
1 #include "libm.h"
2
3 /* acosh(x) = log(x + sqrt(x*x-1)) */
4 double acosh(double x)
5 {
6         union {double f; uint64_t i;} u = {.f = x};
7         unsigned e = u.i >> 52 & 0x7ff;
8
9         /* x < 1 domain error is handled in the called functions */
10
11         if (e < 0x3ff + 1)
12                 /* |x| < 2, up to 2ulp error in [1,1.125] */
13                 return log1p(x-1 + sqrt((x-1)*(x-1)+2*(x-1)));
14         if (e < 0x3ff + 26)
15                 /* |x| < 0x1p26 */
16                 return log(2*x - 1/(x+sqrt(x*x-1)));
17         /* |x| >= 0x1p26 or nan */
18         return log(x) + 0.693147180559945309417232121458176568;
19 }