1 | use super::exp; |
2 | use super::expm1; |
3 | use super::k_expo2; |
4 | |
5 | /// Hyperbolic cosine (f64) |
6 | /// |
7 | /// Computes the hyperbolic cosine of the argument x. |
8 | /// Is defined as `(exp(x) + exp(-x))/2` |
9 | /// Angles are specified in radians. |
10 | #[cfg_attr (all(test, assert_no_panic), no_panic::no_panic)] |
11 | pub fn cosh(mut x: f64) -> f64 { |
12 | /* |x| */ |
13 | let mut ix = x.to_bits(); |
14 | ix &= 0x7fffffffffffffff; |
15 | x = f64::from_bits(ix); |
16 | let w = ix >> 32; |
17 | |
18 | /* |x| < log(2) */ |
19 | if w < 0x3fe62e42 { |
20 | if w < 0x3ff00000 - (26 << 20) { |
21 | let x1p120 = f64::from_bits(0x4770000000000000); |
22 | force_eval!(x + x1p120); |
23 | return 1.; |
24 | } |
25 | let t = expm1(x); // exponential minus 1 |
26 | return 1. + t * t / (2. * (1. + t)); |
27 | } |
28 | |
29 | /* |x| < log(DBL_MAX) */ |
30 | if w < 0x40862e42 { |
31 | let t = exp(x); |
32 | /* note: if x>log(0x1p26) then the 1/t is not needed */ |
33 | return 0.5 * (t + 1. / t); |
34 | } |
35 | |
36 | /* |x| > log(DBL_MAX) or nan */ |
37 | k_expo2(x) |
38 | } |
39 | |