Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing clamp for Float trait #305

Merged
merged 2 commits into from
May 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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