Преглед на файлове

Require Neg for Complex conj and inv

If T is an unsigned integer type, these methods are guaranteed to
overflow unless the result is actually real, so we should disallow
them for the same reason that Neg was removed from these types.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Anders Kaseorg преди 10 години
родител
ревизия
7fb887bffc
променени са 1 файла, в които са добавени 4 реда и са изтрити 2 реда
  1. 4 2
      src/complex.rs

+ 4 - 2
src/complex.rs

@@ -56,11 +56,13 @@ impl<T: Clone + Num> Complex<T> {
     pub fn unscale(&self, t: T) -> Complex<T> {
         Complex::new(self.re.clone() / t.clone(), self.im.clone() / t)
     }
+}
 
+impl<T: Clone + Num + Neg<Output = T>> Complex<T> {
     /// Returns the complex conjugate. i.e. `re - i im`
     #[inline]
     pub fn conj(&self) -> Complex<T> {
-        Complex::new(self.re.clone(), T::zero() - self.im.clone())
+        Complex::new(self.re.clone(), -self.im.clone())
     }
 
     /// Returns `1/self`
@@ -68,7 +70,7 @@ impl<T: Clone + Num> Complex<T> {
     pub fn inv(&self) -> Complex<T> {
         let norm_sqr = self.norm_sqr();
         Complex::new(self.re.clone() / norm_sqr.clone(),
-                    T::zero() - self.im.clone() / norm_sqr)
+                     -self.im.clone() / norm_sqr)
     }
 }