Skip to content

Commit 266ca8b

Browse files
committed
Assert exact equality in non-MIRI configs
1 parent 9602bbb commit 266ca8b

File tree

5 files changed

+53
-21
lines changed

5 files changed

+53
-21
lines changed

Diff for: library/std/tests/floats/f128.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -989,11 +989,18 @@ fn test_algebraic() {
989989
let a: f128 = 123.0;
990990
let b: f128 = 456.0;
991991

992-
assert_approx_eq!(a.algebraic_add(b), a + b);
993-
assert_approx_eq!(a.algebraic_sub(b), a - b);
994-
assert_approx_eq!(a.algebraic_mul(b), a * b);
995-
assert_approx_eq!(a.algebraic_div(b), a / b);
996-
assert_approx_eq!(a.algebraic_rem(b), a % b);
992+
// Check that individual operations match their primitive counterparts.
993+
//
994+
// This is a check of current implementations and does NOT imply any form of
995+
// guarantee about future behavior. The compiler reserves the right to make
996+
// these operations inexact matches in the future.
997+
let eps = if cfg!(miri) { 1e-6 } else { 0.0 };
998+
999+
assert_approx_eq!(a.algebraic_add(b), a + b, eps);
1000+
assert_approx_eq!(a.algebraic_sub(b), a - b, eps);
1001+
assert_approx_eq!(a.algebraic_mul(b), a * b, eps);
1002+
assert_approx_eq!(a.algebraic_div(b), a / b, eps);
1003+
assert_approx_eq!(a.algebraic_rem(b), a % b, eps);
9971004
}
9981005

9991006
#[test]

Diff for: library/std/tests/floats/f16.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -959,11 +959,20 @@ fn test_algebraic() {
959959
let a: f16 = 123.0;
960960
let b: f16 = 456.0;
961961

962-
assert_approx_eq!(a.algebraic_add(b), a + b, 1e1);
963-
assert_approx_eq!(a.algebraic_sub(b), a - b, 1e1);
964-
assert_approx_eq!(a.algebraic_mul(b), a * b, 1e3);
965-
assert_approx_eq!(a.algebraic_div(b), a / b, 1e0);
966-
assert_approx_eq!(a.algebraic_rem(b), a % b, 1e0);
962+
// Check that individual operations match their primitive counterparts.
963+
//
964+
// This is a check of current implementations and does NOT imply any form of
965+
// guarantee about future behavior. The compiler reserves the right to make
966+
// these operations inexact matches in the future.
967+
let eps_add = if cfg!(miri) { 1e1 } else { 0.0 };
968+
let eps_mul = if cfg!(miri) { 1e3 } else { 0.0 };
969+
let eps_div = if cfg!(miri) { 1e0 } else { 0.0 };
970+
971+
assert_approx_eq!(a.algebraic_add(b), a + b, eps_add);
972+
assert_approx_eq!(a.algebraic_sub(b), a - b, eps_add);
973+
assert_approx_eq!(a.algebraic_mul(b), a * b, eps_mul);
974+
assert_approx_eq!(a.algebraic_div(b), a / b, eps_div);
975+
assert_approx_eq!(a.algebraic_rem(b), a % b, eps_div);
967976
}
968977

969978
#[test]

Diff for: library/std/tests/floats/f32.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -921,9 +921,18 @@ fn test_algebraic() {
921921
let a: f32 = 123.0;
922922
let b: f32 = 456.0;
923923

924-
assert_approx_eq!(a.algebraic_add(b), a + b, 1e-3);
925-
assert_approx_eq!(a.algebraic_sub(b), a - b, 1e-3);
926-
assert_approx_eq!(a.algebraic_mul(b), a * b, 1e-1);
927-
assert_approx_eq!(a.algebraic_div(b), a / b, 1e-4);
928-
assert_approx_eq!(a.algebraic_rem(b), a % b, 1e-4);
924+
// Check that individual operations match their primitive counterparts.
925+
//
926+
// This is a check of current implementations and does NOT imply any form of
927+
// guarantee about future behavior. The compiler reserves the right to make
928+
// these operations inexact matches in the future.
929+
let eps_add = if cfg!(miri) { 1e-3 } else { 0.0 };
930+
let eps_mul = if cfg!(miri) { 1e-1 } else { 0.0 };
931+
let eps_div = if cfg!(miri) { 1e-4 } else { 0.0 };
932+
933+
assert_approx_eq!(a.algebraic_add(b), a + b, eps_add);
934+
assert_approx_eq!(a.algebraic_sub(b), a - b, eps_add);
935+
assert_approx_eq!(a.algebraic_mul(b), a * b, eps_mul);
936+
assert_approx_eq!(a.algebraic_div(b), a / b, eps_div);
937+
assert_approx_eq!(a.algebraic_rem(b), a % b, eps_div);
929938
}

Diff for: library/std/tests/floats/f64.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -900,9 +900,16 @@ fn test_algebraic() {
900900
let a: f64 = 123.0;
901901
let b: f64 = 456.0;
902902

903-
assert_approx_eq!(a.algebraic_add(b), a + b);
904-
assert_approx_eq!(a.algebraic_sub(b), a - b);
905-
assert_approx_eq!(a.algebraic_mul(b), a * b);
906-
assert_approx_eq!(a.algebraic_div(b), a / b);
907-
assert_approx_eq!(a.algebraic_rem(b), a % b);
903+
// Check that individual operations match their primitive counterparts.
904+
//
905+
// This is a check of current implementations and does NOT imply any form of
906+
// guarantee about future behavior. The compiler reserves the right to make
907+
// these operations inexact matches in the future.
908+
let eps = if cfg!(miri) { 1e-6 } else { 0.0 };
909+
910+
assert_approx_eq!(a.algebraic_add(b), a + b, eps);
911+
assert_approx_eq!(a.algebraic_sub(b), a - b, eps);
912+
assert_approx_eq!(a.algebraic_mul(b), a * b, eps);
913+
assert_approx_eq!(a.algebraic_div(b), a / b, eps);
914+
assert_approx_eq!(a.algebraic_rem(b), a % b, eps);
908915
}

Diff for: library/std/tests/floats/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ macro_rules! assert_approx_eq {
1010
let (a, b) = (&$a, &$b);
1111
let diff = (*a - *b).abs();
1212
assert!(
13-
diff < $lim,
13+
diff <= $lim,
1414
"{a:?} is not approximately equal to {b:?} (threshold {lim:?}, difference {diff:?})",
1515
lim = $lim
1616
);

0 commit comments

Comments
 (0)