/rust/registry/src/index.crates.io-1949cf8c6b5b557f/libm-0.2.15/src/math/tanhf.rs
Line | Count | Source |
1 | | use super::expm1f; |
2 | | |
3 | | /// The hyperbolic tangent of `x` (f32). |
4 | | /// |
5 | | /// `x` is specified in radians. |
6 | | #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] |
7 | 0 | pub fn tanhf(mut x: f32) -> f32 { |
8 | | /* x = |x| */ |
9 | 0 | let mut ix = x.to_bits(); |
10 | 0 | let sign = (ix >> 31) != 0; |
11 | 0 | ix &= 0x7fffffff; |
12 | 0 | x = f32::from_bits(ix); |
13 | 0 | let w = ix; |
14 | | |
15 | 0 | let tt = if w > 0x3f0c9f54 { |
16 | | /* |x| > log(3)/2 ~= 0.5493 or nan */ |
17 | 0 | if w > 0x41200000 { |
18 | | /* |x| > 10 */ |
19 | 0 | 1. + 0. / x |
20 | | } else { |
21 | 0 | let t = expm1f(2. * x); |
22 | 0 | 1. - 2. / (t + 2.) |
23 | | } |
24 | 0 | } else if w > 0x3e82c578 { |
25 | | /* |x| > log(5/3)/2 ~= 0.2554 */ |
26 | 0 | let t = expm1f(2. * x); |
27 | 0 | t / (t + 2.) |
28 | 0 | } else if w >= 0x00800000 { |
29 | | /* |x| >= 0x1p-126 */ |
30 | 0 | let t = expm1f(-2. * x); |
31 | 0 | -t / (t + 2.) |
32 | | } else { |
33 | | /* |x| is subnormal */ |
34 | 0 | force_eval!(x * x); |
35 | 0 | x |
36 | | }; |
37 | 0 | if sign { -tt } else { tt } |
38 | 0 | } |