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