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. 12
      fractional/src/fractional.rs

12
fractional/src/fractional.rs

@ -86,7 +86,17 @@ impl Fractional {
#[inline]
pub fn reduce(self) -> Self {
let Fractional(n, d) = self;
Self(n / hcf(n, d), d / hcf(n, d))
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))
}
}
#[inline]

Loading…
Cancel
Save