Browse Source

reduce very large fractions close to a whole number to the whole number

master
Georg Hopp 6 years ago
parent
commit
d522cf78ec
Signed by: ghopp GPG Key ID: 4C5D226768784538
  1. 10
      fractional/src/fractional.rs

10
fractional/src/fractional.rs

@ -86,8 +86,18 @@ impl Fractional {
#[inline] #[inline]
pub fn reduce(self) -> Self { pub fn reduce(self) -> Self {
let Fractional(n, d) = self; let Fractional(n, d) = self;
let (_n, _d) = if n > d { (n, d) } else { (d, n) };
// if the difference from _n % _d to _n is very big we are close to
// a whole number and can ignore the fractional part... this reduces
// the precision but ensures smaller numbers for numerator and
// denominator.
if _d > 1 && (_n % _d) * 10000000 < _n {
Self(_n / _d, 1)
} else {
Self(n / hcf(n, d), d / hcf(n, d)) Self(n / hcf(n, d), d / hcf(n, d))
} }
}
#[inline] #[inline]
pub fn numerator(self) -> i64 { pub fn numerator(self) -> i64 {

Loading…
Cancel
Save