|
|
@ -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 {
|
|
|
|