@@ -652,6 +652,36 @@ pub trait FloatCore: Num + NumCast + Neg<Output = Self> + PartialOrd + Copy {
652652 }
653653 }
654654
655+ /// A value bounded by a minimum and a maximum
656+ ///
657+ /// If input is less than min then this returns min.
658+ /// If input is greater than max then this returns max.
659+ /// Otherwise this returns input.
660+ ///
661+ /// **Panics** in debug mode if `!(min <= max)`.
662+ ///
663+ /// # Examples
664+ ///
665+ /// ```
666+ /// use num_traits::float::FloatCore;
667+ ///
668+ /// fn check<T: FloatCore>(val: T, min: T, max: T, expected: T) {
669+ /// assert!(val.clamp(min, max) == expected);
670+ /// }
671+ ///
672+ ///
673+ /// check(1.0f32, 0.0, 2.0, 1.0);
674+ /// check(1.0f32, 2.0, 3.0, 2.0);
675+ /// check(3.0f32, 0.0, 2.0, 2.0);
676+ ///
677+ /// check(1.0f64, 0.0, 2.0, 1.0);
678+ /// check(1.0f64, 2.0, 3.0, 2.0);
679+ /// check(3.0f64, 0.0, 2.0, 2.0);
680+ /// ```
681+ fn clamp ( self , min : Self , max : Self ) -> Self {
682+ crate :: clamp ( self , min, max)
683+ }
684+
655685 /// Returns the reciprocal (multiplicative inverse) of the number.
656686 ///
657687 /// # Examples
@@ -791,6 +821,7 @@ impl FloatCore for f32 {
791821 Self :: is_finite( self ) -> bool ;
792822 Self :: is_normal( self ) -> bool ;
793823 Self :: is_subnormal( self ) -> bool ;
824+ Self :: clamp( self , min: Self , max: Self ) -> Self ;
794825 Self :: classify( self ) -> FpCategory ;
795826 Self :: is_sign_positive( self ) -> bool ;
796827 Self :: is_sign_negative( self ) -> bool ;
@@ -852,6 +883,7 @@ impl FloatCore for f64 {
852883 Self :: is_finite( self ) -> bool ;
853884 Self :: is_normal( self ) -> bool ;
854885 Self :: is_subnormal( self ) -> bool ;
886+ Self :: clamp( self , min: Self , max: Self ) -> Self ;
855887 Self :: classify( self ) -> FpCategory ;
856888 Self :: is_sign_positive( self ) -> bool ;
857889 Self :: is_sign_negative( self ) -> bool ;
@@ -1497,6 +1529,19 @@ pub trait Float: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
14971529 /// ```
14981530 fn min ( self , other : Self ) -> Self ;
14991531
1532+ /// Clamps a value between a min and max.
1533+ ///
1534+ /// ```
1535+ /// use num_traits::Float;
1536+ ///
1537+ /// let x = 1.0;
1538+ /// let y = 2.0;
1539+ /// let z = 3.0;
1540+ ///
1541+ /// assert_eq!(x.clamp(y, z), 2.0);
1542+ /// ```
1543+ fn clamp ( self , min : Self , max : Self ) -> Self ;
1544+
15001545 /// The positive difference of two numbers.
15011546 ///
15021547 /// * If `self <= other`: `0:0`
@@ -1895,6 +1940,7 @@ macro_rules! float_impl_std {
18951940 Self :: is_normal( self ) -> bool ;
18961941 Self :: is_subnormal( self ) -> bool ;
18971942 Self :: classify( self ) -> FpCategory ;
1943+ Self :: clamp( self , min: Self , max: Self ) -> Self ;
18981944 Self :: floor( self ) -> Self ;
18991945 Self :: ceil( self ) -> Self ;
19001946 Self :: round( self ) -> Self ;
@@ -1978,6 +2024,7 @@ macro_rules! float_impl_libm {
19782024 Self :: is_finite( self ) -> bool ;
19792025 Self :: is_normal( self ) -> bool ;
19802026 Self :: is_subnormal( self ) -> bool ;
2027+ Self :: clamp( self , min: Self , max: Self ) -> Self ;
19812028 Self :: classify( self ) -> FpCategory ;
19822029 Self :: is_sign_positive( self ) -> bool ;
19832030 Self :: is_sign_negative( self ) -> bool ;
0 commit comments