1 | use super::log1pf; |
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 (f32) |
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)] |
9 | pub fn atanhf(mut x: f32) -> f32 { |
10 | let mut u = x.to_bits(); |
11 | let sign = (u >> 31) != 0; |
12 | |
13 | /* |x| */ |
14 | u &= 0x7fffffff; |
15 | x = f32::from_bits(u); |
16 | |
17 | if u < 0x3f800000 - (1 << 23) { |
18 | if u < 0x3f800000 - (32 << 23) { |
19 | /* handle underflow */ |
20 | if u < (1 << 23) { |
21 | force_eval!((x * x) as f32); |
22 | } |
23 | } else { |
24 | /* |x| < 0.5, up to 1.7ulp error */ |
25 | x = 0.5 * log1pf(2.0 * x + 2.0 * x * x / (1.0 - x)); |
26 | } |
27 | } else { |
28 | /* avoid overflow */ |
29 | x = 0.5 * log1pf(2.0 * (x / (1.0 - x))); |
30 | } |
31 | |
32 | if sign { |
33 | -x |
34 | } else { |
35 | x |
36 | } |
37 | } |
38 | |