Skip to content

Commit 7759b70

Browse files
committed
make the default float comparison tolerance type-dependent
1 parent 60ffe6f commit 7759b70

File tree

1 file changed

+30
-5
lines changed
  • library/coretests/tests/floats

1 file changed

+30
-5
lines changed

library/coretests/tests/floats/mod.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
use std::fmt;
22
use std::ops::{Add, Div, Mul, Rem, Sub};
33

4-
/// Verify that floats are within a tolerance of each other, 1.0e-6 by default.
4+
/// Set the default tolerance for float comparison based on the type.
5+
trait Approx {
6+
const LIM: Self;
7+
}
8+
9+
impl Approx for f16 {
10+
const LIM: Self = 1e-3;
11+
}
12+
impl Approx for f32 {
13+
const LIM: Self = 1e-6;
14+
}
15+
impl Approx for f64 {
16+
const LIM: Self = 1e-6;
17+
}
18+
impl Approx for f128 {
19+
const LIM: Self = 1e-9;
20+
}
21+
22+
/// Determine the tolerance for values of the argument type.
23+
const fn lim_for_ty<T: Approx + Copy>(_x: T) -> T {
24+
T::LIM
25+
}
26+
27+
/// Verify that floats are within a tolerance of each other.
528
macro_rules! assert_approx_eq_ {
6-
($a:expr, $b:expr) => {{ assert_approx_eq!($a, $b, 1.0e-6) }};
29+
($a:expr, $b:expr) => {{ assert_approx_eq!($a, $b, $crate::floats::lim_for_ty($a)) }};
730
($a:expr, $b:expr, $lim:expr) => {{
831
let (a, b) = (&$a, &$b);
932
let diff = (*a - *b).abs();
@@ -94,7 +117,7 @@ mod const_asserts {
94117
pub(crate) use assert_biteq;
95118

96119
macro_rules! assert_approx_eq {
97-
($a:expr, $b:expr) => {{ assert_approx_eq!($a, $b, 1.0e-6) }};
120+
($a:expr, $b:expr) => {{ assert_approx_eq!($a, $b, $crate::floats::lim_for_ty($a)) }};
98121
($a:expr, $b:expr, $lim:expr) => {{
99122
let (a, b) = (&$a, &$b);
100123
let diff = (*a - *b).abs();
@@ -170,6 +193,8 @@ macro_rules! float_test {
170193

171194
$( $( #[$const_meta] )+ )?
172195
mod const_ {
196+
#[allow(unused)]
197+
use super::Approx;
173198
#[allow(unused)]
174199
use $crate::floats::const_asserts::{assert_eq, assert_biteq, assert_approx_eq};
175200

@@ -650,15 +675,15 @@ float_test! {
650675
},
651676
test<Float> {
652677
assert_biteq!((1.0 as Float).fract(), 0.0);
653-
assert_approx_eq!((1.3 as Float).fract(), 0.3, 1e-3); // rounding differs between float types
678+
assert_approx_eq!((1.3 as Float).fract(), 0.3); // rounding differs between float types
654679
assert_biteq!((1.5 as Float).fract(), 0.5);
655680
assert_approx_eq!((1.7 as Float).fract(), 0.7);
656681
assert_biteq!((0.5 as Float).fract(), 0.5);
657682
assert_biteq!((0.0 as Float).fract(), 0.0);
658683
assert_biteq!((-0.0 as Float).fract(), 0.0);
659684
assert_biteq!((-0.5 as Float).fract(), -0.5);
660685
assert_biteq!((-1.0 as Float).fract(), 0.0);
661-
assert_approx_eq!((-1.3 as Float).fract(), -0.3, 1e-3); // rounding differs between float types
686+
assert_approx_eq!((-1.3 as Float).fract(), -0.3); // rounding differs between float types
662687
assert_biteq!((-1.5 as Float).fract(), -0.5);
663688
assert_approx_eq!((-1.7 as Float).fract(), -0.7);
664689
assert_eq!(Float::MAX.fract(), 0.0);

0 commit comments

Comments
 (0)