Explorar el Código

:recycle: moved abs to trait Numeric

Felix Bytow hace 2 años
padre
commit
7797501a60
Se han modificado 3 ficheros con 67 adiciones y 19 borrados
  1. 0 11
      src/traits/float.rs
  2. 59 0
      src/traits/numeric.rs
  3. 8 8
      src/types/complex.rs

+ 0 - 11
src/traits/float.rs

@@ -2,9 +2,6 @@ use std::ops::Neg;
 
 /// Marker trait, because certain operations only make sense on floating point types.
 pub trait Float: Neg<Output=Self> {
-    /// Calculate the absolute value. For primitive floats it just calls their `abs` function.
-    fn abs(self) -> Self;
-
     /// Calculate the square root. For primitive floats it just calls their `sqrt` function.
     fn sqrt(self) -> Self;
 
@@ -25,10 +22,6 @@ pub trait Float: Neg<Output=Self> {
 }
 
 impl Float for f32 {
-    fn abs(self) -> Self {
-        self.abs()
-    }
-
     fn sqrt(self) -> Self {
         self.sqrt()
     }
@@ -54,10 +47,6 @@ impl Float for f32 {
 }
 
 impl Float for f64 {
-    fn abs(self) -> Self {
-        self.abs()
-    }
-
     fn sqrt(self) -> Self {
         self.sqrt()
     }

+ 59 - 0
src/traits/numeric.rs

@@ -13,90 +13,149 @@ PartialEq {
     /// The caller must make sure, that the given value can be represented in the target type.
     /// Otherwise behaviour is undefined.
     unsafe fn whole(value: u32) -> Self;
+
+    /// Calculate the absolute value.
+    fn abs(self) -> Self;
 }
 
 impl Numeric for f32 {
     unsafe fn whole(value: u32) -> Self {
         value as Self
     }
+
+    fn abs(self) -> Self {
+        self.abs()
+    }
 }
 
 impl Numeric for f64 {
     unsafe fn whole(value: u32) -> Self {
         value as Self
     }
+
+    fn abs(self) -> Self {
+        self.abs()
+    }
 }
 
 impl Numeric for i8 {
     unsafe fn whole(value: u32) -> Self {
         value as Self
     }
+
+    fn abs(self) -> Self {
+        self.abs()
+    }
 }
 
 impl Numeric for i16 {
     unsafe fn whole(value: u32) -> Self {
         value as Self
     }
+
+    fn abs(self) -> Self {
+        self.abs()
+    }
 }
 
 impl Numeric for i32 {
     unsafe fn whole(value: u32) -> Self {
         value as Self
     }
+
+    fn abs(self) -> Self {
+        self.abs()
+    }
 }
 
 impl Numeric for i64 {
     unsafe fn whole(value: u32) -> Self {
         value as Self
     }
+
+    fn abs(self) -> Self {
+        self.abs()
+    }
 }
 
 impl Numeric for i128 {
     unsafe fn whole(value: u32) -> Self {
         value as Self
     }
+
+    fn abs(self) -> Self {
+        self.abs()
+    }
 }
 
 impl Numeric for isize {
     unsafe fn whole(value: u32) -> Self {
         value as Self
     }
+
+    fn abs(self) -> Self {
+        self.abs()
+    }
 }
 
 impl Numeric for u8 {
     unsafe fn whole(value: u32) -> Self {
         value as Self
     }
+
+    fn abs(self) -> Self {
+        self
+    }
 }
 
 impl Numeric for u16 {
     unsafe fn whole(value: u32) -> Self {
         value as Self
     }
+
+    fn abs(self) -> Self {
+        self
+    }
 }
 
 impl Numeric for u32 {
     unsafe fn whole(value: u32) -> Self {
         value as Self
     }
+
+    fn abs(self) -> Self {
+        self
+    }
 }
 
 impl Numeric for u64 {
     unsafe fn whole(value: u32) -> Self {
         value as Self
     }
+
+    fn abs(self) -> Self {
+        self
+    }
 }
 
 impl Numeric for u128 {
     unsafe fn whole(value: u32) -> Self {
         value as Self
     }
+
+    fn abs(self) -> Self {
+        self
+    }
 }
 
 impl Numeric for usize {
     unsafe fn whole(value: u32) -> Self {
         value as Self
     }
+
+    fn abs(self) -> Self {
+        self
+    }
 }
 
 #[cfg(test)]

+ 8 - 8
src/types/complex.rs

@@ -195,13 +195,6 @@ impl<T: Float + Numeric + Primitive> PartialEq for Complex<T> {
 }
 
 impl<T: Float + Primitive + Numeric> Float for Complex<T> {
-    fn abs(self) -> Self {
-        Self {
-            r: (self.r * self.r + self.i * self.i).sqrt(),
-            i: unsafe { T::whole(0) },
-        }
-    }
-
     fn sqrt(self) -> Self {
         let zero = unsafe { T::whole(0) };
         let two = unsafe { T::whole(2) };
@@ -261,6 +254,13 @@ impl<T: Float + Numeric + Primitive> Numeric for Complex<T> {
     unsafe fn whole(value: u32) -> Self {
         Self { r: T::whole(value), i: T::whole(0) }
     }
+
+    fn abs(self) -> Self {
+        Self {
+            r: (self.r * self.r + self.i * self.i).sqrt(),
+            i: unsafe { T::whole(0) },
+        }
+    }
 }
 
 #[cfg(test)]
@@ -292,7 +292,7 @@ mod tests {
     #[test]
     fn square_root_with_negatives() {
         let a = Complex { r: -1.0, i: -1.0 };
-        let root = a.sqrt();
+        let root: Complex<f64> = a.sqrt();
         let expected = Complex { r: 0.45508986, i: -1.09868411 };
         assert!((root.r - expected.r).abs() < 0.00001);
         assert!((root.i - expected.i).abs() < 0.00001);