summaryrefslogtreecommitdiff
path: root/src/math/atanh.c
blob: 84a84c69278b7ea8350b36292adb2ce9febab12b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "libm.h"

/* atanh(x) = log((1+x)/(1-x))/2 = log1p(2x/(1-x))/2 ~= x + x^3/3 + o(x^5) */
double atanh(double x)
{
	union {double f; uint64_t i;} u = {.f = x};
	unsigned e = u.i >> 52 & 0x7ff;
	unsigned s = u.i >> 63;

	/* |x| */
	u.i &= (uint64_t)-1/2;
	x = u.f;

	if (e < 0x3ff - 1) {
		/* |x| < 0.5, up to 1.7ulp error */
		x = 0.5*log1p(2*x + 2*x*x/(1-x));
	} else {
		x = 0.5*log1p(2*x/(1-x));
	}
	return s ? -x : x;
}