Commit d522cf78ecc96dfdefcadadbdd259f503861effb

Authored by Georg Hopp
1 parent 466367aa

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

Showing 1 changed file with 11 additions and 1 deletions
... ... @@ -86,7 +86,17 @@ impl Fractional {
86 86 #[inline]
87 87 pub fn reduce(self) -> Self {
88 88 let Fractional(n, d) = self;
89   - Self(n / hcf(n, d), d / hcf(n, d))
  89 + let (_n, _d) = if n > d { (n, d) } else { (d, n) };
  90 +
  91 + // if the difference from _n % _d to _n is very big we are close to
  92 + // a whole number and can ignore the fractional part... this reduces
  93 + // the precision but ensures smaller numbers for numerator and
  94 + // denominator.
  95 + if _d > 1 && (_n % _d) * 10000000 < _n {
  96 + Self(_n / _d, 1)
  97 + } else {
  98 + Self(n / hcf(n, d), d / hcf(n, d))
  99 + }
90 100 }
91 101
92 102 #[inline]
... ...
Please register or login to post a comment