diff --git a/Cargo.lock b/Cargo.lock index c3b950bd506..80a0e1999cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -71,15 +71,6 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" -[[package]] -name = "float-cmp" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b09cf3155332e944990140d967ff5eceb70df778b34f77d8075db46e4704e6d8" -dependencies = [ - "num-traits", -] - [[package]] name = "futures-core" version = "0.3.32" @@ -388,7 +379,6 @@ dependencies = [ name = "test_helpers" version = "0.1.0" dependencies = [ - "float-cmp", "proptest", ] diff --git a/crates/std_float/tests/float.rs b/crates/std_float/tests/float.rs index 0fa5da3dca5..ece8cdda8f4 100644 --- a/crates/std_float/tests/float.rs +++ b/crates/std_float/tests/float.rs @@ -1,4 +1,5 @@ #![feature(portable_simd)] +#![feature(f16)] macro_rules! unary_test { { $scalar:tt, $($func:tt),+ } => { @@ -91,5 +92,6 @@ macro_rules! impl_tests { } } +impl_tests! { f16 } impl_tests! { f32 } impl_tests! { f64 } diff --git a/crates/test_helpers/Cargo.toml b/crates/test_helpers/Cargo.toml index da7ef7bd994..2abacd89b8e 100644 --- a/crates/test_helpers/Cargo.toml +++ b/crates/test_helpers/Cargo.toml @@ -6,4 +6,3 @@ publish = false [dependencies] proptest = { workspace = true, features = ["alloc", "std"] } -float-cmp = "0.10" diff --git a/crates/test_helpers/src/approxeq.rs b/crates/test_helpers/src/approxeq.rs index 57b43a16bc6..b868e09e148 100644 --- a/crates/test_helpers/src/approxeq.rs +++ b/crates/test_helpers/src/approxeq.rs @@ -1,7 +1,5 @@ //! Compare numeric types approximately. -use float_cmp::Ulps; - pub trait ApproxEq { fn approxeq(&self, other: &Self, _ulps: i64) -> bool; fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result; @@ -43,7 +41,14 @@ macro_rules! impl_float_approxeq { if self.is_nan() && other.is_nan() { true } else { - (self.ulps(other) as i64).abs() <= ulps + let allowed_ulp_diff = ulps; + + // Approximate the ULP by taking half the distance between the number one place "up" + // and the number one place "down". + let ulp = (other.next_up() - other.next_down()) / 2.0; + let ulp_diff = ((self - other) / ulp).abs().round() as i64; + + ulp_diff <= allowed_ulp_diff } } @@ -55,7 +60,7 @@ macro_rules! impl_float_approxeq { }; } -impl_float_approxeq! { f32, f64 } +impl_float_approxeq! { f16, f32, f64 } impl ApproxEq for [T; N] { fn approxeq(&self, other: &Self, ulps: i64) -> bool {