Skip to content

Commit 4a60945

Browse files
Change stdlib float tests to account for miri nondet floats.
1 parent f391acb commit 4a60945

File tree

4 files changed

+49
-49
lines changed

4 files changed

+49
-49
lines changed

library/core/src/num/f32.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,7 @@ pub mod math {
18791879
///
18801880
/// let x = 2.0_f32;
18811881
/// let abs_difference = (f32::math::powi(x, 2) - (x * x)).abs();
1882-
/// assert!(abs_difference <= 1e-5);
1882+
/// assert!(abs_difference <= 1e-4);
18831883
///
18841884
/// assert_eq!(f32::math::powi(f32::NAN, 0), 1.0);
18851885
/// ```
@@ -1942,8 +1942,8 @@ pub mod math {
19421942
/// let abs_difference_x = (f32::math::abs_sub(x, 1.0) - 2.0).abs();
19431943
/// let abs_difference_y = (f32::math::abs_sub(y, 1.0) - 0.0).abs();
19441944
///
1945-
/// assert!(abs_difference_x <= f32::EPSILON);
1946-
/// assert!(abs_difference_y <= f32::EPSILON);
1945+
/// assert!(abs_difference_x <= 1e-4);
1946+
/// assert!(abs_difference_y <= 1e-4);
19471947
/// ```
19481948
///
19491949
/// _This standalone function is for testing only.
@@ -1988,7 +1988,7 @@ pub mod math {
19881988
/// // x^(1/3) - 2 == 0
19891989
/// let abs_difference = (f32::math::cbrt(x) - 2.0).abs();
19901990
///
1991-
/// assert!(abs_difference <= f32::EPSILON);
1991+
/// assert!(abs_difference <= 1e-4);
19921992
/// ```
19931993
///
19941994
/// _This standalone function is for testing only.

library/std/src/num/f32.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ impl f32 {
304304
/// ```
305305
/// let x = 2.0_f32;
306306
/// let abs_difference = (x.powi(2) - (x * x)).abs();
307-
/// assert!(abs_difference <= 1e-5);
307+
/// assert!(abs_difference <= 1e-4);
308308
///
309309
/// assert_eq!(f32::powi(f32::NAN, 0), 1.0);
310310
/// ```
@@ -328,7 +328,7 @@ impl f32 {
328328
/// ```
329329
/// let x = 2.0_f32;
330330
/// let abs_difference = (x.powf(2.0) - (x * x)).abs();
331-
/// assert!(abs_difference <= 1e-5);
331+
/// assert!(abs_difference <= 1e-4);
332332
///
333333
/// assert_eq!(f32::powf(1.0, f32::NAN), 1.0);
334334
/// assert_eq!(f32::powf(f32::NAN, 0.0), 1.0);
@@ -388,7 +388,7 @@ impl f32 {
388388
/// // ln(e) - 1 == 0
389389
/// let abs_difference = (e.ln() - 1.0).abs();
390390
///
391-
/// assert!(abs_difference <= 1e-6);
391+
/// assert!(abs_difference <= 1e-4);
392392
/// ```
393393
#[rustc_allow_incoherent_impl]
394394
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -413,7 +413,7 @@ impl f32 {
413413
/// // 2^2 - 4 == 0
414414
/// let abs_difference = (f.exp2() - 4.0).abs();
415415
///
416-
/// assert!(abs_difference <= 1e-5);
416+
/// assert!(abs_difference <= 1e-4);
417417
/// ```
418418
#[rustc_allow_incoherent_impl]
419419
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -442,7 +442,7 @@ impl f32 {
442442
/// // ln(e) - 1 == 0
443443
/// let abs_difference = (e.ln() - 1.0).abs();
444444
///
445-
/// assert!(abs_difference <= 1e-6);
445+
/// assert!(abs_difference <= 1e-4);
446446
/// ```
447447
///
448448
/// Non-positive values:
@@ -479,7 +479,7 @@ impl f32 {
479479
/// // log5(5) - 1 == 0
480480
/// let abs_difference = (five.log(5.0) - 1.0).abs();
481481
///
482-
/// assert!(abs_difference <= 1e-6);
482+
/// assert!(abs_difference <= 1e-4);
483483
/// ```
484484
///
485485
/// Non-positive values:
@@ -512,7 +512,7 @@ impl f32 {
512512
/// // log2(2) - 1 == 0
513513
/// let abs_difference = (two.log2() - 1.0).abs();
514514
///
515-
/// assert!(abs_difference <= 1e-6);
515+
/// assert!(abs_difference <= 1e-4);
516516
/// ```
517517
///
518518
/// Non-positive values:
@@ -545,7 +545,7 @@ impl f32 {
545545
/// // log10(10) - 1 == 0
546546
/// let abs_difference = (ten.log10() - 1.0).abs();
547547
///
548-
/// assert!(abs_difference <= 1e-6);
548+
/// assert!(abs_difference <= 1e-4);
549549
/// ```
550550
///
551551
/// Non-positive values:
@@ -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-4);
586+
/// assert!(abs_difference_y <= 1e-4);
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-4);
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-4);
656656
/// ```
657657
#[rustc_allow_incoherent_impl]
658658
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -676,7 +676,7 @@ impl f32 {
676676
///
677677
/// let abs_difference = (x.sin() - 1.0).abs();
678678
///
679-
/// assert!(abs_difference <= 1e-6);
679+
/// assert!(abs_difference <= 1e-4);
680680
/// ```
681681
#[rustc_allow_incoherent_impl]
682682
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -700,7 +700,7 @@ impl f32 {
700700
///
701701
/// let abs_difference = (x.cos() - 1.0).abs();
702702
///
703-
/// assert!(abs_difference <= 1e-6);
703+
/// assert!(abs_difference <= 1e-4);
704704
/// ```
705705
#[rustc_allow_incoherent_impl]
706706
#[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-4);
729729
/// ```
730730
#[rustc_allow_incoherent_impl]
731731
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -784,7 +784,7 @@ impl f32 {
784784
/// // acos(cos(pi/4))
785785
/// let abs_difference = (f.cos().acos() - std::f32::consts::FRAC_PI_4).abs();
786786
///
787-
/// assert!(abs_difference <= 1e-6);
787+
/// assert!(abs_difference <= 1e-4);
788788
/// ```
789789
#[doc(alias = "arccos")]
790790
#[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-4);
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-4);
858+
/// assert!(abs_difference_2 <= 1e-4);
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]
@@ -914,7 +914,7 @@ impl f32 {
914914
/// let approx = x + x * x / 2.0;
915915
/// let abs_difference = (x.exp_m1() - approx).abs();
916916
///
917-
/// assert!(abs_difference < 1e-10);
917+
/// assert!(abs_difference < 1e-4);
918918
/// ```
919919
#[rustc_allow_incoherent_impl]
920920
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -945,7 +945,7 @@ impl f32 {
945945
/// let approx = x - x * x / 2.0;
946946
/// let abs_difference = (x.ln_1p() - approx).abs();
947947
///
948-
/// assert!(abs_difference < 1e-10);
948+
/// assert!(abs_difference < 1e-4);
949949
/// ```
950950
///
951951
/// Out-of-range values:
@@ -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-4);
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-4);
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"]
@@ -1095,7 +1095,7 @@ impl f32 {
10951095
///
10961096
/// let abs_difference = (f - x).abs();
10971097
///
1098-
/// assert!(abs_difference <= 1e-6);
1098+
/// assert!(abs_difference <= 1e-5);
10991099
/// ```
11001100
#[doc(alias = "arccosh")]
11011101
#[rustc_allow_incoherent_impl]
@@ -1125,7 +1125,7 @@ impl f32 {
11251125
///
11261126
/// let abs_difference = (f - e).abs();
11271127
///
1128-
/// assert!(abs_difference <= 1e-5);
1128+
/// assert!(abs_difference <= 1e-4);
11291129
/// ```
11301130
#[doc(alias = "arctanh")]
11311131
#[rustc_allow_incoherent_impl]
@@ -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-4);
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-4);
12521252
/// ```
12531253
#[rustc_allow_incoherent_impl]
12541254
#[must_use = "method returns a new number and does not mutate the original value"]

library/std/src/num/f64.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ impl f64 {
304304
/// ```
305305
/// let x = 2.0_f64;
306306
/// let abs_difference = (x.powi(2) - (x * x)).abs();
307-
/// assert!(abs_difference <= 1e-14);
307+
/// assert!(abs_difference <= 1e-10);
308308
///
309309
/// assert_eq!(f64::powi(f64::NAN, 0), 1.0);
310310
/// ```
@@ -328,7 +328,7 @@ impl f64 {
328328
/// ```
329329
/// let x = 2.0_f64;
330330
/// let abs_difference = (x.powf(2.0) - (x * x)).abs();
331-
/// assert!(abs_difference <= 1e-14);
331+
/// assert!(abs_difference <= 1e-10);
332332
///
333333
/// assert_eq!(f64::powf(1.0, f64::NAN), 1.0);
334334
/// assert_eq!(f64::powf(f64::NAN, 0.0), 1.0);
@@ -725,7 +725,7 @@ impl f64 {
725725
/// let x = std::f64::consts::FRAC_PI_4;
726726
/// let abs_difference = (x.tan() - 1.0).abs();
727727
///
728-
/// assert!(abs_difference < 1e-14);
728+
/// assert!(abs_difference < 1e-10);
729729
/// ```
730730
#[rustc_allow_incoherent_impl]
731731
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -754,7 +754,7 @@ impl f64 {
754754
/// // asin(sin(pi/2))
755755
/// let abs_difference = (f.sin().asin() - std::f64::consts::FRAC_PI_2).abs();
756756
///
757-
/// assert!(abs_difference < 1e-7);
757+
/// assert!(abs_difference < 1e-5);
758758
/// ```
759759
#[doc(alias = "arcsin")]
760760
#[rustc_allow_incoherent_impl]
@@ -914,7 +914,7 @@ impl f64 {
914914
/// let approx = x + x * x / 2.0;
915915
/// let abs_difference = (x.exp_m1() - approx).abs();
916916
///
917-
/// assert!(abs_difference < 1e-20);
917+
/// assert!(abs_difference < 1e-10);
918918
/// ```
919919
#[rustc_allow_incoherent_impl]
920920
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -945,7 +945,7 @@ impl f64 {
945945
/// let approx = x - x * x / 2.0;
946946
/// let abs_difference = (x.ln_1p() - approx).abs();
947947
///
948-
/// assert!(abs_difference < 1e-20);
948+
/// assert!(abs_difference < 1e-10);
949949
/// ```
950950
///
951951
/// Out-of-range values:
@@ -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"]

library/std/tests/floats/f32.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ 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);
82+
assert_approx_eq!(10.0f32.log(10.0), 1.0, APPROX_DELTA);
8383
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());
@@ -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,10 +196,10 @@ 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());
202-
assert_approx_eq!((-0.5f32).gamma(), -2.0 * consts::PI.sqrt());
202+
assert_approx_eq!((-0.5f32).gamma(), -2.0 * consts::PI.sqrt(), APPROX_DELTA);
203203
assert_eq!(0.0f32.gamma(), f32::INFINITY);
204204
assert_eq!((-0.0f32).gamma(), f32::NEG_INFINITY);
205205
assert!((-1.0f32).gamma().is_nan());
@@ -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)