Skip to content

Commit 00452bd

Browse files
change tests to use fixed constants to let them pass with miri
1 parent 5bafe9d commit 00452bd

File tree

7 files changed

+44
-77
lines changed

7 files changed

+44
-77
lines changed

library/core/src/num/f32.rs

Lines changed: 1 addition & 1 deletion
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 <= f32::EPSILON);
1882+
/// assert!(abs_difference <= 1e-5);
18831883
///
18841884
/// assert_eq!(f32::math::powi(f32::NAN, 0), 1.0);
18851885
/// ```

library/core/src/num/f64.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1877,7 +1877,7 @@ pub mod math {
18771877
///
18781878
/// let x = 2.0_f64;
18791879
/// let abs_difference = (f64::math::powi(x, 2) - (x * x)).abs();
1880-
/// assert!(abs_difference <= f64::EPSILON);
1880+
/// assert!(abs_difference <= 1e-6);
18811881
///
18821882
/// assert_eq!(f64::math::powi(f64::NAN, 0), 1.0);
18831883
/// ```

library/coretests/tests/floats/f32.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ const NAN_MASK1: u32 = 0x002a_aaaa;
2323
/// Second pattern over the mantissa
2424
const NAN_MASK2: u32 = 0x0055_5555;
2525

26+
/// Miri adds some extra errors to float functions; make sure the tests still pass.
27+
/// These values are purely used as a canary to test against and are thus not a stable guarantee Rust provides.
28+
/// They serve as a way to get an idea of the real precision of floating point operations on different platforms.
29+
const APPROX_DELTA: f32 = if cfg!(miri) { 1e-4 } else { 1e-6 };
30+
2631
#[test]
2732
fn test_num_f32() {
2833
super::test_num(10f32, 2f32);
@@ -437,8 +442,8 @@ fn test_powi() {
437442
let nan: f32 = f32::NAN;
438443
let inf: f32 = f32::INFINITY;
439444
let neg_inf: f32 = f32::NEG_INFINITY;
440-
assert_biteq!(1.0f32.powi(1), 1.0);
441-
assert_approx_eq!((-3.1f32).powi(2), 9.61);
445+
assert_approx_eq!(1.0f32.powi(1), 1.0);
446+
assert_approx_eq!((-3.1f32).powi(2), 9.61, APPROX_DELTA);
442447
assert_approx_eq!(5.9f32.powi(-2), 0.028727);
443448
assert_biteq!(8.3f32.powi(0), 1.0);
444449
assert!(nan.powi(2).is_nan());

library/std/src/num/f32.rs

Lines changed: 17 additions & 17 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 <= 8.0 * f32::EPSILON);
307+
/// assert!(abs_difference <= 1e-5);
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 <= 8.0 * f32::EPSILON);
331+
/// assert!(abs_difference <= 1e-5);
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 <= 4.0 * f32::EPSILON);
391+
/// assert!(abs_difference <= 1e-6);
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 <= 8.0 * f32::EPSILON);
416+
/// assert!(abs_difference <= 1e-5);
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 <= 4.0 * f32::EPSILON);
445+
/// assert!(abs_difference <= 1e-6);
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 <= 4.0 * f32::EPSILON);
482+
/// assert!(abs_difference <= 1e-6);
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 <= 4.0 * f32::EPSILON);
515+
/// assert!(abs_difference <= 1e-6);
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 <= 4.0 * f32::EPSILON);
548+
/// assert!(abs_difference <= 1e-6);
549549
/// ```
550550
///
551551
/// Non-positive values:
@@ -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 <= 4.0 * f32::EPSILON);
655+
/// assert!(abs_difference <= 1e-6);
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 <= 4.0 * f32::EPSILON);
679+
/// assert!(abs_difference <= 1e-6);
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 <= 4.0 * f32::EPSILON);
703+
/// assert!(abs_difference <= 1e-6);
704704
/// ```
705705
#[rustc_allow_incoherent_impl]
706706
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -754,7 +754,7 @@ impl f32 {
754754
/// // asin(sin(pi/2))
755755
/// let abs_difference = (f.sin().asin() - std::f32::consts::FRAC_PI_2).abs();
756756
///
757-
/// assert!(abs_difference <= f32::EPSILON);
757+
/// assert!(abs_difference <= 1e-3);
758758
/// ```
759759
#[doc(alias = "arcsin")]
760760
#[rustc_allow_incoherent_impl]
@@ -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 <= 4.0 * f32::EPSILON);
787+
/// assert!(abs_difference <= 1e-6);
788788
/// ```
789789
#[doc(alias = "arccos")]
790790
#[rustc_allow_incoherent_impl]
@@ -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 <= 4.0 * f32::EPSILON);
888-
/// assert!(abs_difference_1 <= 4.0 * f32::EPSILON);
887+
/// assert!(abs_difference_0 <= 1e-6);
888+
/// assert!(abs_difference_1 <= 1e-6);
889889
/// ```
890890
#[doc(alias = "sincos")]
891891
#[rustc_allow_incoherent_impl]
@@ -1067,7 +1067,7 @@ impl f32 {
10671067
///
10681068
/// let abs_difference = (f - x).abs();
10691069
///
1070-
/// assert!(abs_difference <= 4.0 * f32::EPSILON);
1070+
/// assert!(abs_difference <= 1e-7);
10711071
/// ```
10721072
#[doc(alias = "arcsinh")]
10731073
#[rustc_allow_incoherent_impl]
@@ -1095,7 +1095,7 @@ impl f32 {
10951095
///
10961096
/// let abs_difference = (f - x).abs();
10971097
///
1098-
/// assert!(abs_difference <= 4.0 * f32::EPSILON);
1098+
/// assert!(abs_difference <= 1e-6);
10991099
/// ```
11001100
#[doc(alias = "arccosh")]
11011101
#[rustc_allow_incoherent_impl]

library/std/src/num/f64.rs

Lines changed: 3 additions & 3 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 <= 8.0 * f64::EPSILON);
307+
/// assert!(abs_difference <= 1e-14);
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 <= 8.0 * f64::EPSILON);
331+
/// assert!(abs_difference <= 1e-14);
332332
///
333333
/// assert_eq!(f64::powf(1.0, f64::NAN), 1.0);
334334
/// assert_eq!(f64::powf(f64::NAN, 0.0), 1.0);
@@ -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-10);
757+
/// assert!(abs_difference < 1e-7);
758758
/// ```
759759
#[doc(alias = "arcsin")]
760760
#[rustc_allow_incoherent_impl]

library/std/tests/floats/f32.rs

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::f32::consts;
33
/// Miri adds some extra errors to float functions; make sure the tests still pass.
44
/// These values are purely used as a canary to test against and are thus not a stable guarantee Rust provides.
55
/// They serve as a way to get an idea of the real precision of floating point operations on different platforms.
6-
const APPROX_DELTA: f32 = if cfg!(miri) { 1e-4 } else { 1e-6 };
6+
const APPROX_DELTA: f32 = if cfg!(miri) { 1e-3 } else { 1e-6 };
77

88
#[allow(unused_macros)]
99
macro_rules! assert_f32_biteq {
@@ -22,17 +22,9 @@ fn test_powf() {
2222
let inf: f32 = f32::INFINITY;
2323
let neg_inf: f32 = f32::NEG_INFINITY;
2424
assert_eq!(1.0f32.powf(1.0), 1.0);
25-
assert_approx_eq!(
26-
3.4f32.powf(4.5),
27-
246.408218,
28-
APPROX_DELTA /* Miri float-non-det: Make tests pass for now */
29-
);
25+
assert_approx_eq!(3.4f32.powf(4.5), 246.408218, APPROX_DELTA);
3026
assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
31-
assert_approx_eq!(
32-
(-3.1f32).powf(2.0),
33-
9.61,
34-
APPROX_DELTA /* Miri float-non-det: Make tests pass for now */
35-
);
27+
assert_approx_eq!((-3.1f32).powf(2.0), 9.61, APPROX_DELTA);
3628
assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
3729
assert_eq!(8.3f32.powf(0.0), 1.0);
3830
assert!(nan.powf(2.0).is_nan());
@@ -44,11 +36,7 @@ fn test_powf() {
4436
fn test_exp() {
4537
assert_eq!(1.0, 0.0f32.exp());
4638
assert_approx_eq!(2.718282, 1.0f32.exp(), APPROX_DELTA);
47-
assert_approx_eq!(
48-
148.413162,
49-
5.0f32.exp(),
50-
APPROX_DELTA /* Miri float-non-det: Make tests pass for now */
51-
);
39+
assert_approx_eq!(148.413162, 5.0f32.exp(), APPROX_DELTA);
5240

5341
let inf: f32 = f32::INFINITY;
5442
let neg_inf: f32 = f32::NEG_INFINITY;
@@ -60,11 +48,7 @@ fn test_exp() {
6048

6149
#[test]
6250
fn test_exp2() {
63-
assert_approx_eq!(
64-
32.0,
65-
5.0f32.exp2(),
66-
APPROX_DELTA /* Miri float-non-det: Make tests pass for now */
67-
);
51+
assert_approx_eq!(32.0, 5.0f32.exp2(), APPROX_DELTA);
6852
assert_eq!(1.0, 0.0f32.exp2());
6953

7054
let inf: f32 = f32::INFINITY;
@@ -87,11 +71,7 @@ fn test_ln() {
8771
assert!((-2.3f32).ln().is_nan());
8872
assert_eq!((-0.0f32).ln(), neg_inf);
8973
assert_eq!(0.0f32.ln(), neg_inf);
90-
assert_approx_eq!(
91-
4.0f32.ln(),
92-
1.386294,
93-
APPROX_DELTA /* Miri float-non-det: Make tests pass for now */
94-
);
74+
assert_approx_eq!(4.0f32.ln(), 1.386294, APPROX_DELTA);
9575
}
9676

9777
#[test]
@@ -101,7 +81,7 @@ fn test_log() {
10181
let neg_inf: f32 = f32::NEG_INFINITY;
10282
assert_approx_eq!(10.0f32.log(10.0), 1.0);
10383
assert_approx_eq!(2.3f32.log(3.5), 0.664858);
104-
assert_approx_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0);
84+
assert_approx_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0, APPROX_DELTA);
10585
assert!(1.0f32.log(1.0).is_nan());
10686
assert!(1.0f32.log(-13.9).is_nan());
10787
assert!(nan.log(2.3).is_nan());
@@ -117,17 +97,9 @@ fn test_log2() {
11797
let nan: f32 = f32::NAN;
11898
let inf: f32 = f32::INFINITY;
11999
let neg_inf: f32 = f32::NEG_INFINITY;
120-
assert_approx_eq!(
121-
10.0f32.log2(),
122-
3.321928,
123-
APPROX_DELTA /* Miri float-non-det: Make tests pass for now */
124-
);
100+
assert_approx_eq!(10.0f32.log2(), 3.321928, APPROX_DELTA);
125101
assert_approx_eq!(2.3f32.log2(), 1.201634);
126-
assert_approx_eq!(
127-
1.0f32.exp().log2(),
128-
1.442695,
129-
APPROX_DELTA /* Miri float-non-det: Make tests pass for now */
130-
);
102+
assert_approx_eq!(1.0f32.exp().log2(), 1.442695, APPROX_DELTA);
131103
assert!(nan.log2().is_nan());
132104
assert_eq!(inf.log2(), inf);
133105
assert!(neg_inf.log2().is_nan());
@@ -191,11 +163,7 @@ fn test_acosh() {
191163
assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
192164

193165
// test for low accuracy from issue 104548
194-
assert_approx_eq!(
195-
60.0f32,
196-
60.0f32.cosh().acosh(),
197-
APPROX_DELTA /* Miri float-non-det: Make tests pass for now */
198-
);
166+
assert_approx_eq!(60.0f32, 60.0f32.cosh().acosh(), APPROX_DELTA);
199167
}
200168

201169
#[test]
@@ -274,11 +242,7 @@ fn test_real_consts() {
274242
let ln_10: f32 = consts::LN_10;
275243

276244
assert_approx_eq!(frac_pi_2, pi / 2f32);
277-
assert_approx_eq!(
278-
frac_pi_3,
279-
pi / 3f32,
280-
APPROX_DELTA /* Miri float-non-det: Make tests pass for now */
281-
);
245+
assert_approx_eq!(frac_pi_3, pi / 3f32, APPROX_DELTA);
282246
assert_approx_eq!(frac_pi_4, pi / 4f32);
283247
assert_approx_eq!(frac_pi_6, pi / 6f32);
284248
assert_approx_eq!(frac_pi_8, pi / 8f32);
@@ -290,9 +254,5 @@ fn test_real_consts() {
290254
assert_approx_eq!(log2_e, e.log2());
291255
assert_approx_eq!(log10_e, e.log10());
292256
assert_approx_eq!(ln_2, 2f32.ln());
293-
assert_approx_eq!(
294-
ln_10,
295-
10f32.ln(),
296-
APPROX_DELTA /* Miri float-non-det: Make tests pass for now */
297-
);
257+
assert_approx_eq!(ln_10, 10f32.ln(), APPROX_DELTA);
298258
}

src/tools/miri/tests/pass/float.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ pub fn libm() {
10341034
assert_approx_eq!(400f64.powf(0.5f64), 20f64);
10351035

10361036
// Some inputs to powf and powi result in fixed outputs
1037-
// and thus must be exactly equal to that value
1037+
// and thus must be exactly equal to that value.
10381038
// C standard says:
10391039
// 1^y = 1 for any y, even a NaN.
10401040
assert_eq!(1f32.powf(10.0), 1.0);
@@ -1069,11 +1069,13 @@ pub fn libm() {
10691069

10701070
// For pow (powf in rust) the C standard says:
10711071
// x^0 = 1 for all x even a sNaN
1072+
// FIXME(#4286): this does not match the behavior of all implementations.
10721073
assert_eq!(SNAN_F32.powf(0.0), 1.0);
10731074
assert_eq!(SNAN_F64.powf(0.0), 1.0);
10741075

10751076
// For pown (powi in rust) the C standard says:
10761077
// x^0 = 1 for all x even a sNaN
1078+
// FIXME(#4286): this does not match the behavior of all implementations.
10771079
assert_eq!(SNAN_F32.powi(0), 1.0);
10781080
assert_eq!(SNAN_F64.powi(0), 1.0);
10791081

0 commit comments

Comments
 (0)