1
Fork 0

Improve accuracy of asinh and acosh

This commit is contained in:
Max Willsey 2022-11-17 12:50:33 -08:00
parent b6097f2e1b
commit 26b23f4a37
4 changed files with 32 additions and 4 deletions

View file

@ -882,7 +882,9 @@ impl f64 {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn asinh(self) -> f64 {
(self.abs() + ((self * self) + 1.0).sqrt()).ln().copysign(self)
let ax = self.abs();
let ix = 1.0 / ax;
(ax + (ax / (Self::hypot(1.0, ix) + ix))).ln_1p().copysign(self)
}
/// Inverse hyperbolic cosine function.
@ -902,7 +904,11 @@ impl f64 {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn acosh(self) -> f64 {
if self < 1.0 { Self::NAN } else { (self + ((self * self) - 1.0).sqrt()).ln() }
if self < 1.0 {
Self::NAN
} else {
(self + ((self - 1.0).sqrt() * (self + 1.0).sqrt())).ln()
}
}
/// Inverse hyperbolic tangent function.