| 1 | use super::{log, log1p, sqrt}; |
| 2 | |
| 3 | const 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 | #[cfg_attr (all(test, assert_no_panic), no_panic::no_panic)] |
| 11 | pub fn asinh(mut x: f64) -> f64 { |
| 12 | let mut u = x.to_bits(); |
| 13 | let e = ((u >> 52) as usize) & 0x7ff; |
| 14 | let sign = (u >> 63) != 0; |
| 15 | |
| 16 | /* |x| */ |
| 17 | u &= (!0) >> 1; |
| 18 | x = f64::from_bits(u); |
| 19 | |
| 20 | if e >= 0x3ff + 26 { |
| 21 | /* |x| >= 0x1p26 or inf or nan */ |
| 22 | x = log(x) + LN2; |
| 23 | } else if e >= 0x3ff + 1 { |
| 24 | /* |x| >= 2 */ |
| 25 | x = log(2.0 * x + 1.0 / (sqrt(x * x + 1.0) + x)); |
| 26 | } else if e >= 0x3ff - 26 { |
| 27 | /* |x| >= 0x1p-26, up to 1.6ulp error in [0.125,0.5] */ |
| 28 | x = log1p(x + x * x / (sqrt(x * x + 1.0) + 1.0)); |
| 29 | } else { |
| 30 | /* |x| < 0x1p-26, raise inexact if x != 0 */ |
| 31 | let x1p120 = f64::from_bits(0x4770000000000000); |
| 32 | force_eval!(x + x1p120); |
| 33 | } |
| 34 | |
| 35 | if sign { -x } else { x } |
| 36 | } |
| 37 | |