rust/fpnum/src/lib.rs
changeset 13906 2a3d119b9fe9
parent 13905 b172a5d40eee
child 13917 ac1801fe51d9
equal deleted inserted replaced
13905:b172a5d40eee 13906:2a3d119b9fe9
   125 }
   125 }
   126 
   126 
   127 impl Ord for FPNum {
   127 impl Ord for FPNum {
   128     #[inline]
   128     #[inline]
   129     fn cmp(&self, rhs: &Self) -> cmp::Ordering {
   129     fn cmp(&self, rhs: &Self) -> cmp::Ordering {
   130         if self.value == 0 && rhs.value == 0 {
   130         #[inline]
   131             cmp::Ordering::Equal
   131         fn extend(n: &FPNum) -> i128 {
   132         } else if self.is_negative != rhs.is_negative {
   132             if n.is_negative {
   133             if self.is_negative {
   133                 -(n.value as i128)
   134                 cmp::Ordering::Less
       
   135             } else {
   134             } else {
   136                 cmp::Ordering::Greater
   135                 n.value as i128
   137             }
   136             }
   138         } else if self.value == rhs.value {
   137         }
   139             cmp::Ordering::Equal
   138         extend(self).cmp(&(extend(rhs)))
   140         } else if self.is_negative {
       
   141             if self.value > rhs.value {
       
   142                 cmp::Ordering::Less
       
   143             } else {
       
   144                 cmp::Ordering::Greater
       
   145             }
       
   146         } else {
       
   147             if self.value < rhs.value {
       
   148                 cmp::Ordering::Less
       
   149             } else {
       
   150                 cmp::Ordering::Greater
       
   151             }
       
   152         }
       
   153     }
   139     }
   154 }
   140 }
   155 
   141 
   156 impl ops::Add for FPNum {
   142 impl ops::Add for FPNum {
   157     type Output = Self;
   143     type Output = Self;
   311     assert!((-z).is_negative);
   297     assert!((-z).is_negative);
   312     assert_eq!(n - n, z)
   298     assert_eq!(n - n, z)
   313 }
   299 }
   314 
   300 
   315 #[test]
   301 #[test]
       
   302 fn ord() {
       
   303     let z = FPNum::from(0);;
       
   304     let n1_5 = FPNum::new(3, 2);
       
   305     let n2_25 = FPNum::new(9, 4);
       
   306 
       
   307     assert!(!(z > z));
       
   308     assert!(!(z < z));
       
   309     assert!(n2_25 > n1_5);
       
   310     assert!(-n2_25 < n1_5);
       
   311     assert!(-n2_25 < -n1_5);
       
   312 }
       
   313 
       
   314 #[test]
   316 fn arith() {
   315 fn arith() {
   317     let n1_5 = FPNum::new(3, 2);
   316     let n1_5 = FPNum::new(3, 2);
   318     let n2_25 = FPNum::new(9, 4);
   317     let n2_25 = FPNum::new(9, 4);
   319 
   318 
   320     assert_eq!(n1_5 + n1_5, FPNum::from(3));
   319     assert_eq!(n1_5 + n1_5, FPNum::from(3));