1use super::log1p;
2
3/* atanh(x) = log((1+x)/(1-x))/2 = log1p(2x/(1-x))/2 ~= x + x^3/3 + o(x^5) */
4/// Inverse hyperbolic tangent (f64)
5///
6/// Calculates the inverse hyperbolic tangent of `x`.
7/// Is defined as `log((1+x)/(1-x))/2 = log1p(2x/(1-x))/2`.
8#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
9pub fn atanh(x: f64) -> f64 {
10 let u = x.to_bits();
11 let e = ((u >> 52) as usize) & 0x7ff;
12 let sign = (u >> 63) != 0;
13
14 /* |x| */
15 let mut y = f64::from_bits(u & 0x7fff_ffff_ffff_ffff);
16
17 if e < 0x3ff - 1 {
18 if e < 0x3ff - 32 {
19 /* handle underflow */
20 if e == 0 {
21 force_eval!(y as f32);
22 }
23 } else {
24 /* |x| < 0.5, up to 1.7ulp error */
25 y = 0.5 * log1p(2.0 * y + 2.0 * y * y / (1.0 - y));
26 }
27 } else {
28 /* avoid overflow */
29 y = 0.5 * log1p(2.0 * (y / (1.0 - y)));
30 }
31
32 if sign {
33 -y
34 } else {
35 y
36 }
37}
38