libm/math/
coshf.rs

1use super::{expf, expm1f, k_expo2f};
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 coshf(mut x: f32) -> f32 {
10    let x1p120 = f32::from_bits(0x7b800000); // 0x1p120f === 2 ^ 120
11
12    /* |x| */
13    let mut ix = x.to_bits();
14    ix &= 0x7fffffff;
15    x = f32::from_bits(ix);
16    let w = ix;
17
18    /* |x| < log(2) */
19    if w < 0x3f317217 {
20        if w < (0x3f800000 - (12 << 23)) {
21            force_eval!(x + x1p120);
22            return 1.;
23        }
24        let t = expm1f(x);
25        return 1. + t * t / (2. * (1. + t));
26    }
27
28    /* |x| < log(FLT_MAX) */
29    if w < 0x42b17217 {
30        let t = expf(x);
31        return 0.5 * (t + 1. / t);
32    }
33
34    /* |x| > log(FLT_MAX) or nan */
35    k_expo2f(x)
36}