Skip to content

Commit ff929e5

Browse files
Rollup merge of rust-lang#59327 - Xaeroxe:clamp-doc, r=scottmcm
Add NAN test to docs Documents and tests NAN behavior for the new (f32, f64)::clamp function.
2 parents 5fa6896 + 72f5d91 commit ff929e5

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/libstd/f32.rs

+19
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,7 @@ impl f32 {
970970
/// assert!((-3.0f32).clamp(-2.0f32, 1.0f32) == -2.0f32);
971971
/// assert!((0.0f32).clamp(-2.0f32, 1.0f32) == 0.0f32);
972972
/// assert!((2.0f32).clamp(-2.0f32, 1.0f32) == 1.0f32);
973+
/// assert!((std::f32::NAN).clamp(-2.0f32, 1.0f32).is_nan());
973974
/// ```
974975
#[unstable(feature = "clamp", issue = "44095")]
975976
#[inline]
@@ -1581,4 +1582,22 @@ mod tests {
15811582
assert_eq!(f32::from_bits(masked_nan1).to_bits(), masked_nan1);
15821583
assert_eq!(f32::from_bits(masked_nan2).to_bits(), masked_nan2);
15831584
}
1585+
1586+
#[test]
1587+
#[should_panic]
1588+
fn test_clamp_min_greater_than_max() {
1589+
1.0f32.clamp(3.0, 1.0);
1590+
}
1591+
1592+
#[test]
1593+
#[should_panic]
1594+
fn test_clamp_min_is_nan() {
1595+
1.0f32.clamp(NAN, 1.0);
1596+
}
1597+
1598+
#[test]
1599+
#[should_panic]
1600+
fn test_clamp_max_is_nan() {
1601+
1.0f32.clamp(3.0, NAN);
1602+
}
15841603
}

src/libstd/f64.rs

+19
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,7 @@ impl f64 {
892892
/// assert!((-3.0f64).clamp(-2.0f64, 1.0f64) == -2.0f64);
893893
/// assert!((0.0f64).clamp(-2.0f64, 1.0f64) == 0.0f64);
894894
/// assert!((2.0f64).clamp(-2.0f64, 1.0f64) == 1.0f64);
895+
/// assert!((std::f64::NAN).clamp(-2.0f64, 1.0f64).is_nan());
895896
/// ```
896897
#[unstable(feature = "clamp", issue = "44095")]
897898
#[inline]
@@ -1522,4 +1523,22 @@ mod tests {
15221523
assert_eq!(f64::from_bits(masked_nan1).to_bits(), masked_nan1);
15231524
assert_eq!(f64::from_bits(masked_nan2).to_bits(), masked_nan2);
15241525
}
1526+
1527+
#[test]
1528+
#[should_panic]
1529+
fn test_clamp_min_greater_than_max() {
1530+
1.0f64.clamp(3.0, 1.0);
1531+
}
1532+
1533+
#[test]
1534+
#[should_panic]
1535+
fn test_clamp_min_is_nan() {
1536+
1.0f64.clamp(NAN, 1.0);
1537+
}
1538+
1539+
#[test]
1540+
#[should_panic]
1541+
fn test_clamp_max_is_nan() {
1542+
1.0f64.clamp(3.0, NAN);
1543+
}
15251544
}

0 commit comments

Comments
 (0)