rust/fpnum/src/lib.rs
changeset 13884 fbbb4fcd6a75
parent 13883 2bfd7472ef1d
child 13885 cd39e87d7a80
--- a/rust/fpnum/src/lib.rs	Sat Oct 13 17:23:19 2018 +0200
+++ b/rust/fpnum/src/lib.rs	Sat Oct 13 00:43:04 2018 +0300
@@ -127,29 +127,15 @@
 impl Ord for FPNum {
     #[inline]
     fn cmp(&self, rhs: &Self) -> cmp::Ordering {
-        if self.value == 0 && rhs.value == 0 {
-            cmp::Ordering::Equal
-        } else if self.is_negative != rhs.is_negative {
-            if self.is_negative {
-                cmp::Ordering::Less
+        #[inline]
+        fn extend(n: &FPNum) -> i128 {
+            if n.is_negative {
+                -(n.value as i128)
             } else {
-                cmp::Ordering::Greater
-            }
-        } else if self.value == rhs.value {
-            cmp::Ordering::Equal
-        } else if self.is_negative {
-            if self.value > rhs.value {
-                cmp::Ordering::Less
-            } else {
-                cmp::Ordering::Greater
-            }
-        } else {
-            if self.value < rhs.value {
-                cmp::Ordering::Less
-            } else {
-                cmp::Ordering::Greater
+                n.value as i128
             }
         }
+        extend(self).cmp(&(extend(rhs)))
     }
 }
 
@@ -314,6 +300,19 @@
 }
 
 #[test]
+fn ord() {
+    let z = FPNum::from(0);;
+    let n1_5 = FPNum::new(3, 2);
+    let n2_25 = FPNum::new(9, 4);
+
+    assert!(!(z > z));
+    assert!(!(z < z));
+    assert!(n2_25 > n1_5);
+    assert!(-n2_25 < n1_5);
+    assert!(-n2_25 < -n1_5);
+}
+
+#[test]
 fn arith() {
     let n1_5 = FPNum::new(3, 2);
     let n2_25 = FPNum::new(9, 4);