Skip to content

Commit 6245a59

Browse files
approx delta different when running in miri
1 parent 3caf613 commit 6245a59

File tree

1 file changed

+13
-8
lines changed
  • library/std/tests/floats

1 file changed

+13
-8
lines changed

library/std/tests/floats/f32.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ const NAN_MASK1: u32 = 0x002a_aaaa;
2222
/// Second pattern over the mantissa
2323
const NAN_MASK2: u32 = 0x0055_5555;
2424

25+
/// Miri adds some extra errors to float functions; make sure the tests still pass.
26+
/// These values are purely used as a canary to test against and are thus not a stable guarantee Rust provides.
27+
/// They serve as a way to get an idea of the real precision of floating point operations on different platforms.
28+
const APPROX_DETLA: f32 = if cfg!(miri) { 1e-4 } else { 1e-6 };
29+
2530
#[allow(unused_macros)]
2631
macro_rules! assert_f32_biteq {
2732
($left : expr, $right : expr) => {
@@ -446,7 +451,7 @@ fn test_powi() {
446451
assert_approx_eq!(
447452
(-3.1f32).powi(2),
448453
9.61,
449-
1e-4 /* Miri float-non-det: Make tests pass for now */
454+
APPROX_DETLA /* Miri float-non-det: Make tests pass for now */
450455
);
451456
assert_approx_eq!(5.9f32.powi(-2), 0.028727);
452457
assert_eq!(8.3f32.powi(0), 1.0);
@@ -464,13 +469,13 @@ fn test_powf() {
464469
assert_approx_eq!(
465470
3.4f32.powf(4.5),
466471
246.408218,
467-
1e-4 /* Miri float-non-det: Make tests pass for now */
472+
APPROX_DETLA /* Miri float-non-det: Make tests pass for now */
468473
);
469474
assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
470475
assert_approx_eq!(
471476
(-3.1f32).powf(2.0),
472477
9.61,
473-
1e-4 /* Miri float-non-det: Make tests pass for now */
478+
APPROX_DETLA /* Miri float-non-det: Make tests pass for now */
474479
);
475480
assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
476481
assert_eq!(8.3f32.powf(0.0), 1.0);
@@ -497,7 +502,7 @@ fn test_exp() {
497502
assert_approx_eq!(
498503
148.413162,
499504
5.0f32.exp(),
500-
1e-4 /* Miri float-non-det: Make tests pass for now */
505+
APPROX_DETLA /* Miri float-non-det: Make tests pass for now */
501506
);
502507

503508
let inf: f32 = f32::INFINITY;
@@ -513,7 +518,7 @@ fn test_exp2() {
513518
assert_approx_eq!(
514519
32.0,
515520
5.0f32.exp2(),
516-
1e-4 /* Miri float-non-det: Make tests pass for now */
521+
APPROX_DETLA /* Miri float-non-det: Make tests pass for now */
517522
);
518523
assert_eq!(1.0, 0.0f32.exp2());
519524

@@ -540,7 +545,7 @@ fn test_ln() {
540545
assert_approx_eq!(
541546
4.0f32.ln(),
542547
1.386294,
543-
1e-4 /* Miri float-non-det: Make tests pass for now */
548+
APPROX_DETLA /* Miri float-non-det: Make tests pass for now */
544549
);
545550
}
546551

@@ -666,7 +671,7 @@ fn test_acosh() {
666671
assert_approx_eq!(
667672
60.0f32,
668673
60.0f32.cosh().acosh(),
669-
1e-4 /* Miri float-non-det: Make tests pass for now */
674+
APPROX_DETLA /* Miri float-non-det: Make tests pass for now */
670675
);
671676
}
672677

@@ -749,7 +754,7 @@ fn test_real_consts() {
749754
assert_approx_eq!(
750755
frac_pi_3,
751756
pi / 3f32,
752-
1e-4 /* Miri float-non-det: Make tests pass for now */
757+
APPROX_DETLA /* Miri float-non-det: Make tests pass for now */
753758
);
754759
assert_approx_eq!(frac_pi_4, pi / 4f32);
755760
assert_approx_eq!(frac_pi_6, pi / 6f32);

0 commit comments

Comments
 (0)