File tree Expand file tree Collapse file tree 1 file changed +11
-0
lines changed Expand file tree Collapse file tree 1 file changed +11
-0
lines changed Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ use std::error::Error;
3737use num_bigint:: { BigInt , BigUint , Sign , ToBigInt } ;
3838
3939use num_integer:: Integer ;
40+ use num_traits:: Unsigned ;
4041use num_traits:: float:: FloatCore ;
4142use num_traits:: ToPrimitive ;
4243use num_traits:: {
@@ -1280,6 +1281,16 @@ impl<T: Integer + Signed + Bounded + NumCast + Clone> Ratio<T> {
12801281 }
12811282}
12821283
1284+ impl < T : Integer + Unsigned + Bounded + NumCast + Clone > Ratio < T > {
1285+ pub fn approximate_float_unsigned < F : FloatCore + NumCast > ( f : F ) -> Option < Ratio < T > > {
1286+ // 1/10e-20 < 1/2**32 which seems like a good default, and 30 seems
1287+ // to work well. Might want to choose something based on the types in the future, e.g.
1288+ // T::max().recip() and T::bits() or something similar.
1289+ let epsilon = <F as NumCast >:: from ( 10e-20 ) . expect ( "Can't convert 10e-20" ) ;
1290+ approximate_float_unsigned ( f, epsilon, 30 )
1291+ }
1292+ }
1293+
12831294fn approximate_float < T , F > ( val : F , max_error : F , max_iterations : usize ) -> Option < Ratio < T > >
12841295where
12851296 T : Integer + Signed + Bounded + NumCast + Clone ,
You can’t perform that action at this time.
0 commit comments