Commit ae2f27cee1e35d5584840a4ba2a096f590771add
1 parent
7f9b6744
Fix some edges on Fractional handling
Showing
1 changed file
with
7 additions
and
6 deletions
| ... | ... | @@ -129,7 +129,11 @@ impl Fractional { |
| 129 | 129 | } |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | - inner(1, x / 2, x) | |
| 132 | + match x { | |
| 133 | + 0 => 0.into(), | |
| 134 | + 1 => 1.into(), | |
| 135 | + _ => inner(1, x / 2, x), | |
| 136 | + } | |
| 133 | 137 | } |
| 134 | 138 | |
| 135 | 139 | let Fractional(n, d) = self; |
| ... | ... | @@ -141,7 +145,7 @@ impl Fractional { |
| 141 | 145 | }; |
| 142 | 146 | |
| 143 | 147 | let d = match d.cmp(&0) { |
| 144 | - Ordering::Equal => return Err("division by zero"), | |
| 148 | + Ordering::Equal => 0.into(), | |
| 145 | 149 | Ordering::Less => return Err("sqrt on negative undefined"), |
| 146 | 150 | Ordering::Greater => floor_sqrt(d), |
| 147 | 151 | }; |
| ... | ... | @@ -268,10 +272,7 @@ impl Sub for Fractional { |
| 268 | 272 | type Output = Self; |
| 269 | 273 | |
| 270 | 274 | fn sub(self, other: Self) -> Self { |
| 271 | - let Fractional(n1, d1) = self; | |
| 272 | - let Fractional(n2, d2) = other; | |
| 273 | - let n = n1 * (self.gcd(other) / d1) - n2 * (self.gcd(other) / d2); | |
| 274 | - Self(n, self.gcd(other)).reduce() | |
| 275 | + self + -other | |
| 275 | 276 | } |
| 276 | 277 | } |
| 277 | 278 | ... | ... |
Please
register
or
login
to post a comment