mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-27 09:14:20 +00:00
Move {f32,f64}::clamp to core.
This commit is contained in:
parent
192c7dbb6d
commit
0523eeb8a3
@ -961,4 +961,39 @@ impl f32 {
|
||||
|
||||
left.cmp(&right)
|
||||
}
|
||||
|
||||
/// Restrict a value to a certain interval unless it is NaN.
|
||||
///
|
||||
/// Returns `max` if `self` is greater than `max`, and `min` if `self` is
|
||||
/// less than `min`. Otherwise this returns `self`.
|
||||
///
|
||||
/// Note that this function returns NaN if the initial value was NaN as
|
||||
/// well.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if `min > max`, `min` is NaN, or `max` is NaN.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// assert!((-3.0f32).clamp(-2.0, 1.0) == -2.0);
|
||||
/// assert!((0.0f32).clamp(-2.0, 1.0) == 0.0);
|
||||
/// assert!((2.0f32).clamp(-2.0, 1.0) == 1.0);
|
||||
/// assert!((f32::NAN).clamp(-2.0, 1.0).is_nan());
|
||||
/// ```
|
||||
#[must_use = "method returns a new number and does not mutate the original value"]
|
||||
#[stable(feature = "clamp", since = "1.50.0")]
|
||||
#[inline]
|
||||
pub fn clamp(self, min: f32, max: f32) -> f32 {
|
||||
assert!(min <= max);
|
||||
let mut x = self;
|
||||
if x < min {
|
||||
x = min;
|
||||
}
|
||||
if x > max {
|
||||
x = max;
|
||||
}
|
||||
x
|
||||
}
|
||||
}
|
||||
|
@ -975,4 +975,39 @@ impl f64 {
|
||||
|
||||
left.cmp(&right)
|
||||
}
|
||||
|
||||
/// Restrict a value to a certain interval unless it is NaN.
|
||||
///
|
||||
/// Returns `max` if `self` is greater than `max`, and `min` if `self` is
|
||||
/// less than `min`. Otherwise this returns `self`.
|
||||
///
|
||||
/// Note that this function returns NaN if the initial value was NaN as
|
||||
/// well.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if `min > max`, `min` is NaN, or `max` is NaN.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// assert!((-3.0f64).clamp(-2.0, 1.0) == -2.0);
|
||||
/// assert!((0.0f64).clamp(-2.0, 1.0) == 0.0);
|
||||
/// assert!((2.0f64).clamp(-2.0, 1.0) == 1.0);
|
||||
/// assert!((f64::NAN).clamp(-2.0, 1.0).is_nan());
|
||||
/// ```
|
||||
#[must_use = "method returns a new number and does not mutate the original value"]
|
||||
#[stable(feature = "clamp", since = "1.50.0")]
|
||||
#[inline]
|
||||
pub fn clamp(self, min: f64, max: f64) -> f64 {
|
||||
assert!(min <= max);
|
||||
let mut x = self;
|
||||
if x < min {
|
||||
x = min;
|
||||
}
|
||||
if x > max {
|
||||
x = max;
|
||||
}
|
||||
x
|
||||
}
|
||||
}
|
||||
|
@ -877,39 +877,4 @@ impl f32 {
|
||||
pub fn atanh(self) -> f32 {
|
||||
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
|
||||
}
|
||||
|
||||
/// Restrict a value to a certain interval unless it is NaN.
|
||||
///
|
||||
/// Returns `max` if `self` is greater than `max`, and `min` if `self` is
|
||||
/// less than `min`. Otherwise this returns `self`.
|
||||
///
|
||||
/// Note that this function returns NaN if the initial value was NaN as
|
||||
/// well.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if `min > max`, `min` is NaN, or `max` is NaN.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// assert!((-3.0f32).clamp(-2.0, 1.0) == -2.0);
|
||||
/// assert!((0.0f32).clamp(-2.0, 1.0) == 0.0);
|
||||
/// assert!((2.0f32).clamp(-2.0, 1.0) == 1.0);
|
||||
/// assert!((f32::NAN).clamp(-2.0, 1.0).is_nan());
|
||||
/// ```
|
||||
#[must_use = "method returns a new number and does not mutate the original value"]
|
||||
#[stable(feature = "clamp", since = "1.50.0")]
|
||||
#[inline]
|
||||
pub fn clamp(self, min: f32, max: f32) -> f32 {
|
||||
assert!(min <= max);
|
||||
let mut x = self;
|
||||
if x < min {
|
||||
x = min;
|
||||
}
|
||||
if x > max {
|
||||
x = max;
|
||||
}
|
||||
x
|
||||
}
|
||||
}
|
||||
|
@ -880,41 +880,6 @@ impl f64 {
|
||||
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
|
||||
}
|
||||
|
||||
/// Restrict a value to a certain interval unless it is NaN.
|
||||
///
|
||||
/// Returns `max` if `self` is greater than `max`, and `min` if `self` is
|
||||
/// less than `min`. Otherwise this returns `self`.
|
||||
///
|
||||
/// Note that this function returns NaN if the initial value was NaN as
|
||||
/// well.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if `min > max`, `min` is NaN, or `max` is NaN.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// assert!((-3.0f64).clamp(-2.0, 1.0) == -2.0);
|
||||
/// assert!((0.0f64).clamp(-2.0, 1.0) == 0.0);
|
||||
/// assert!((2.0f64).clamp(-2.0, 1.0) == 1.0);
|
||||
/// assert!((f64::NAN).clamp(-2.0, 1.0).is_nan());
|
||||
/// ```
|
||||
#[must_use = "method returns a new number and does not mutate the original value"]
|
||||
#[stable(feature = "clamp", since = "1.50.0")]
|
||||
#[inline]
|
||||
pub fn clamp(self, min: f64, max: f64) -> f64 {
|
||||
assert!(min <= max);
|
||||
let mut x = self;
|
||||
if x < min {
|
||||
x = min;
|
||||
}
|
||||
if x > max {
|
||||
x = max;
|
||||
}
|
||||
x
|
||||
}
|
||||
|
||||
// Solaris/Illumos requires a wrapper around log, log2, and log10 functions
|
||||
// because of their non-standard behavior (e.g., log(-n) returns -Inf instead
|
||||
// of expected NaN).
|
||||
|
Loading…
Reference in New Issue
Block a user