Skip to content

Enable Non-determinism of float operations in Miri and change std tests #138062

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

Merged
merged 2 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/core/src/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1879,7 +1879,7 @@ pub mod math {
///
/// let x = 2.0_f32;
/// let abs_difference = (f32::math::powi(x, 2) - (x * x)).abs();
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference <= 1e-5);
///
/// assert_eq!(f32::math::powi(f32::NAN, 0), 1.0);
/// ```
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1877,7 +1877,7 @@ pub mod math {
///
/// let x = 2.0_f64;
/// let abs_difference = (f64::math::powi(x, 2) - (x * x)).abs();
/// assert!(abs_difference <= f64::EPSILON);
/// assert!(abs_difference <= 1e-6);
///
/// assert_eq!(f64::math::powi(f64::NAN, 0), 1.0);
/// ```
Expand Down
9 changes: 7 additions & 2 deletions library/coretests/tests/floats/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const NAN_MASK1: u32 = 0x002a_aaaa;
/// Second pattern over the mantissa
const NAN_MASK2: u32 = 0x0055_5555;

/// Miri adds some extra errors to float functions; make sure the tests still pass.
/// These values are purely used as a canary to test against and are thus not a stable guarantee Rust provides.
/// They serve as a way to get an idea of the real precision of floating point operations on different platforms.
const APPROX_DELTA: f32 = if cfg!(miri) { 1e-4 } else { 1e-6 };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only use this once in this file, but it's the same reason as in library/std/tests/floats/f32.rs.


#[test]
fn test_num_f32() {
super::test_num(10f32, 2f32);
Expand Down Expand Up @@ -437,8 +442,8 @@ fn test_powi() {
let nan: f32 = f32::NAN;
let inf: f32 = f32::INFINITY;
let neg_inf: f32 = f32::NEG_INFINITY;
assert_biteq!(1.0f32.powi(1), 1.0);
assert_approx_eq!((-3.1f32).powi(2), 9.61);
assert_approx_eq!(1.0f32.powi(1), 1.0);
assert_approx_eq!((-3.1f32).powi(2), 9.61, APPROX_DELTA);
assert_approx_eq!(5.9f32.powi(-2), 0.028727);
assert_biteq!(8.3f32.powi(0), 1.0);
assert!(nan.powi(2).is_nan());
Expand Down
2 changes: 1 addition & 1 deletion library/coretests/tests/floats/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ fn test_powi() {
let nan: f64 = f64::NAN;
let inf: f64 = f64::INFINITY;
let neg_inf: f64 = f64::NEG_INFINITY;
assert_biteq!(1.0f64.powi(1), 1.0);
assert_approx_eq!(1.0f64.powi(1), 1.0);
assert_approx_eq!((-3.1f64).powi(2), 9.61);
assert_approx_eq!(5.9f64.powi(-2), 0.028727);
assert_biteq!(8.3f64.powi(0), 1.0);
Expand Down
6 changes: 4 additions & 2 deletions library/coretests/tests/num/dec2flt/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ fn test_f16_integer_decode() {
fn test_f32_integer_decode() {
assert_eq!(3.14159265359f32.integer_decode(), (13176795, -22, 1));
assert_eq!((-8573.5918555f32).integer_decode(), (8779358, -10, -1));
assert_eq!(2f32.powf(100.0).integer_decode(), (8388608, 77, 1));
// Set 2^100 directly instead of using powf, because it doesn't guarentee precision
assert_eq!(1.2676506e30_f32.integer_decode(), (8388608, 77, 1));
assert_eq!(0f32.integer_decode(), (0, -150, 1));
assert_eq!((-0f32).integer_decode(), (0, -150, -1));
assert_eq!(f32::INFINITY.integer_decode(), (8388608, 105, 1));
Expand All @@ -39,7 +40,8 @@ fn test_f32_integer_decode() {
fn test_f64_integer_decode() {
assert_eq!(3.14159265359f64.integer_decode(), (7074237752028906, -51, 1));
assert_eq!((-8573.5918555f64).integer_decode(), (4713381968463931, -39, -1));
assert_eq!(2f64.powf(100.0).integer_decode(), (4503599627370496, 48, 1));
// Set 2^100 directly instead of using powf, because it doesn't guarentee precision
assert_eq!(1.2676506002282294e30_f64.integer_decode(), (4503599627370496, 48, 1));
assert_eq!(0f64.integer_decode(), (0, -1075, 1));
assert_eq!((-0f64).integer_decode(), (0, -1075, -1));
assert_eq!(f64::INFINITY.integer_decode(), (4503599627370496, 972, 1));
Expand Down
34 changes: 17 additions & 17 deletions library/std/src/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ impl f32 {
/// ```
/// let x = 2.0_f32;
/// let abs_difference = (x.powi(2) - (x * x)).abs();
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference <= 1e-5);
///
/// assert_eq!(f32::powi(f32::NAN, 0), 1.0);
/// ```
Expand All @@ -328,7 +328,7 @@ impl f32 {
/// ```
/// let x = 2.0_f32;
/// let abs_difference = (x.powf(2.0) - (x * x)).abs();
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference <= 1e-5);
///
/// assert_eq!(f32::powf(1.0, f32::NAN), 1.0);
/// assert_eq!(f32::powf(f32::NAN, 0.0), 1.0);
Expand Down Expand Up @@ -388,7 +388,7 @@ impl f32 {
/// // ln(e) - 1 == 0
/// let abs_difference = (e.ln() - 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 All @@ -413,7 +413,7 @@ impl f32 {
/// // 2^2 - 4 == 0
/// let abs_difference = (f.exp2() - 4.0).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// 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 @@ -442,7 +442,7 @@ impl f32 {
/// // ln(e) - 1 == 0
/// let abs_difference = (e.ln() - 1.0).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference <= 1e-6);
/// ```
///
/// Non-positive values:
Expand Down Expand Up @@ -479,7 +479,7 @@ impl f32 {
/// // log5(5) - 1 == 0
/// let abs_difference = (five.log(5.0) - 1.0).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference <= 1e-6);
/// ```
///
/// Non-positive values:
Expand Down Expand Up @@ -512,7 +512,7 @@ impl f32 {
/// // log2(2) - 1 == 0
/// let abs_difference = (two.log2() - 1.0).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference <= 1e-6);
/// ```
///
/// Non-positive values:
Expand Down Expand Up @@ -545,7 +545,7 @@ impl f32 {
/// // log10(10) - 1 == 0
/// let abs_difference = (ten.log10() - 1.0).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference <= 1e-6);
/// ```
///
/// Non-positive values:
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 <= 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 All @@ -676,7 +676,7 @@ impl f32 {
///
/// let abs_difference = (x.sin() - 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 All @@ -700,7 +700,7 @@ impl f32 {
///
/// let abs_difference = (x.cos() - 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 @@ -754,7 +754,7 @@ impl f32 {
/// // asin(sin(pi/2))
/// let abs_difference = (f.sin().asin() - std::f32::consts::FRAC_PI_2).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference <= 1e-3);
/// ```
#[doc(alias = "arcsin")]
#[rustc_allow_incoherent_impl]
Expand Down Expand Up @@ -784,7 +784,7 @@ impl f32 {
/// // acos(cos(pi/4))
/// let abs_difference = (f.cos().acos() - std::f32::consts::FRAC_PI_4).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference <= 1e-6);
/// ```
#[doc(alias = "arccos")]
#[rustc_allow_incoherent_impl]
Expand Down Expand Up @@ -884,8 +884,8 @@ impl f32 {
/// let abs_difference_0 = (f.0 - x.sin()).abs();
/// let abs_difference_1 = (f.1 - x.cos()).abs();
///
/// assert!(abs_difference_0 <= f32::EPSILON);
/// assert!(abs_difference_1 <= f32::EPSILON);
/// assert!(abs_difference_0 <= 1e-6);
/// assert!(abs_difference_1 <= 1e-6);
/// ```
#[doc(alias = "sincos")]
#[rustc_allow_incoherent_impl]
Expand Down Expand Up @@ -1067,7 +1067,7 @@ impl f32 {
///
/// let abs_difference = (f - x).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference <= 1e-7);
/// ```
#[doc(alias = "arcsinh")]
#[rustc_allow_incoherent_impl]
Expand Down Expand Up @@ -1095,7 +1095,7 @@ impl f32 {
///
/// let abs_difference = (f - x).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference <= 1e-6);
/// ```
#[doc(alias = "arccosh")]
#[rustc_allow_incoherent_impl]
Expand Down
6 changes: 3 additions & 3 deletions library/std/src/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ impl f64 {
/// ```
/// let x = 2.0_f64;
/// let abs_difference = (x.powi(2) - (x * x)).abs();
/// assert!(abs_difference <= f64::EPSILON);
/// assert!(abs_difference <= 1e-14);
///
/// assert_eq!(f64::powi(f64::NAN, 0), 1.0);
/// ```
Expand All @@ -328,7 +328,7 @@ impl f64 {
/// ```
/// let x = 2.0_f64;
/// let abs_difference = (x.powf(2.0) - (x * x)).abs();
/// assert!(abs_difference <= f64::EPSILON);
/// assert!(abs_difference <= 1e-14);
///
/// assert_eq!(f64::powf(1.0, f64::NAN), 1.0);
/// assert_eq!(f64::powf(f64::NAN, 0.0), 1.0);
Expand Down Expand Up @@ -754,7 +754,7 @@ impl f64 {
/// // asin(sin(pi/2))
/// let abs_difference = (f.sin().asin() - std::f64::consts::FRAC_PI_2).abs();
///
/// assert!(abs_difference < 1e-10);
/// assert!(abs_difference < 1e-7);
/// ```
#[doc(alias = "arcsin")]
#[rustc_allow_incoherent_impl]
Expand Down
33 changes: 19 additions & 14 deletions library/std/tests/floats/f32.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use std::f32::consts;

/// Miri adds some extra errors to float functions; make sure the tests still pass.
/// These values are purely used as a canary to test against and are thus not a stable guarantee Rust provides.
/// They serve as a way to get an idea of the real precision of floating point operations on different platforms.
const APPROX_DELTA: f32 = if cfg!(miri) { 1e-3 } else { 1e-6 };

#[allow(unused_macros)]
macro_rules! assert_f32_biteq {
($left : expr, $right : expr) => {
Expand All @@ -17,9 +22,9 @@ fn test_powf() {
let inf: f32 = f32::INFINITY;
let neg_inf: f32 = f32::NEG_INFINITY;
assert_eq!(1.0f32.powf(1.0), 1.0);
assert_approx_eq!(3.4f32.powf(4.5), 246.408218);
assert_approx_eq!(3.4f32.powf(4.5), 246.408218, APPROX_DELTA);
assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
assert_approx_eq!((-3.1f32).powf(2.0), 9.61);
assert_approx_eq!((-3.1f32).powf(2.0), 9.61, APPROX_DELTA);
assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
assert_eq!(8.3f32.powf(0.0), 1.0);
assert!(nan.powf(2.0).is_nan());
Expand All @@ -30,8 +35,8 @@ fn test_powf() {
#[test]
fn test_exp() {
assert_eq!(1.0, 0.0f32.exp());
assert_approx_eq!(2.718282, 1.0f32.exp());
assert_approx_eq!(148.413162, 5.0f32.exp());
assert_approx_eq!(2.718282, 1.0f32.exp(), APPROX_DELTA);
assert_approx_eq!(148.413162, 5.0f32.exp(), APPROX_DELTA);

let inf: f32 = f32::INFINITY;
let neg_inf: f32 = f32::NEG_INFINITY;
Expand All @@ -43,7 +48,7 @@ fn test_exp() {

#[test]
fn test_exp2() {
assert_eq!(32.0, 5.0f32.exp2());
assert_approx_eq!(32.0, 5.0f32.exp2(), APPROX_DELTA);
assert_eq!(1.0, 0.0f32.exp2());

let inf: f32 = f32::INFINITY;
Expand All @@ -66,17 +71,17 @@ fn test_ln() {
assert!((-2.3f32).ln().is_nan());
assert_eq!((-0.0f32).ln(), neg_inf);
assert_eq!(0.0f32.ln(), neg_inf);
assert_approx_eq!(4.0f32.ln(), 1.386294);
assert_approx_eq!(4.0f32.ln(), 1.386294, APPROX_DELTA);
}

#[test]
fn test_log() {
let nan: f32 = f32::NAN;
let inf: f32 = f32::INFINITY;
let neg_inf: f32 = f32::NEG_INFINITY;
assert_eq!(10.0f32.log(10.0), 1.0);
assert_approx_eq!(10.0f32.log(10.0), 1.0);
assert_approx_eq!(2.3f32.log(3.5), 0.664858);
assert_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0);
assert_approx_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0, APPROX_DELTA);
assert!(1.0f32.log(1.0).is_nan());
assert!(1.0f32.log(-13.9).is_nan());
assert!(nan.log(2.3).is_nan());
Expand All @@ -92,9 +97,9 @@ fn test_log2() {
let nan: f32 = f32::NAN;
let inf: f32 = f32::INFINITY;
let neg_inf: f32 = f32::NEG_INFINITY;
assert_approx_eq!(10.0f32.log2(), 3.321928);
assert_approx_eq!(10.0f32.log2(), 3.321928, APPROX_DELTA);
assert_approx_eq!(2.3f32.log2(), 1.201634);
assert_approx_eq!(1.0f32.exp().log2(), 1.442695);
assert_approx_eq!(1.0f32.exp().log2(), 1.442695, APPROX_DELTA);
assert!(nan.log2().is_nan());
assert_eq!(inf.log2(), inf);
assert!(neg_inf.log2().is_nan());
Expand All @@ -108,7 +113,7 @@ fn test_log10() {
let nan: f32 = f32::NAN;
let inf: f32 = f32::INFINITY;
let neg_inf: f32 = f32::NEG_INFINITY;
assert_eq!(10.0f32.log10(), 1.0);
assert_approx_eq!(10.0f32.log10(), 1.0);
assert_approx_eq!(2.3f32.log10(), 0.361728);
assert_approx_eq!(1.0f32.exp().log10(), 0.434294);
assert_eq!(1.0f32.log10(), 0.0);
Expand Down Expand Up @@ -158,7 +163,7 @@ fn test_acosh() {
assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);

// test for low accuracy from issue 104548
assert_approx_eq!(60.0f32, 60.0f32.cosh().acosh());
assert_approx_eq!(60.0f32, 60.0f32.cosh().acosh(), APPROX_DELTA);
}

#[test]
Expand Down Expand Up @@ -237,7 +242,7 @@ fn test_real_consts() {
let ln_10: f32 = consts::LN_10;

assert_approx_eq!(frac_pi_2, pi / 2f32);
assert_approx_eq!(frac_pi_3, pi / 3f32);
assert_approx_eq!(frac_pi_3, pi / 3f32, APPROX_DELTA);
assert_approx_eq!(frac_pi_4, pi / 4f32);
assert_approx_eq!(frac_pi_6, pi / 6f32);
assert_approx_eq!(frac_pi_8, pi / 8f32);
Expand All @@ -249,5 +254,5 @@ fn test_real_consts() {
assert_approx_eq!(log2_e, e.log2());
assert_approx_eq!(log10_e, e.log10());
assert_approx_eq!(ln_2, 2f32.ln());
assert_approx_eq!(ln_10, 10f32.ln());
assert_approx_eq!(ln_10, 10f32.ln(), APPROX_DELTA);
}
8 changes: 4 additions & 4 deletions library/std/tests/floats/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn test_exp() {

#[test]
fn test_exp2() {
assert_eq!(32.0, 5.0f64.exp2());
assert_approx_eq!(32.0, 5.0f64.exp2());
assert_eq!(1.0, 0.0f64.exp2());

let inf: f64 = f64::INFINITY;
Expand Down Expand Up @@ -74,9 +74,9 @@ fn test_log() {
let nan: f64 = f64::NAN;
let inf: f64 = f64::INFINITY;
let neg_inf: f64 = f64::NEG_INFINITY;
assert_eq!(10.0f64.log(10.0), 1.0);
assert_approx_eq!(10.0f64.log(10.0), 1.0);
assert_approx_eq!(2.3f64.log(3.5), 0.664858);
assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
assert_approx_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
assert!(1.0f64.log(1.0).is_nan());
assert!(1.0f64.log(-13.9).is_nan());
assert!(nan.log(2.3).is_nan());
Expand Down Expand Up @@ -108,7 +108,7 @@ fn test_log10() {
let nan: f64 = f64::NAN;
let inf: f64 = f64::INFINITY;
let neg_inf: f64 = f64::NEG_INFINITY;
assert_eq!(10.0f64.log10(), 1.0);
assert_approx_eq!(10.0f64.log10(), 1.0);
assert_approx_eq!(2.3f64.log10(), 0.361728);
assert_approx_eq!(1.0f64.exp().log10(), 0.434294);
assert_eq!(1.0f64.log10(), 0.0);
Expand Down
Loading
Loading