Skip to content

Commit 573cdc4

Browse files
authored
Rollup merge of rust-lang#59410 - tbu-:pr_doc_clarifyclamp, r=joshtriplett
Clarify `{Ord,f32,f64}::clamp` docs a little Explicitly call out when it returns NaN, adhere to the panic doc guidelines.
2 parents 71cb3d1 + 0bb36a2 commit 573cdc4

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

src/libcore/cmp.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,14 @@ pub trait Ord: Eq + PartialOrd<Self> {
568568
if self <= other { self } else { other }
569569
}
570570

571-
/// Returns max if self is greater than max, and min if self is less than min.
572-
/// Otherwise this will return self. Panics if min > max.
571+
/// Restrict a value to a certain interval.
572+
///
573+
/// Returns `max` if `self` is greater than `max`, and `min` if `self` is
574+
/// less than `min`. Otherwise this returns `self`.
575+
///
576+
/// # Panics
577+
///
578+
/// Panics if `min > max`.
573579
///
574580
/// # Examples
575581
///
@@ -586,8 +592,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
586592
assert!(min <= max);
587593
if self < min {
588594
min
589-
}
590-
else if self > max {
595+
} else if self > max {
591596
max
592597
} else {
593598
self

src/libstd/f32.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -960,17 +960,27 @@ impl f32 {
960960
pub fn atanh(self) -> f32 {
961961
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
962962
}
963-
/// Returns max if self is greater than max, and min if self is less than min.
964-
/// Otherwise this returns self. Panics if min > max, min equals NaN, or max equals NaN.
963+
964+
/// Restrict a value to a certain interval unless it is NaN.
965+
///
966+
/// Returns `max` if `self` is greater than `max`, and `min` if `self` is
967+
/// less than `min`. Otherwise this returns `self`.
968+
///
969+
/// Not that this function returns NaN if the initial value was NaN as
970+
/// well.
971+
///
972+
/// # Panics
973+
///
974+
/// Panics if `min > max`, `min` is NaN, or `max` is NaN.
965975
///
966976
/// # Examples
967977
///
968978
/// ```
969979
/// #![feature(clamp)]
970-
/// assert!((-3.0f32).clamp(-2.0f32, 1.0f32) == -2.0f32);
971-
/// assert!((0.0f32).clamp(-2.0f32, 1.0f32) == 0.0f32);
972-
/// assert!((2.0f32).clamp(-2.0f32, 1.0f32) == 1.0f32);
973-
/// assert!((std::f32::NAN).clamp(-2.0f32, 1.0f32).is_nan());
980+
/// assert!((-3.0f32).clamp(-2.0, 1.0) == -2.0);
981+
/// assert!((0.0f32).clamp(-2.0, 1.0) == 0.0);
982+
/// assert!((2.0f32).clamp(-2.0, 1.0) == 1.0);
983+
/// assert!((std::f32::NAN).clamp(-2.0, 1.0).is_nan());
974984
/// ```
975985
#[unstable(feature = "clamp", issue = "44095")]
976986
#[inline]

src/libstd/f64.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -882,17 +882,26 @@ impl f64 {
882882
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
883883
}
884884

885-
/// Returns max if self is greater than max, and min if self is less than min.
886-
/// Otherwise this returns self. Panics if min > max, min equals NaN, or max equals NaN.
885+
/// Restrict a value to a certain interval unless it is NaN.
886+
///
887+
/// Returns `max` if `self` is greater than `max`, and `min` if `self` is
888+
/// less than `min`. Otherwise this returns `self`.
889+
///
890+
/// Not that this function returns NaN if the initial value was NaN as
891+
/// well.
892+
///
893+
/// # Panics
894+
///
895+
/// Panics if `min > max`, `min` is NaN, or `max` is NaN.
887896
///
888897
/// # Examples
889898
///
890899
/// ```
891900
/// #![feature(clamp)]
892-
/// assert!((-3.0f64).clamp(-2.0f64, 1.0f64) == -2.0f64);
893-
/// assert!((0.0f64).clamp(-2.0f64, 1.0f64) == 0.0f64);
894-
/// assert!((2.0f64).clamp(-2.0f64, 1.0f64) == 1.0f64);
895-
/// assert!((std::f64::NAN).clamp(-2.0f64, 1.0f64).is_nan());
901+
/// assert!((-3.0f64).clamp(-2.0, 1.0) == -2.0);
902+
/// assert!((0.0f64).clamp(-2.0, 1.0) == 0.0);
903+
/// assert!((2.0f64).clamp(-2.0, 1.0) == 1.0);
904+
/// assert!((std::f64::NAN).clamp(-2.0, 1.0).is_nan());
896905
/// ```
897906
#[unstable(feature = "clamp", issue = "44095")]
898907
#[inline]

0 commit comments

Comments
 (0)