Skip to content

Commit

Permalink
added Ord trait to GenericFraction (#74)
Browse files Browse the repository at this point in the history
* added Ord trait to GenericFraction

* rewrote GenericFraction<T>::cmp() to be eaiser to read

* replaces the .unwrap() on GenericFraction<T>::cmp() with .expect()
  • Loading branch information
DrAlta committed Dec 3, 2022
1 parent 8a5c0cc commit 5b3ea44
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/fraction/generic_fraction.rs
Expand Up @@ -516,6 +516,25 @@ impl<T: Clone + Integer> PartialOrd for GenericFraction<T> {
}
}

impl<T: Clone + Integer> Ord for GenericFraction<T> {
fn cmp(&self, other: &Self) -> Ordering {
if *self == GenericFraction::NaN {
if *other == GenericFraction::NaN {
Ordering::Equal
} else {
Ordering::Less
}
} else {
if *other == GenericFraction::NaN {
Ordering::Greater
} else {
self.partial_cmp(other).expect("Well when I wrote this the only way partial_cmp() would return None was if one of the argument was NaN, which they weren't in this case.")
}
}
}
}


impl<T: Clone + Integer> Neg for GenericFraction<T> {
type Output = GenericFraction<T>;

Expand Down

0 comments on commit 5b3ea44

Please sign in to comment.