Skip to content

Commit

Permalink
Merge pull request #305 from michaelciraci/implement-float-clamp
Browse files Browse the repository at this point in the history
Implementing clamp for Float trait
  • Loading branch information
cuviper committed May 3, 2024
2 parents a90d4a6 + 1a44ffb commit 8358949
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,36 @@ pub trait FloatCore: Num + NumCast + Neg<Output = Self> + PartialOrd + Copy {
}
}

/// A value bounded by a minimum and a maximum
///
/// If input is less than min then this returns min.
/// If input is greater than max then this returns max.
/// Otherwise this returns input.
///
/// **Panics** in debug mode if `!(min <= max)`.
///
/// # Examples
///
/// ```
/// use num_traits::float::FloatCore;
///
/// fn check<T: FloatCore>(val: T, min: T, max: T, expected: T) {
/// assert!(val.clamp(min, max) == expected);
/// }
///
///
/// check(1.0f32, 0.0, 2.0, 1.0);
/// check(1.0f32, 2.0, 3.0, 2.0);
/// check(3.0f32, 0.0, 2.0, 2.0);
///
/// check(1.0f64, 0.0, 2.0, 1.0);
/// check(1.0f64, 2.0, 3.0, 2.0);
/// check(3.0f64, 0.0, 2.0, 2.0);
/// ```
fn clamp(self, min: Self, max: Self) -> Self {
crate::clamp(self, min, max)
}

/// Returns the reciprocal (multiplicative inverse) of the number.
///
/// # Examples
Expand Down Expand Up @@ -791,6 +821,7 @@ impl FloatCore for f32 {
Self::is_finite(self) -> bool;
Self::is_normal(self) -> bool;
Self::is_subnormal(self) -> bool;
Self::clamp(self, min: Self, max: Self) -> Self;
Self::classify(self) -> FpCategory;
Self::is_sign_positive(self) -> bool;
Self::is_sign_negative(self) -> bool;
Expand Down Expand Up @@ -852,6 +883,7 @@ impl FloatCore for f64 {
Self::is_finite(self) -> bool;
Self::is_normal(self) -> bool;
Self::is_subnormal(self) -> bool;
Self::clamp(self, min: Self, max: Self) -> Self;
Self::classify(self) -> FpCategory;
Self::is_sign_positive(self) -> bool;
Self::is_sign_negative(self) -> bool;
Expand Down Expand Up @@ -1497,6 +1529,23 @@ pub trait Float: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// ```
fn min(self, other: Self) -> Self;

/// Clamps a value between a min and max.
///
/// **Panics** in debug mode if `!(min <= max)`.
///
/// ```
/// use num_traits::Float;
///
/// let x = 1.0;
/// let y = 2.0;
/// let z = 3.0;
///
/// assert_eq!(x.clamp(y, z), 2.0);
/// ```
fn clamp(self, min: Self, max: Self) -> Self {
crate::clamp(self, min, max)
}

/// The positive difference of two numbers.
///
/// * If `self <= other`: `0:0`
Expand Down Expand Up @@ -1895,6 +1944,7 @@ macro_rules! float_impl_std {
Self::is_normal(self) -> bool;
Self::is_subnormal(self) -> bool;
Self::classify(self) -> FpCategory;
Self::clamp(self, min: Self, max: Self) -> Self;
Self::floor(self) -> Self;
Self::ceil(self) -> Self;
Self::round(self) -> Self;
Expand Down Expand Up @@ -1978,6 +2028,7 @@ macro_rules! float_impl_libm {
Self::is_finite(self) -> bool;
Self::is_normal(self) -> bool;
Self::is_subnormal(self) -> bool;
Self::clamp(self, min: Self, max: Self) -> Self;
Self::classify(self) -> FpCategory;
Self::is_sign_positive(self) -> bool;
Self::is_sign_negative(self) -> bool;
Expand Down

0 comments on commit 8358949

Please sign in to comment.