Skip to content

Commit

Permalink
Rollup merge of #72486 - Ralith:asinh-fix, r=dtolnay
Browse files Browse the repository at this point in the history
Fix asinh of negative values

Rust's current implementation of asinh has [large errors](https://www.wolframalpha.com/input/?i=arcsinh%28x%29%2C+ln%28x%2B%28x%5E2%2B1%29%5E0.5%29%2C+x+from+-67452095.07139316+to+0) in its negative range. ~These are (mostly) not numerical, but rather seem due to an incorrect implementation.~ This appears to be due to avoidable catastrophic cancellation.
[Playground before/after](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=bd04ae6d86d06612e4e389a8b95d19ab).
[glibc uses](https://github.com/bminor/glibc/blob/81dca813cc35f91414731fdd0ff6b756d5e1827f/sysdeps/ieee754/dbl-64/s_asinh.c#L56) abs here.

Many thanks to @danieldeankon for finding this weird behavior, @jebrosen for diagnosing it, and @toasteater for identifying the probable implementation error!
  • Loading branch information
RalfJung authored Jun 19, 2020
2 parents 9c54c65 + 35a2915 commit 99be102
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
8 changes: 3 additions & 5 deletions src/libstd/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,11 +832,7 @@ impl f32 {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn asinh(self) -> f32 {
if self == Self::NEG_INFINITY {
Self::NEG_INFINITY
} else {
(self + ((self * self) + 1.0).sqrt()).ln().copysign(self)
}
(self.abs() + ((self * self) + 1.0).sqrt()).ln().copysign(self)
}

/// Inverse hyperbolic cosine function.
Expand Down Expand Up @@ -1413,6 +1409,8 @@ mod tests {
assert!((-0.0f32).asinh().is_sign_negative()); // issue 63271
assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
// regression test for the catastrophic cancellation fixed in 72486
assert_approx_eq!((-3000.0f32).asinh(), -8.699514775987968673236893537700647f32);
}

#[test]
Expand Down
8 changes: 3 additions & 5 deletions src/libstd/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,11 +834,7 @@ impl f64 {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn asinh(self) -> f64 {
if self == Self::NEG_INFINITY {
Self::NEG_INFINITY
} else {
(self + ((self * self) + 1.0).sqrt()).ln().copysign(self)
}
(self.abs() + ((self * self) + 1.0).sqrt()).ln().copysign(self)
}

/// Inverse hyperbolic cosine function.
Expand Down Expand Up @@ -1442,6 +1438,8 @@ mod tests {
// issue 63271
assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
// regression test for the catastrophic cancellation fixed in 72486
assert_approx_eq!((-67452098.07139316f64).asinh(), -18.72007542627454439398548429400083);
}

#[test]
Expand Down

0 comments on commit 99be102

Please sign in to comment.