11use std:: num:: FpCategory as Fp ;
22use std:: ops:: { Add , Div , Mul , Rem , Sub } ;
33
4- trait TestableFloat {
4+ trait TestableFloat : Sized {
55 /// Unsigned int with the same size, for converting to/from bits.
66 type Int ;
77 /// Set the default tolerance for float comparison based on the type.
88 const APPROX : Self ;
9+ // Needed for looser tolerance for f16
10+ const PI_TO_DEGREES_APPROX : Self = Self :: APPROX ;
911 const ZERO : Self ;
1012 const ONE : Self ;
13+ const PI : Self ;
1114 const MIN_POSITIVE_NORMAL : Self ;
1215 const MAX_SUBNORMAL : Self ;
1316 /// Smallest number
@@ -25,8 +28,10 @@ trait TestableFloat {
2528impl TestableFloat for f16 {
2629 type Int = u16 ;
2730 const APPROX : Self = 1e-3 ;
31+ const PI_TO_DEGREES_APPROX : Self = 0.125 ;
2832 const ZERO : Self = 0.0 ;
2933 const ONE : Self = 1.0 ;
34+ const PI : Self = std:: f16:: consts:: PI ;
3035 const MIN_POSITIVE_NORMAL : Self = Self :: MIN_POSITIVE ;
3136 const MAX_SUBNORMAL : Self = Self :: MIN_POSITIVE . next_down ( ) ;
3237 const TINY : Self = Self :: from_bits ( 0x1 ) ;
@@ -41,6 +46,7 @@ impl TestableFloat for f32 {
4146 const APPROX : Self = 1e-6 ;
4247 const ZERO : Self = 0.0 ;
4348 const ONE : Self = 1.0 ;
49+ const PI : Self = std:: f32:: consts:: PI ;
4450 const MIN_POSITIVE_NORMAL : Self = Self :: MIN_POSITIVE ;
4551 const MAX_SUBNORMAL : Self = Self :: MIN_POSITIVE . next_down ( ) ;
4652 const TINY : Self = Self :: from_bits ( 0x1 ) ;
@@ -55,6 +61,7 @@ impl TestableFloat for f64 {
5561 const APPROX : Self = 1e-6 ;
5662 const ZERO : Self = 0.0 ;
5763 const ONE : Self = 1.0 ;
64+ const PI : Self = std:: f64:: consts:: PI ;
5865 const MIN_POSITIVE_NORMAL : Self = Self :: MIN_POSITIVE ;
5966 const MAX_SUBNORMAL : Self = Self :: MIN_POSITIVE . next_down ( ) ;
6067 const TINY : Self = Self :: from_bits ( 0x1 ) ;
@@ -69,6 +76,7 @@ impl TestableFloat for f128 {
6976 const APPROX : Self = 1e-9 ;
7077 const ZERO : Self = 0.0 ;
7178 const ONE : Self = 1.0 ;
79+ const PI : Self = std:: f128:: consts:: PI ;
7280 const MIN_POSITIVE_NORMAL : Self = Self :: MIN_POSITIVE ;
7381 const MAX_SUBNORMAL : Self = Self :: MIN_POSITIVE . next_down ( ) ;
7482 const TINY : Self = Self :: from_bits ( 0x1 ) ;
@@ -1381,3 +1389,24 @@ float_test! {
13811389 assert_biteq!( neg_inf. powi( 2 ) , inf) ;
13821390 }
13831391}
1392+
1393+ float_test ! {
1394+ name: to_degrees,
1395+ attrs: {
1396+ f16: #[ cfg( target_has_reliable_f16) ] ,
1397+ f128: #[ cfg( target_has_reliable_f128) ] ,
1398+ } ,
1399+ test<Float > {
1400+ let pi: Float = Float :: PI ;
1401+ let nan: Float = Float :: NAN ;
1402+ let inf: Float = Float :: INFINITY ;
1403+ let neg_inf: Float = Float :: NEG_INFINITY ;
1404+ assert_biteq!( ( 0.0 as Float ) . to_degrees( ) , 0.0 ) ;
1405+ assert_approx_eq!( ( -5.8 as Float ) . to_degrees( ) , -332.31552117587745090765431723855668471 ) ;
1406+ assert_approx_eq!( pi. to_degrees( ) , 180.0 , Float :: PI_TO_DEGREES_APPROX ) ;
1407+ assert!( nan. to_degrees( ) . is_nan( ) ) ;
1408+ assert_biteq!( inf. to_degrees( ) , inf) ;
1409+ assert_biteq!( neg_inf. to_degrees( ) , neg_inf) ;
1410+ assert_biteq!( ( 1.0 as Float ) . to_degrees( ) , 57.2957795130823208767981548141051703 ) ;
1411+ }
1412+ }
0 commit comments