Browse Source

Fix some edges on Fractional handling

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

13
fractional/src/fractional.rs

@ -129,7 +129,11 @@ impl Fractional {
} }
} }
inner(1, x / 2, x)
match x {
0 => 0.into(),
1 => 1.into(),
_ => inner(1, x / 2, x),
}
} }
let Fractional(n, d) = self; let Fractional(n, d) = self;
@ -141,7 +145,7 @@ impl Fractional {
}; };
let d = match d.cmp(&0) { let d = match d.cmp(&0) {
Ordering::Equal => return Err("division by zero"),
Ordering::Equal => 0.into(),
Ordering::Less => return Err("sqrt on negative undefined"), Ordering::Less => return Err("sqrt on negative undefined"),
Ordering::Greater => floor_sqrt(d), Ordering::Greater => floor_sqrt(d),
}; };
@ -268,10 +272,7 @@ impl Sub for Fractional {
type Output = Self; type Output = Self;
fn sub(self, other: Self) -> Self { fn sub(self, other: Self) -> Self {
let Fractional(n1, d1) = self;
let Fractional(n2, d2) = other;
let n = n1 * (self.gcd(other) / d1) - n2 * (self.gcd(other) / d2);
Self(n, self.gcd(other)).reduce()
self + -other
} }
} }

Loading…
Cancel
Save