libm/math/
cosh.rs

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