trig_const/asinh.rs
1use crate::{ln, log1p::log1p, sqrt};
2
3const LN2: f64 = 0.693147180559945309417232121458176568; /* 0x3fe62e42, 0xfefa39ef*/
4
5/* asinh(x) = sign(x)*log(|x|+sqrt(x*x+1)) ~= x - x^3/6 + o(x^5) */
6/// Inverse hyperbolic sine (f64)
7///
8/// Calculates the inverse hyperbolic sine of `x`.
9/// Is defined as `sgn(x)*log(|x|+sqrt(x*x+1))`.
10/// ```
11/// # use trig_const::asinh;
12/// const ASINH_1: f64 = asinh(0.0);
13/// assert_eq!(ASINH_1, 0.0);
14/// ```
15pub const fn asinh(mut x: f64) -> f64 {
16 let mut u = x.to_bits();
17 let e = ((u >> 52) as usize) & 0x7ff;
18 let sign = (u >> 63) != 0;
19
20 /* |x| */
21 u &= (!0) >> 1;
22 x = f64::from_bits(u);
23
24 if e >= 0x3ff + 26 {
25 /* |x| >= 0x1p26 or inf or nan */
26 x = ln(x) + LN2;
27 } else if e > 0x3ff {
28 /* |x| >= 2 */
29 x = ln(2.0 * x + 1.0 / (sqrt(x * x + 1.0) + x));
30 } else if e >= 0x3ff - 26 {
31 /* |x| >= 0x1p-26, up to 1.6ulp error in [0.125,0.5] */
32 x = log1p(x + x * x / (sqrt(x * x + 1.0) + 1.0));
33 } else {
34 /* |x| < 0x1p-26, raise inexact if x != 0 */
35 }
36
37 if sign {
38 -x
39 } else {
40 x
41 }
42}