Skip to content

Commit d89ce12

Browse files
bors[bot]nullsaucecuviper
authored
Merge #109
109: Exposes approximate_float for unsigned ratios r=cuviper a=flavioroth Hey there. Cool crate! Any reason `approximate_float_unsigned` isn't exposed in the lib interface ? It makes it impossible to approximate into unsigned ratios. Here's a small patch that fixes that. Hope it helps! Co-authored-by: Flavio Roth <flavio@secondspectrum.com> Co-authored-by: Josh Stone <cuviper@gmail.com>
2 parents e0f97ff + fb0f847 commit d89ce12

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ use num_bigint::{BigInt, BigUint, Sign, ToBigInt};
3838

3939
use num_integer::Integer;
4040
use num_traits::float::FloatCore;
41-
use num_traits::ToPrimitive;
4241
use num_traits::{
4342
Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Inv, Num, NumCast, One,
44-
Pow, Signed, Zero,
43+
Pow, Signed, ToPrimitive, Unsigned, Zero,
4544
};
4645

4746
mod pow;
@@ -1280,6 +1279,16 @@ impl<T: Integer + Signed + Bounded + NumCast + Clone> Ratio<T> {
12801279
}
12811280
}
12821281

1282+
impl<T: Integer + Unsigned + Bounded + NumCast + Clone> Ratio<T> {
1283+
pub fn approximate_float_unsigned<F: FloatCore + NumCast>(f: F) -> Option<Ratio<T>> {
1284+
// 1/10e-20 < 1/2**32 which seems like a good default, and 30 seems
1285+
// to work well. Might want to choose something based on the types in the future, e.g.
1286+
// T::max().recip() and T::bits() or something similar.
1287+
let epsilon = <F as NumCast>::from(10e-20).expect("Can't convert 10e-20");
1288+
approximate_float_unsigned(f, epsilon, 30)
1289+
}
1290+
}
1291+
12831292
fn approximate_float<T, F>(val: F, max_error: F, max_iterations: usize) -> Option<Ratio<T>>
12841293
where
12851294
T: Integer + Signed + Bounded + NumCast + Clone,

0 commit comments

Comments
 (0)