1
1
use std:: num:: FpCategory as Fp ;
2
2
use std:: ops:: { Add , Div , Mul , Rem , Sub } ;
3
3
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
4
trait 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 ;
25
9
}
26
10
27
11
impl 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 ( ) ;
30
15
}
31
16
32
17
impl 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 ( ) ;
35
21
}
36
22
37
23
impl 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 ( ) ;
40
27
}
41
28
42
29
impl 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 ( ) ;
45
33
}
46
34
47
35
/// 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
50
38
}
51
39
52
40
// We have runtime ("rt") and const versions of these macros.
@@ -211,7 +199,7 @@ macro_rules! float_test {
211
199
$( $( #[ $const_meta] ) + ) ?
212
200
mod const_ {
213
201
#[ allow( unused) ]
214
- use super :: { Approx , TestableFloat } ;
202
+ use super :: TestableFloat ;
215
203
#[ allow( unused) ]
216
204
use std:: num:: FpCategory as Fp ;
217
205
#[ allow( unused) ]
@@ -373,16 +361,16 @@ float_test! {
373
361
f128: #[ cfg( any( miri, target_has_reliable_f128) ) ] ,
374
362
} ,
375
363
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 ) ) ;
386
374
}
387
375
}
388
376
@@ -487,8 +475,8 @@ float_test! {
487
475
assert!( !zero. is_normal( ) ) ;
488
476
assert!( !neg_zero. is_normal( ) ) ;
489
477
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( ) ) ;
492
480
}
493
481
}
494
482
@@ -509,8 +497,8 @@ float_test! {
509
497
assert!( matches!( zero. classify( ) , Fp :: Zero ) ) ;
510
498
assert!( matches!( neg_zero. classify( ) , Fp :: Zero ) ) ;
511
499
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 ) ) ;
514
502
}
515
503
}
516
504
0 commit comments