@@ -652,6 +652,36 @@ pub trait FloatCore: Num + NumCast + Neg<Output = Self> + PartialOrd + Copy {
652
652
}
653
653
}
654
654
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
+
655
685
/// Returns the reciprocal (multiplicative inverse) of the number.
656
686
///
657
687
/// # Examples
@@ -791,6 +821,7 @@ impl FloatCore for f32 {
791
821
Self :: is_finite( self ) -> bool ;
792
822
Self :: is_normal( self ) -> bool ;
793
823
Self :: is_subnormal( self ) -> bool ;
824
+ Self :: clamp( self , min: Self , max: Self ) -> Self ;
794
825
Self :: classify( self ) -> FpCategory ;
795
826
Self :: is_sign_positive( self ) -> bool ;
796
827
Self :: is_sign_negative( self ) -> bool ;
@@ -852,6 +883,7 @@ impl FloatCore for f64 {
852
883
Self :: is_finite( self ) -> bool ;
853
884
Self :: is_normal( self ) -> bool ;
854
885
Self :: is_subnormal( self ) -> bool ;
886
+ Self :: clamp( self , min: Self , max: Self ) -> Self ;
855
887
Self :: classify( self ) -> FpCategory ;
856
888
Self :: is_sign_positive( self ) -> bool ;
857
889
Self :: is_sign_negative( self ) -> bool ;
@@ -1497,6 +1529,23 @@ pub trait Float: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
1497
1529
/// ```
1498
1530
fn min ( self , other : Self ) -> Self ;
1499
1531
1532
+ /// Clamps a value between a min and max.
1533
+ ///
1534
+ /// **Panics** in debug mode if `!(min <= max)`.
1535
+ ///
1536
+ /// ```
1537
+ /// use num_traits::Float;
1538
+ ///
1539
+ /// let x = 1.0;
1540
+ /// let y = 2.0;
1541
+ /// let z = 3.0;
1542
+ ///
1543
+ /// assert_eq!(x.clamp(y, z), 2.0);
1544
+ /// ```
1545
+ fn clamp ( self , min : Self , max : Self ) -> Self {
1546
+ crate :: clamp ( self , min, max)
1547
+ }
1548
+
1500
1549
/// The positive difference of two numbers.
1501
1550
///
1502
1551
/// * If `self <= other`: `0:0`
@@ -1895,6 +1944,7 @@ macro_rules! float_impl_std {
1895
1944
Self :: is_normal( self ) -> bool ;
1896
1945
Self :: is_subnormal( self ) -> bool ;
1897
1946
Self :: classify( self ) -> FpCategory ;
1947
+ Self :: clamp( self , min: Self , max: Self ) -> Self ;
1898
1948
Self :: floor( self ) -> Self ;
1899
1949
Self :: ceil( self ) -> Self ;
1900
1950
Self :: round( self ) -> Self ;
@@ -1978,6 +2028,7 @@ macro_rules! float_impl_libm {
1978
2028
Self :: is_finite( self ) -> bool ;
1979
2029
Self :: is_normal( self ) -> bool ;
1980
2030
Self :: is_subnormal( self ) -> bool ;
2031
+ Self :: clamp( self , min: Self , max: Self ) -> Self ;
1981
2032
Self :: classify( self ) -> FpCategory ;
1982
2033
Self :: is_sign_positive( self ) -> bool ;
1983
2034
Self :: is_sign_negative( self ) -> bool ;
0 commit comments