11use std:: num:: FpCategory as Fp ;
22use std:: ops:: { Add , Div , Mul , Rem , Sub } ;
33
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-
224trait TestableFloat {
23- const SMALL_NORMAL : Self ;
24- const SUBNORMAL : Self ;
5+ /// Set the default tolerance for float comparison based on the type.
6+ const APPROX : Self ;
7+ const MIN_POSITIVE_NORMAL : Self ;
8+ const MAX_SUBNORMAL : Self ;
259}
2610
2711impl TestableFloat for f16 {
28- const SMALL_NORMAL : Self = 1e-4 ;
29- const SUBNORMAL : Self = 1e-5 ;
12+ const APPROX : Self = 1e-3 ;
13+ const MIN_POSITIVE_NORMAL : Self = Self :: MIN_POSITIVE ;
14+ const MAX_SUBNORMAL : Self = Self :: MIN_POSITIVE . next_down ( ) ;
3015}
3116
3217impl TestableFloat for f32 {
33- const SMALL_NORMAL : Self = 1e-37 ;
34- const SUBNORMAL : Self = 1e-38 ;
18+ const APPROX : Self = 1e-6 ;
19+ const MIN_POSITIVE_NORMAL : Self = Self :: MIN_POSITIVE ;
20+ const MAX_SUBNORMAL : Self = Self :: MIN_POSITIVE . next_down ( ) ;
3521}
3622
3723impl TestableFloat for f64 {
38- const SMALL_NORMAL : Self = 1e-307 ;
39- const SUBNORMAL : Self = 1e-308 ;
24+ const APPROX : Self = 1e-6 ;
25+ const MIN_POSITIVE_NORMAL : Self = Self :: MIN_POSITIVE ;
26+ const MAX_SUBNORMAL : Self = Self :: MIN_POSITIVE . next_down ( ) ;
4027}
4128
4229impl TestableFloat for f128 {
43- const SMALL_NORMAL : Self = 1e-4931 ;
44- const SUBNORMAL : Self = 1e-4932 ;
30+ const APPROX : Self = 1e-9 ;
31+ const MIN_POSITIVE_NORMAL : Self = Self :: MIN_POSITIVE ;
32+ const MAX_SUBNORMAL : Self = Self :: MIN_POSITIVE . next_down ( ) ;
4533}
4634
4735/// Determine the tolerance for values of the argument type.
48- const fn lim_for_ty < T : Approx + Copy > ( _x : T ) -> T {
49- T :: LIM
36+ const fn lim_for_ty < T : TestableFloat + Copy > ( _x : T ) -> T {
37+ T :: APPROX
5038}
5139
5240// We have runtime ("rt") and const versions of these macros.
@@ -211,7 +199,7 @@ macro_rules! float_test {
211199 $( $( #[ $const_meta] ) + ) ?
212200 mod const_ {
213201 #[ allow( unused) ]
214- use super :: { Approx , TestableFloat } ;
202+ use super :: TestableFloat ;
215203 #[ allow( unused) ]
216204 use std:: num:: FpCategory as Fp ;
217205 #[ allow( unused) ]
@@ -373,16 +361,16 @@ float_test! {
373361 f128: #[ cfg( any( miri, target_has_reliable_f128) ) ] ,
374362 } ,
375363 test<Float > {
376- let neg_zero: Float = -0.0 ;
377- assert!( 0.0 == neg_zero) ;
378- assert_biteq!( -0.0 , neg_zero) ;
379- assert!( !neg_zero. is_infinite( ) ) ;
380- assert!( neg_zero. is_finite( ) ) ;
381- assert!( !neg_zero. is_sign_positive( ) ) ;
382- assert!( neg_zero. is_sign_negative( ) ) ;
383- assert!( !neg_zero. is_nan( ) ) ;
384- assert!( !neg_zero. is_normal( ) ) ;
385- assert!( matches!( neg_zero. classify( ) , Fp :: Zero ) ) ;
364+ let neg_zero: Float = -0.0 ;
365+ assert!( 0.0 == neg_zero) ;
366+ assert_biteq!( -0.0 , neg_zero) ;
367+ assert!( !neg_zero. is_infinite( ) ) ;
368+ assert!( neg_zero. is_finite( ) ) ;
369+ assert!( !neg_zero. is_sign_positive( ) ) ;
370+ assert!( neg_zero. is_sign_negative( ) ) ;
371+ assert!( !neg_zero. is_nan( ) ) ;
372+ assert!( !neg_zero. is_normal( ) ) ;
373+ assert!( matches!( neg_zero. classify( ) , Fp :: Zero ) ) ;
386374 }
387375}
388376
@@ -487,8 +475,8 @@ float_test! {
487475 assert!( !zero. is_normal( ) ) ;
488476 assert!( !neg_zero. is_normal( ) ) ;
489477 assert!( one. is_normal( ) ) ;
490- assert!( Float :: SMALL_NORMAL . is_normal( ) ) ;
491- assert!( !Float :: SUBNORMAL . is_normal( ) ) ;
478+ assert!( Float :: MIN_POSITIVE_NORMAL . is_normal( ) ) ;
479+ assert!( !Float :: MAX_SUBNORMAL . is_normal( ) ) ;
492480 }
493481}
494482
@@ -509,8 +497,8 @@ float_test! {
509497 assert!( matches!( zero. classify( ) , Fp :: Zero ) ) ;
510498 assert!( matches!( neg_zero. classify( ) , Fp :: Zero ) ) ;
511499 assert!( matches!( 1f32 . classify( ) , Fp :: Normal ) ) ;
512- assert!( matches!( Float :: SMALL_NORMAL . classify( ) , Fp :: Normal ) ) ;
513- assert!( matches!( Float :: SUBNORMAL . classify( ) , Fp :: Subnormal ) ) ;
500+ assert!( matches!( Float :: MIN_POSITIVE_NORMAL . classify( ) , Fp :: Normal ) ) ;
501+ assert!( matches!( Float :: MAX_SUBNORMAL . classify( ) , Fp :: Subnormal ) ) ;
514502 }
515503}
516504
0 commit comments