Skip to content

Commit 8358949

Browse files
authored
Merge pull request #305 from michaelciraci/implement-float-clamp
Implementing clamp for Float trait
2 parents a90d4a6 + 1a44ffb commit 8358949

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/float.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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,23 @@ 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+
/// **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+
15001549
/// The positive difference of two numbers.
15011550
///
15021551
/// * If `self <= other`: `0:0`
@@ -1895,6 +1944,7 @@ macro_rules! float_impl_std {
18951944
Self::is_normal(self) -> bool;
18961945
Self::is_subnormal(self) -> bool;
18971946
Self::classify(self) -> FpCategory;
1947+
Self::clamp(self, min: Self, max: Self) -> Self;
18981948
Self::floor(self) -> Self;
18991949
Self::ceil(self) -> Self;
19001950
Self::round(self) -> Self;
@@ -1978,6 +2028,7 @@ macro_rules! float_impl_libm {
19782028
Self::is_finite(self) -> bool;
19792029
Self::is_normal(self) -> bool;
19802030
Self::is_subnormal(self) -> bool;
2031+
Self::clamp(self, min: Self, max: Self) -> Self;
19812032
Self::classify(self) -> FpCategory;
19822033
Self::is_sign_positive(self) -> bool;
19832034
Self::is_sign_negative(self) -> bool;

0 commit comments

Comments
 (0)