1 /**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the mingw-w64 runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5 */
6 #include <math.h>
7 #include <errno.h>
8 #include "fastmath.h"
9
10 /* atanh (x) = 0.5 * log ((1.0 + x)/(1.0 - x)) */
11
atanh(double x)12 double atanh(double x)
13 {
14 double z;
15 if (isnan (x))
16 return x;
17 z = fabs (x);
18 if (z == 1.0)
19 {
20 errno = ERANGE;
21 return (x > 0 ? INFINITY : -INFINITY);
22 }
23 if (z > 1.0)
24 {
25 errno = EDOM;
26 return nan("");
27 }
28 /* Rearrange formula to avoid precision loss for small x.
29
30 atanh(x) = 0.5 * log ((1.0 + x)/(1.0 - x))
31 = 0.5 * log1p ((1.0 + x)/(1.0 - x) - 1.0)
32 = 0.5 * log1p ((1.0 + x - 1.0 + x) /(1.0 - x))
33 = 0.5 * log1p ((2.0 * x ) / (1.0 - x)) */
34 z = 0.5 * __fast_log1p ((z + z) / (1.0 - z));
35 return x >= 0 ? z : -z;
36 }
37