From 5b3ea44e553321f06bacb7cce1ed2c92b3492dc9 Mon Sep 17 00:00:00 2001 From: Hsingai Tigris Altaica Date: Sat, 3 Dec 2022 14:34:12 -0800 Subject: [PATCH] added Ord trait to GenericFraction (#74) * added Ord trait to GenericFraction * rewrote GenericFraction::cmp() to be eaiser to read * replaces the .unwrap() on GenericFraction::cmp() with .expect() --- src/fraction/generic_fraction.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/fraction/generic_fraction.rs b/src/fraction/generic_fraction.rs index 768b9017..98cd8f5f 100644 --- a/src/fraction/generic_fraction.rs +++ b/src/fraction/generic_fraction.rs @@ -516,6 +516,25 @@ impl PartialOrd for GenericFraction { } } +impl Ord for GenericFraction { + 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 Neg for GenericFraction { type Output = GenericFraction;