File tree 1 file changed +27
-0
lines changed
1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,9 @@ use std::mem;
2
2
use std:: ops:: Neg ;
3
3
use std:: num:: FpCategory ;
4
4
5
+ // Used for default implementation of `epsilon`
6
+ use std:: f32;
7
+
5
8
use { Num , NumCast } ;
6
9
7
10
// FIXME: these doctests aren't actually helpful, because they're using and
@@ -89,6 +92,25 @@ pub trait Float
89
92
/// ```
90
93
fn min_positive_value ( ) -> Self ;
91
94
95
+ /// Returns epsilon, a small positive value.
96
+ ///
97
+ /// ```
98
+ /// use num_traits::Float;
99
+ /// use std::f64;
100
+ ///
101
+ /// let x: f64 = Float::epsilon();
102
+ ///
103
+ /// assert_eq!(x, f64::EPSILON);
104
+ /// ```
105
+ ///
106
+ /// # Panics
107
+ ///
108
+ /// The default implementation will panic if `f32::EPSILON` cannot
109
+ /// be cast to `Self`.
110
+ fn epsilon ( ) -> Self {
111
+ Self :: from ( f32:: EPSILON ) . expect ( "Unable to cast from f32::EPSILON" )
112
+ }
113
+
92
114
/// Returns the largest finite value that this type can represent.
93
115
///
94
116
/// ```
@@ -936,6 +958,11 @@ macro_rules! float_impl {
936
958
:: std:: $T:: MIN_POSITIVE
937
959
}
938
960
961
+ #[ inline]
962
+ fn epsilon( ) -> Self {
963
+ :: std:: $T:: EPSILON
964
+ }
965
+
939
966
#[ inline]
940
967
fn max_value( ) -> Self {
941
968
:: std:: $T:: MAX
You can’t perform that action at this time.
0 commit comments