Explorar o código

Implementing epsilon function to retrieve EPSILON constant

James Lucas %!s(int64=8) %!d(string=hai) anos
pai
achega
a64cf66f4c
Modificáronse 1 ficheiros con 17 adicións e 0 borrados
  1. 17 0
      traits/src/float.rs

+ 17 - 0
traits/src/float.rs

@@ -89,6 +89,18 @@ pub trait Float
     /// ```
     fn min_positive_value() -> Self;
 
+    /// Returns epsilon, a small positive value.
+    ///
+    /// ```
+    /// use num_traits::Float;
+    /// use std::f64;
+    ///
+    /// let x: f64 = Float::epsilon();
+    ///
+    /// assert_eq!(x, f64::EPSILON);
+    /// ```
+    fn epsilon() -> Self;
+
     /// Returns the largest finite value that this type can represent.
     ///
     /// ```
@@ -936,6 +948,11 @@ macro_rules! float_impl {
                 ::std::$T::MIN_POSITIVE
             }
 
+            #[inline]
+            fn epsilon() -> Self {
+                ::std::$T::EPSILON
+            }
+
             #[inline]
             fn max_value() -> Self {
                 ::std::$T::MAX