trig_const/acosh.rs
1use crate::ln;
2
3use super::{log1p::log1p, sqrt};
4
5const LN2: f64 = 0.693147180559945309417232121458176568; /* 0x3fe62e42, 0xfefa39ef*/
6
7/// Inverse hyperbolic cosine (f64)
8///
9/// Calculates the inverse hyperbolic cosine of `x`.
10/// Is defined as `log(x + sqrt(x*x-1))`.
11/// `x` must be a number greater than or equal to 1.
12///
13/// ```
14/// # use trig_const::acosh;
15/// const ACOSH_1: f64 = acosh(1.0);
16/// assert_eq!(ACOSH_1, 0.0);
17/// ```
18pub const fn acosh(x: f64) -> f64 {
19 let u = x.to_bits();
20 let e = ((u >> 52) as usize) & 0x7ff;
21
22 /* x < 1 domain error is handled in the called functions */
23
24 if e < 0x3ff + 1 {
25 /* |x| < 2, up to 2ulp error in [1,1.125] */
26 return log1p(x - 1.0 + sqrt((x - 1.0) * (x - 1.0) + 2.0 * (x - 1.0)));
27 }
28 if e < 0x3ff + 26 {
29 /* |x| < 0x1p26 */
30 return ln(2.0 * x - 1.0 / (x + sqrt(x * x - 1.0)));
31 }
32 /* |x| >= 0x1p26 or nan */
33 ln(x) + LN2
34}