Skip to content

Miri: non-deterministic floating point operations in foreign_items #143906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
6 changes: 3 additions & 3 deletions library/core/src/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1942,8 +1942,8 @@ pub mod math {
/// let abs_difference_x = (f32::math::abs_sub(x, 1.0) - 2.0).abs();
/// let abs_difference_y = (f32::math::abs_sub(y, 1.0) - 0.0).abs();
///
/// assert!(abs_difference_x <= f32::EPSILON);
/// assert!(abs_difference_y <= f32::EPSILON);
/// assert!(abs_difference_x <= 1e-6);
/// assert!(abs_difference_y <= 1e-6);
/// ```
///
/// _This standalone function is for testing only.
Expand Down Expand Up @@ -1988,7 +1988,7 @@ pub mod math {
/// // x^(1/3) - 2 == 0
/// let abs_difference = (f32::math::cbrt(x) - 2.0).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference <= 1e-6);
/// ```
///
/// _This standalone function is for testing only.
Expand Down
30 changes: 15 additions & 15 deletions library/std/src/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,8 @@ impl f32 {
/// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
/// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
///
/// assert!(abs_difference_x <= f32::EPSILON);
/// assert!(abs_difference_y <= f32::EPSILON);
/// assert!(abs_difference_x <= 1e-6);
/// assert!(abs_difference_y <= 1e-6);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
Expand Down Expand Up @@ -621,7 +621,7 @@ impl f32 {
/// // x^(1/3) - 2 == 0
/// let abs_difference = (x.cbrt() - 2.0).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference <= 1e-6);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
Expand Down Expand Up @@ -652,7 +652,7 @@ impl f32 {
/// // sqrt(x^2 + y^2)
/// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
///
/// assert!(abs_difference <= 1e-6);
/// assert!(abs_difference <= 1e-5);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
Expand Down Expand Up @@ -725,7 +725,7 @@ impl f32 {
/// let x = std::f32::consts::FRAC_PI_4;
/// let abs_difference = (x.tan() - 1.0).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference <= 1e-6);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
Expand Down Expand Up @@ -813,7 +813,7 @@ impl f32 {
/// // atan(tan(1))
/// let abs_difference = (f.tan().atan() - 1.0).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference <= 1e-6);
/// ```
#[doc(alias = "arctan")]
#[rustc_allow_incoherent_impl]
Expand Down Expand Up @@ -854,8 +854,8 @@ impl f32 {
/// let abs_difference_1 = (y1.atan2(x1) - (-std::f32::consts::FRAC_PI_4)).abs();
/// let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f32::consts::FRAC_PI_4)).abs();
///
/// assert!(abs_difference_1 <= f32::EPSILON);
/// assert!(abs_difference_2 <= f32::EPSILON);
/// assert!(abs_difference_1 <= 1e-5);
/// assert!(abs_difference_2 <= 1e-5);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
Expand Down Expand Up @@ -982,7 +982,7 @@ impl f32 {
/// let g = ((e * e) - 1.0) / (2.0 * e);
/// let abs_difference = (f - g).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference <= 1e-6);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
Expand Down Expand Up @@ -1012,7 +1012,7 @@ impl f32 {
/// let abs_difference = (f - g).abs();
///
/// // Same result
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference <= 1e-6);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
Expand Down Expand Up @@ -1042,7 +1042,7 @@ impl f32 {
/// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
/// let abs_difference = (f - g).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference <= 1e-6);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
Expand Down Expand Up @@ -1095,7 +1095,7 @@ impl f32 {
///
/// let abs_difference = (f - x).abs();
///
/// assert!(abs_difference <= 1e-6);
/// assert!(abs_difference <= 1e-5);
/// ```
#[doc(alias = "arccosh")]
#[rustc_allow_incoherent_impl]
Expand Down Expand Up @@ -1125,7 +1125,7 @@ impl f32 {
///
/// let abs_difference = (f - e).abs();
///
/// assert!(abs_difference <= 1e-5);
/// assert!(abs_difference <= 1e-4);
/// ```
#[doc(alias = "arctanh")]
#[rustc_allow_incoherent_impl]
Expand Down Expand Up @@ -1153,7 +1153,7 @@ impl f32 {
///
/// let abs_difference = (x.gamma() - 24.0).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference <= 1e-4);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
Expand Down Expand Up @@ -1248,7 +1248,7 @@ impl f32 {
/// let one = x.erf() + x.erfc();
/// let abs_difference = (one - 1.0).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference <= 1e-6);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,7 @@ impl f64 {
///
/// let abs_difference = (x.gamma() - 24.0).abs();
///
/// assert!(abs_difference <= f64::EPSILON);
/// assert!(abs_difference <= 1e-13);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
Expand Down Expand Up @@ -1248,7 +1248,7 @@ impl f64 {
/// let one = x.erf() + x.erfc();
/// let abs_difference = (one - 1.0).abs();
///
/// assert!(abs_difference <= f64::EPSILON);
/// assert!(abs_difference <= 1e-13);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
Expand Down
14 changes: 7 additions & 7 deletions library/std/tests/floats/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn test_log() {
let nan: f32 = f32::NAN;
let inf: f32 = f32::INFINITY;
let neg_inf: f32 = f32::NEG_INFINITY;
assert_approx_eq!(10.0f32.log(10.0), 1.0);
assert_approx_eq!(10.0f32.log(10.0), 1.0, APPROX_DELTA);
assert_approx_eq!(2.3f32.log(3.5), 0.664858);
assert_approx_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0, APPROX_DELTA);
assert!(1.0f32.log(1.0).is_nan());
Expand Down Expand Up @@ -140,10 +140,10 @@ fn test_asinh() {
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);
assert_approx_eq!((-3000.0f32).asinh(), -8.699514775987968673236893537700647f32, APPROX_DELTA);

// test for low accuracy from issue 104548
assert_approx_eq!(60.0f32, 60.0f32.sinh().asinh());
assert_approx_eq!(60.0f32, 60.0f32.sinh().asinh(), APPROX_DELTA);
// mul needed for approximate comparison to be meaningful
assert_approx_eq!(1.0f32, 1e-15f32.sinh().asinh() * 1e15f32);
}
Expand Down Expand Up @@ -196,10 +196,10 @@ fn test_gamma() {
assert_approx_eq!(1.0f32.gamma(), 1.0f32);
assert_approx_eq!(2.0f32.gamma(), 1.0f32);
assert_approx_eq!(3.0f32.gamma(), 2.0f32);
assert_approx_eq!(4.0f32.gamma(), 6.0f32);
assert_approx_eq!(5.0f32.gamma(), 24.0f32);
assert_approx_eq!(4.0f32.gamma(), 6.0f32, APPROX_DELTA);
assert_approx_eq!(5.0f32.gamma(), 24.0f32, APPROX_DELTA);
assert_approx_eq!(0.5f32.gamma(), consts::PI.sqrt());
assert_approx_eq!((-0.5f32).gamma(), -2.0 * consts::PI.sqrt());
assert_approx_eq!((-0.5f32).gamma(), -2.0 * consts::PI.sqrt(), APPROX_DELTA);
assert_eq!(0.0f32.gamma(), f32::INFINITY);
assert_eq!((-0.0f32).gamma(), f32::NEG_INFINITY);
assert!((-1.0f32).gamma().is_nan());
Expand All @@ -218,7 +218,7 @@ fn test_ln_gamma() {
assert_eq!(2.0f32.ln_gamma().1, 1);
assert_approx_eq!(3.0f32.ln_gamma().0, 2.0f32.ln());
assert_eq!(3.0f32.ln_gamma().1, 1);
assert_approx_eq!((-0.5f32).ln_gamma().0, (2.0 * consts::PI.sqrt()).ln());
assert_approx_eq!((-0.5f32).ln_gamma().0, (2.0 * consts::PI.sqrt()).ln(), APPROX_DELTA);
assert_eq!((-0.5f32).ln_gamma().1, -1);
}

Expand Down
Loading
Loading