Skip to content

Commit 26d255d

Browse files
committed
Exposes approximate_float for unsigned ratios
1 parent 83bcb5e commit 26d255d

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use std::error::Error;
3737
use num_bigint::{BigInt, BigUint, Sign, ToBigInt};
3838

3939
use num_integer::Integer;
40+
use num_traits::Unsigned;
4041
use num_traits::float::FloatCore;
4142
use num_traits::ToPrimitive;
4243
use 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+
12831294
fn approximate_float<T, F>(val: F, max_error: F, max_iterations: usize) -> Option<Ratio<T>>
12841295
where
12851296
T: Integer + Signed + Bounded + NumCast + Clone,

0 commit comments

Comments
 (0)