Skip to content

Commit 2a160f5

Browse files
committed
Auto merge of rust-lang#146216 - LorrensP-2158466:miri-float-nondet-foreign-items-take2, r=RalfJung
Miri: non-deterministic floating point operations in foreign_items Take 2 of rust-lang#143906. The last 2 commits are what changed compared to the original pr. Verified the tests using (fish shell): ```fish env MIRIFLAGS="-Zmiri-max-extra-rounding-error -Zmiri-many-seeds" ./x miri --no-fail-fast std core coretests -- f32 f64 ``` r? `@RalfJung`
2 parents e6e32f1 + 88d1546 commit 2a160f5

File tree

6 files changed

+50
-50
lines changed

6 files changed

+50
-50
lines changed

core/src/num/f32.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,8 +1946,8 @@ pub mod math {
19461946
/// let abs_difference_x = (f32::math::abs_sub(x, 1.0) - 2.0).abs();
19471947
/// let abs_difference_y = (f32::math::abs_sub(y, 1.0) - 0.0).abs();
19481948
///
1949-
/// assert!(abs_difference_x <= f32::EPSILON);
1950-
/// assert!(abs_difference_y <= f32::EPSILON);
1949+
/// assert!(abs_difference_x <= 1e-6);
1950+
/// assert!(abs_difference_y <= 1e-6);
19511951
/// ```
19521952
///
19531953
/// _This standalone function is for testing only.
@@ -1992,7 +1992,7 @@ pub mod math {
19921992
/// // x^(1/3) - 2 == 0
19931993
/// let abs_difference = (f32::math::cbrt(x) - 2.0).abs();
19941994
///
1995-
/// assert!(abs_difference <= f32::EPSILON);
1995+
/// assert!(abs_difference <= 1e-6);
19961996
/// ```
19971997
///
19981998
/// _This standalone function is for testing only.

std/src/num/f128.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,10 @@ impl f128 {
467467
/// # #[cfg(not(miri))]
468468
/// # #[cfg(target_has_reliable_f128_math)] {
469469
///
470-
/// let f = std::f128::consts::FRAC_PI_2;
470+
/// let f = std::f128::consts::FRAC_PI_4;
471471
///
472472
/// // asin(sin(pi/2))
473-
/// let abs_difference = (f.sin().asin() - std::f128::consts::FRAC_PI_2).abs();
473+
/// let abs_difference = (f.sin().asin() - f).abs();
474474
///
475475
/// assert!(abs_difference <= f128::EPSILON);
476476
/// # }
@@ -912,10 +912,10 @@ impl f128 {
912912
/// # #[cfg(not(miri))]
913913
/// # #[cfg(target_has_reliable_f128_math)] {
914914
///
915-
/// let e = std::f128::consts::E;
916-
/// let f = e.tanh().atanh();
915+
/// let x = std::f128::consts::FRAC_PI_6;
916+
/// let f = x.tanh().atanh();
917917
///
918-
/// let abs_difference = (f - e).abs();
918+
/// let abs_difference = (f - x).abs();
919919
///
920920
/// assert!(abs_difference <= 1e-5);
921921
/// # }

std/src/num/f16.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,10 @@ impl f16 {
432432
/// # #[cfg(not(miri))]
433433
/// # #[cfg(target_has_reliable_f16_math)] {
434434
///
435-
/// let f = std::f16::consts::FRAC_PI_2;
435+
/// let f = std::f16::consts::FRAC_PI_4;
436436
///
437437
/// // asin(sin(pi/2))
438-
/// let abs_difference = (f.sin().asin() - std::f16::consts::FRAC_PI_2).abs();
438+
/// let abs_difference = (f.sin().asin() - f).abs();
439439
///
440440
/// assert!(abs_difference <= f16::EPSILON);
441441
/// # }
@@ -877,10 +877,10 @@ impl f16 {
877877
/// # #[cfg(not(miri))]
878878
/// # #[cfg(target_has_reliable_f16_math)] {
879879
///
880-
/// let e = std::f16::consts::E;
881-
/// let f = e.tanh().atanh();
880+
/// let x = std::f16::consts::FRAC_PI_6;
881+
/// let f = x.tanh().atanh();
882882
///
883-
/// let abs_difference = (f - e).abs();
883+
/// let abs_difference = (f - x).abs();
884884
///
885885
/// assert!(abs_difference <= 0.01);
886886
/// # }

std/src/num/f32.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,8 @@ impl f32 {
582582
/// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
583583
/// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
584584
///
585-
/// assert!(abs_difference_x <= f32::EPSILON);
586-
/// assert!(abs_difference_y <= f32::EPSILON);
585+
/// assert!(abs_difference_x <= 1e-6);
586+
/// assert!(abs_difference_y <= 1e-6);
587587
/// ```
588588
#[rustc_allow_incoherent_impl]
589589
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -621,7 +621,7 @@ impl f32 {
621621
/// // x^(1/3) - 2 == 0
622622
/// let abs_difference = (x.cbrt() - 2.0).abs();
623623
///
624-
/// assert!(abs_difference <= f32::EPSILON);
624+
/// assert!(abs_difference <= 1e-6);
625625
/// ```
626626
#[rustc_allow_incoherent_impl]
627627
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -652,7 +652,7 @@ impl f32 {
652652
/// // sqrt(x^2 + y^2)
653653
/// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
654654
///
655-
/// assert!(abs_difference <= 1e-6);
655+
/// assert!(abs_difference <= 1e-5);
656656
/// ```
657657
#[rustc_allow_incoherent_impl]
658658
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -725,7 +725,7 @@ impl f32 {
725725
/// let x = std::f32::consts::FRAC_PI_4;
726726
/// let abs_difference = (x.tan() - 1.0).abs();
727727
///
728-
/// assert!(abs_difference <= f32::EPSILON);
728+
/// assert!(abs_difference <= 1e-6);
729729
/// ```
730730
#[rustc_allow_incoherent_impl]
731731
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -749,12 +749,12 @@ impl f32 {
749749
/// # Examples
750750
///
751751
/// ```
752-
/// let f = std::f32::consts::FRAC_PI_2;
752+
/// let f = std::f32::consts::FRAC_PI_4;
753753
///
754754
/// // asin(sin(pi/2))
755-
/// let abs_difference = (f.sin().asin() - std::f32::consts::FRAC_PI_2).abs();
755+
/// let abs_difference = (f.sin().asin() - f).abs();
756756
///
757-
/// assert!(abs_difference <= 1e-3);
757+
/// assert!(abs_difference <= 1e-6);
758758
/// ```
759759
#[doc(alias = "arcsin")]
760760
#[rustc_allow_incoherent_impl]
@@ -813,7 +813,7 @@ impl f32 {
813813
/// // atan(tan(1))
814814
/// let abs_difference = (f.tan().atan() - 1.0).abs();
815815
///
816-
/// assert!(abs_difference <= f32::EPSILON);
816+
/// assert!(abs_difference <= 1e-6);
817817
/// ```
818818
#[doc(alias = "arctan")]
819819
#[rustc_allow_incoherent_impl]
@@ -854,8 +854,8 @@ impl f32 {
854854
/// let abs_difference_1 = (y1.atan2(x1) - (-std::f32::consts::FRAC_PI_4)).abs();
855855
/// let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f32::consts::FRAC_PI_4)).abs();
856856
///
857-
/// assert!(abs_difference_1 <= f32::EPSILON);
858-
/// assert!(abs_difference_2 <= f32::EPSILON);
857+
/// assert!(abs_difference_1 <= 1e-5);
858+
/// assert!(abs_difference_2 <= 1e-5);
859859
/// ```
860860
#[rustc_allow_incoherent_impl]
861861
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -884,8 +884,8 @@ impl f32 {
884884
/// let abs_difference_0 = (f.0 - x.sin()).abs();
885885
/// let abs_difference_1 = (f.1 - x.cos()).abs();
886886
///
887-
/// assert!(abs_difference_0 <= 1e-6);
888-
/// assert!(abs_difference_1 <= 1e-6);
887+
/// assert!(abs_difference_0 <= 1e-4);
888+
/// assert!(abs_difference_1 <= 1e-4);
889889
/// ```
890890
#[doc(alias = "sincos")]
891891
#[rustc_allow_incoherent_impl]
@@ -982,7 +982,7 @@ impl f32 {
982982
/// let g = ((e * e) - 1.0) / (2.0 * e);
983983
/// let abs_difference = (f - g).abs();
984984
///
985-
/// assert!(abs_difference <= f32::EPSILON);
985+
/// assert!(abs_difference <= 1e-6);
986986
/// ```
987987
#[rustc_allow_incoherent_impl]
988988
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -1012,7 +1012,7 @@ impl f32 {
10121012
/// let abs_difference = (f - g).abs();
10131013
///
10141014
/// // Same result
1015-
/// assert!(abs_difference <= f32::EPSILON);
1015+
/// assert!(abs_difference <= 1e-6);
10161016
/// ```
10171017
#[rustc_allow_incoherent_impl]
10181018
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -1042,7 +1042,7 @@ impl f32 {
10421042
/// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
10431043
/// let abs_difference = (f - g).abs();
10441044
///
1045-
/// assert!(abs_difference <= f32::EPSILON);
1045+
/// assert!(abs_difference <= 1e-6);
10461046
/// ```
10471047
#[rustc_allow_incoherent_impl]
10481048
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -1067,7 +1067,7 @@ impl f32 {
10671067
///
10681068
/// let abs_difference = (f - x).abs();
10691069
///
1070-
/// assert!(abs_difference <= 1e-7);
1070+
/// assert!(abs_difference <= 1e-6);
10711071
/// ```
10721072
#[doc(alias = "arcsinh")]
10731073
#[rustc_allow_incoherent_impl]
@@ -1120,10 +1120,10 @@ impl f32 {
11201120
/// # Examples
11211121
///
11221122
/// ```
1123-
/// let e = std::f32::consts::E;
1124-
/// let f = e.tanh().atanh();
1123+
/// let x = std::f32::consts::FRAC_PI_6;
1124+
/// let f = x.tanh().atanh();
11251125
///
1126-
/// let abs_difference = (f - e).abs();
1126+
/// let abs_difference = (f - x).abs();
11271127
///
11281128
/// assert!(abs_difference <= 1e-5);
11291129
/// ```
@@ -1153,7 +1153,7 @@ impl f32 {
11531153
///
11541154
/// let abs_difference = (x.gamma() - 24.0).abs();
11551155
///
1156-
/// assert!(abs_difference <= f32::EPSILON);
1156+
/// assert!(abs_difference <= 1e-5);
11571157
/// ```
11581158
#[rustc_allow_incoherent_impl]
11591159
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -1248,7 +1248,7 @@ impl f32 {
12481248
/// let one = x.erf() + x.erfc();
12491249
/// let abs_difference = (one - 1.0).abs();
12501250
///
1251-
/// assert!(abs_difference <= f32::EPSILON);
1251+
/// assert!(abs_difference <= 1e-6);
12521252
/// ```
12531253
#[rustc_allow_incoherent_impl]
12541254
#[must_use = "method returns a new number and does not mutate the original value"]

std/src/num/f64.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -749,12 +749,12 @@ impl f64 {
749749
/// # Examples
750750
///
751751
/// ```
752-
/// let f = std::f64::consts::FRAC_PI_2;
752+
/// let f = std::f64::consts::FRAC_PI_4;
753753
///
754754
/// // asin(sin(pi/2))
755-
/// let abs_difference = (f.sin().asin() - std::f64::consts::FRAC_PI_2).abs();
755+
/// let abs_difference = (f.sin().asin() - f).abs();
756756
///
757-
/// assert!(abs_difference < 1e-7);
757+
/// assert!(abs_difference < 1e-14);
758758
/// ```
759759
#[doc(alias = "arcsin")]
760760
#[rustc_allow_incoherent_impl]
@@ -1120,10 +1120,10 @@ impl f64 {
11201120
/// # Examples
11211121
///
11221122
/// ```
1123-
/// let e = std::f64::consts::E;
1124-
/// let f = e.tanh().atanh();
1123+
/// let x = std::f64::consts::FRAC_PI_6;
1124+
/// let f = x.tanh().atanh();
11251125
///
1126-
/// let abs_difference = (f - e).abs();
1126+
/// let abs_difference = (f - x).abs();
11271127
///
11281128
/// assert!(abs_difference < 1.0e-10);
11291129
/// ```
@@ -1153,7 +1153,7 @@ impl f64 {
11531153
///
11541154
/// let abs_difference = (x.gamma() - 24.0).abs();
11551155
///
1156-
/// assert!(abs_difference <= f64::EPSILON);
1156+
/// assert!(abs_difference <= 1e-10);
11571157
/// ```
11581158
#[rustc_allow_incoherent_impl]
11591159
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -1248,7 +1248,7 @@ impl f64 {
12481248
/// let one = x.erf() + x.erfc();
12491249
/// let abs_difference = (one - 1.0).abs();
12501250
///
1251-
/// assert!(abs_difference <= f64::EPSILON);
1251+
/// assert!(abs_difference <= 1e-10);
12521252
/// ```
12531253
#[rustc_allow_incoherent_impl]
12541254
#[must_use = "method returns a new number and does not mutate the original value"]

std/tests/floats/f32.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ fn test_log() {
7979
let nan: f32 = f32::NAN;
8080
let inf: f32 = f32::INFINITY;
8181
let neg_inf: f32 = f32::NEG_INFINITY;
82-
assert_approx_eq!(10.0f32.log(10.0), 1.0, APPROX_DELTA);
83-
assert_approx_eq!(2.3f32.log(3.5), 0.664858, APPROX_DELTA);
82+
assert_approx_eq!(10.0f32.log(10.0), 1.0);
83+
assert_approx_eq!(2.3f32.log(3.5), 0.664858);
8484
assert_approx_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0, APPROX_DELTA);
8585
assert!(1.0f32.log(1.0).is_nan());
8686
assert!(1.0f32.log(-13.9).is_nan());
@@ -140,10 +140,10 @@ fn test_asinh() {
140140
assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
141141
assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
142142
// regression test for the catastrophic cancellation fixed in 72486
143-
assert_approx_eq!((-3000.0f32).asinh(), -8.699514775987968673236893537700647f32);
143+
assert_approx_eq!((-3000.0f32).asinh(), -8.699514775987968673236893537700647f32, APPROX_DELTA);
144144

145145
// test for low accuracy from issue 104548
146-
assert_approx_eq!(60.0f32, 60.0f32.sinh().asinh());
146+
assert_approx_eq!(60.0f32, 60.0f32.sinh().asinh(), APPROX_DELTA);
147147
// mul needed for approximate comparison to be meaningful
148148
assert_approx_eq!(1.0f32, 1e-15f32.sinh().asinh() * 1e15f32);
149149
}
@@ -196,8 +196,8 @@ fn test_gamma() {
196196
assert_approx_eq!(1.0f32.gamma(), 1.0f32);
197197
assert_approx_eq!(2.0f32.gamma(), 1.0f32);
198198
assert_approx_eq!(3.0f32.gamma(), 2.0f32);
199-
assert_approx_eq!(4.0f32.gamma(), 6.0f32);
200-
assert_approx_eq!(5.0f32.gamma(), 24.0f32);
199+
assert_approx_eq!(4.0f32.gamma(), 6.0f32, APPROX_DELTA);
200+
assert_approx_eq!(5.0f32.gamma(), 24.0f32, APPROX_DELTA);
201201
assert_approx_eq!(0.5f32.gamma(), consts::PI.sqrt());
202202
assert_approx_eq!((-0.5f32).gamma(), -2.0 * consts::PI.sqrt());
203203
assert_eq!(0.0f32.gamma(), f32::INFINITY);
@@ -218,7 +218,7 @@ fn test_ln_gamma() {
218218
assert_eq!(2.0f32.ln_gamma().1, 1);
219219
assert_approx_eq!(3.0f32.ln_gamma().0, 2.0f32.ln());
220220
assert_eq!(3.0f32.ln_gamma().1, 1);
221-
assert_approx_eq!((-0.5f32).ln_gamma().0, (2.0 * consts::PI.sqrt()).ln());
221+
assert_approx_eq!((-0.5f32).ln_gamma().0, (2.0 * consts::PI.sqrt()).ln(), APPROX_DELTA);
222222
assert_eq!((-0.5f32).ln_gamma().1, -1);
223223
}
224224

0 commit comments

Comments
 (0)