From 70876ee42be27738b0cea4bee17138b12679c5f6 Mon Sep 17 00:00:00 2001 From: Romain Perier Date: Fri, 17 Oct 2025 19:27:21 +0200 Subject: [PATCH] Unify and deduplicate max recip float tests --- library/coretests/tests/floats/f128.rs | 12 ------------ library/coretests/tests/floats/f16.rs | 8 +------- library/coretests/tests/floats/mod.rs | 8 ++++++++ 3 files changed, 9 insertions(+), 19 deletions(-) diff --git a/library/coretests/tests/floats/f128.rs b/library/coretests/tests/floats/f128.rs index 62278bf96c3c1..8e4f0c9899e1c 100644 --- a/library/coretests/tests/floats/f128.rs +++ b/library/coretests/tests/floats/f128.rs @@ -1,8 +1,6 @@ // FIXME(f16_f128): only tested on platforms that have symbols and aren't buggy #![cfg(target_has_reliable_f128)] -#[cfg(any(miri, target_has_reliable_f128_math))] -use super::assert_approx_eq; use super::assert_biteq; // Note these tolerances make sense around zero, but not for more extreme exponents. @@ -20,16 +18,6 @@ const TOL_PRECISE: f128 = 1e-28; // FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support // the intrinsics. -#[test] -#[cfg(any(miri, target_has_reliable_f128_math))] -fn test_max_recip() { - assert_approx_eq!( - f128::MAX.recip(), - 8.40525785778023376565669454330438228902076605e-4933, - 1e-4900 - ); -} - #[test] fn test_from() { assert_biteq!(f128::from(false), 0.0); diff --git a/library/coretests/tests/floats/f16.rs b/library/coretests/tests/floats/f16.rs index 7ffafd467a519..3cff4259de54f 100644 --- a/library/coretests/tests/floats/f16.rs +++ b/library/coretests/tests/floats/f16.rs @@ -1,7 +1,7 @@ // FIXME(f16_f128): only tested on platforms that have symbols and aren't buggy #![cfg(target_has_reliable_f16)] -use super::{assert_approx_eq, assert_biteq}; +use super::assert_biteq; /// Tolerance for results on the order of 10.0e-2 #[allow(unused)] @@ -22,12 +22,6 @@ const TOL_P4: f16 = 10.0; // FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support // the intrinsics. -#[test] -#[cfg(any(miri, target_has_reliable_f16_math))] -fn test_max_recip() { - assert_approx_eq!(f16::MAX.recip(), 1.526624e-5f16, 1e-4); -} - #[test] fn test_from() { assert_biteq!(f16::from(false), 0.0); diff --git a/library/coretests/tests/floats/mod.rs b/library/coretests/tests/floats/mod.rs index 0348065d17fe3..63d5b8fb2c6e9 100644 --- a/library/coretests/tests/floats/mod.rs +++ b/library/coretests/tests/floats/mod.rs @@ -38,6 +38,8 @@ trait TestableFloat: Sized { const MUL_ADD_RESULT: Self; /// The result of (-12.3).mul_add(-4.5, -6.7) const NEG_MUL_ADD_RESULT: Self; + /// Reciprocal of the maximum val + const MAX_RECIP: Self; } impl TestableFloat for f16 { @@ -64,6 +66,7 @@ impl TestableFloat for f16 { const RAW_MINUS_14_DOT_25: Self = Self::from_bits(0xcb20); const MUL_ADD_RESULT: Self = 62.031; const NEG_MUL_ADD_RESULT: Self = 48.625; + const MAX_RECIP: Self = 1.526624e-5; } impl TestableFloat for f32 { @@ -92,6 +95,7 @@ impl TestableFloat for f32 { const RAW_MINUS_14_DOT_25: Self = Self::from_bits(0xc1640000); const MUL_ADD_RESULT: Self = 62.05; const NEG_MUL_ADD_RESULT: Self = 48.65; + const MAX_RECIP: Self = 2.938736e-39; } impl TestableFloat for f64 { @@ -116,6 +120,7 @@ impl TestableFloat for f64 { const RAW_MINUS_14_DOT_25: Self = Self::from_bits(0xc02c800000000000); const MUL_ADD_RESULT: Self = 62.050000000000004; const NEG_MUL_ADD_RESULT: Self = 48.650000000000006; + const MAX_RECIP: Self = 5.562684646268003e-309; } impl TestableFloat for f128 { @@ -140,6 +145,7 @@ impl TestableFloat for f128 { const RAW_MINUS_14_DOT_25: Self = Self::from_bits(0xc002c800000000000000000000000000); const MUL_ADD_RESULT: Self = 62.0500000000000000000000000000000037; const NEG_MUL_ADD_RESULT: Self = 48.6500000000000000000000000000000049; + const MAX_RECIP: Self = 8.40525785778023376565669454330438228902076605e-4933; } /// Determine the tolerance for values of the argument type. @@ -1425,6 +1431,7 @@ float_test! { let nan: Float = Float::NAN; let inf: Float = Float::INFINITY; let neg_inf: Float = Float::NEG_INFINITY; + let max: Float = Float::MAX; assert_biteq!((1.0 as Float).recip(), 1.0); assert_biteq!((2.0 as Float).recip(), 0.5); assert_biteq!((-0.4 as Float).recip(), -2.5); @@ -1432,6 +1439,7 @@ float_test! { assert!(nan.recip().is_nan()); assert_biteq!(inf.recip(), 0.0); assert_biteq!(neg_inf.recip(), -0.0); + assert_biteq!(max.recip(), Float::MAX_RECIP); } }