/rust/registry/src/index.crates.io-6f17d22bba15001f/libm-0.2.11/src/math/atanh.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use 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)] |
9 | 0 | pub fn atanh(x: f64) -> f64 { |
10 | 0 | let u = x.to_bits(); |
11 | 0 | let e = ((u >> 52) as usize) & 0x7ff; |
12 | 0 | let sign = (u >> 63) != 0; |
13 | 0 |
|
14 | 0 | /* |x| */ |
15 | 0 | let mut y = f64::from_bits(u & 0x7fff_ffff_ffff_ffff); |
16 | 0 |
|
17 | 0 | if e < 0x3ff - 1 { |
18 | 0 | if e < 0x3ff - 32 { |
19 | | /* handle underflow */ |
20 | 0 | if e == 0 { |
21 | 0 | force_eval!(y as f32); |
22 | 0 | } |
23 | 0 | } else { |
24 | 0 | /* |x| < 0.5, up to 1.7ulp error */ |
25 | 0 | y = 0.5 * log1p(2.0 * y + 2.0 * y * y / (1.0 - y)); |
26 | 0 | } |
27 | 0 | } else { |
28 | 0 | /* avoid overflow */ |
29 | 0 | y = 0.5 * log1p(2.0 * (y / (1.0 - y))); |
30 | 0 | } |
31 | | |
32 | 0 | if sign { -y } else { y } |
33 | 0 | } |