Skip to content

Commit aeb4276

Browse files
committed
Panic on NaN
1 parent f6e951e commit aeb4276

5 files changed

Lines changed: 35 additions & 22 deletions

File tree

library/core/src/cmp/clamp.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ macro impl_for_float($t:ty) {
6767
#[rustc_const_unstable(feature = "clamp_bounds", issue = "147781")]
6868
impl const ClampBounds<$t> for RangeFrom<$t> {
6969
fn clamp(self, value: $t) -> $t {
70+
assert!(!self.start.is_nan(), "min was NaN");
7071
value.max(self.start)
7172
}
7273
}
@@ -75,6 +76,7 @@ macro impl_for_float($t:ty) {
7576
#[rustc_const_unstable(feature = "clamp_bounds", issue = "147781")]
7677
impl const ClampBounds<$t> for RangeToInclusive<$t> {
7778
fn clamp(self, value: $t) -> $t {
79+
assert!(!self.end.is_nan(), "max was NaN");
7880
value.min(self.end)
7981
}
8082
}
@@ -84,8 +86,7 @@ macro impl_for_float($t:ty) {
8486
impl const ClampBounds<$t> for RangeInclusive<$t> {
8587
fn clamp(self, value: $t) -> $t {
8688
let (start, end) = self.into_inner();
87-
// Deliberately avoid using `clamp` to handle NaN consistently
88-
value.max(start).min(end)
89+
value.clamp(start, end)
8990
}
9091
}
9192
}

library/core/src/num/f128.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,17 +1325,20 @@ impl f128 {
13251325
self.clamp(-limit, limit)
13261326
}
13271327

1328-
/// Restrict a value to a certain range.
1328+
/// Restrict a value to a certain range, unless it is NaN.
13291329
///
13301330
/// This is largely equal to `max`, `min`, or `clamp`, depending on whether the range is
1331-
/// `min..`, `..=max`, or `min..=max`, respectively. However, whereas `clamp` panics on NaN
1332-
/// values, this function treats them as unbounded, like `max` and `min`.
1331+
/// `min..`, `..=max`, or `min..=max`, respectively. However, unlike `max` and `min`, it will
1332+
/// panic if any bound is NaN.
1333+
///
1334+
/// Note that this function returns NaN if the initial value was NaN as
1335+
/// well.
13331336
///
13341337
/// Exclusive ranges are not permitted.
13351338
///
13361339
/// # Panics
13371340
///
1338-
/// Panics on `min..=max` if `min > max`.
1341+
/// Panics on `min..=max` if `min > max`, or if any bound is NaN.
13391342
///
13401343
/// # Examples
13411344
///
@@ -1345,7 +1348,7 @@ impl f128 {
13451348
/// assert_eq!(0.0f128.clamp_to(-2.0..=1.0), 0.0);
13461349
/// assert_eq!(2.0f128.clamp_to(..=1.0), 1.0);
13471350
/// assert_eq!(5.0f128.clamp_to(7.0..), 7.0);
1348-
/// assert_eq!(4.0f128.clamp_to(1.0..=f128::NAN), 4.0);
1351+
/// assert!(f128::NAN.clamp_to(1.0..=2.0).is_nan());
13491352
/// ```
13501353
#[must_use]
13511354
#[inline]

library/core/src/num/f16.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,17 +1303,20 @@ impl f16 {
13031303
self.clamp(-limit, limit)
13041304
}
13051305

1306-
/// Restrict a value to a certain range.
1306+
/// Restrict a value to a certain range, unless it is NaN.
13071307
///
13081308
/// This is largely equal to `max`, `min`, or `clamp`, depending on whether the range is
1309-
/// `min..`, `..=max`, or `min..=max`, respectively. However, whereas `clamp` panics on NaN
1310-
/// values, this function treats them as unbounded, like `max` and `min`.
1309+
/// `min..`, `..=max`, or `min..=max`, respectively. However, unlike `max` and `min`, it will
1310+
/// panic if any bound is NaN.
1311+
///
1312+
/// Note that this function returns NaN if the initial value was NaN as
1313+
/// well.
13111314
///
13121315
/// Exclusive ranges are not permitted.
13131316
///
13141317
/// # Panics
13151318
///
1316-
/// Panics on `min..=max` if `min > max`.
1319+
/// Panics on `min..=max` if `min > max`, or if any bound is NaN.
13171320
///
13181321
/// # Examples
13191322
///
@@ -1323,7 +1326,7 @@ impl f16 {
13231326
/// assert_eq!(0.0f16.clamp_to(-2.0..=1.0), 0.0);
13241327
/// assert_eq!(2.0f16.clamp_to(..=1.0), 1.0);
13251328
/// assert_eq!(5.0f16.clamp_to(7.0..), 7.0);
1326-
/// assert_eq!(4.0f16.clamp_to(1.0..=f16::NAN), 4.0);
1329+
/// assert!(f16::NAN.clamp_to(1.0..=2.0).is_nan());
13271330
/// ```
13281331
#[must_use]
13291332
#[inline]

library/core/src/num/f32.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,17 +1477,20 @@ impl f32 {
14771477
self.clamp(-limit, limit)
14781478
}
14791479

1480-
/// Restrict a value to a certain range.
1480+
/// Restrict a value to a certain range, unless it is NaN.
14811481
///
14821482
/// This is largely equal to `max`, `min`, or `clamp`, depending on whether the range is
1483-
/// `min..`, `..=max`, or `min..=max`, respectively. However, whereas `clamp` panics on NaN
1484-
/// values, this function treats them as unbounded, like `max` and `min`.
1483+
/// `min..`, `..=max`, or `min..=max`, respectively. However, unlike `max` and `min`, it will
1484+
/// panic if any bound is NaN.
1485+
///
1486+
/// Note that this function returns NaN if the initial value was NaN as
1487+
/// well.
14851488
///
14861489
/// Exclusive ranges are not permitted.
14871490
///
14881491
/// # Panics
14891492
///
1490-
/// Panics on `min..=max` if `min > max`.
1493+
/// Panics on `min..=max` if `min > max`, or if any bound is NaN.
14911494
///
14921495
/// # Examples
14931496
///
@@ -1497,7 +1500,7 @@ impl f32 {
14971500
/// assert_eq!(0.0f32.clamp_to(-2.0..=1.0), 0.0);
14981501
/// assert_eq!(2.0f32.clamp_to(..=1.0), 1.0);
14991502
/// assert_eq!(5.0f32.clamp_to(7.0..), 7.0);
1500-
/// assert_eq!(4.0f32.clamp_to(1.0..=f32::NAN), 4.0);
1503+
/// assert!(f32::NAN.clamp_to(1.0..=2.0).is_nan());
15011504
/// ```
15021505
#[must_use]
15031506
#[inline]

library/core/src/num/f64.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,17 +1475,20 @@ impl f64 {
14751475
self.clamp(-limit, limit)
14761476
}
14771477

1478-
/// Restrict a value to a certain range.
1478+
/// Restrict a value to a certain range, unless it is NaN.
14791479
///
14801480
/// This is largely equal to `max`, `min`, or `clamp`, depending on whether the range is
1481-
/// `min..`, `..=max`, or `min..=max`, respectively. However, whereas `clamp` panics on NaN
1482-
/// values, this function treats them as unbounded, like `max` and `min`.
1481+
/// `min..`, `..=max`, or `min..=max`, respectively. However, unlike `max` and `min`, it will
1482+
/// panic if any bound is NaN.
1483+
///
1484+
/// Note that this function returns NaN if the initial value was NaN as
1485+
/// well.
14831486
///
14841487
/// Exclusive ranges are not permitted.
14851488
///
14861489
/// # Panics
14871490
///
1488-
/// Panics on `min..=max` if `min > max`.
1491+
/// Panics on `min..=max` if `min > max`, or if any bound is NaN.
14891492
///
14901493
/// # Examples
14911494
///
@@ -1495,7 +1498,7 @@ impl f64 {
14951498
/// assert_eq!(0.0f64.clamp_to(-2.0..=1.0), 0.0);
14961499
/// assert_eq!(2.0f64.clamp_to(..=1.0), 1.0);
14971500
/// assert_eq!(5.0f64.clamp_to(7.0..), 7.0);
1498-
/// assert_eq!(4.0f64.clamp_to(1.0..=f64::NAN), 4.0);
1501+
/// assert!(f64::NAN.clamp_to(1.0..=2.0).is_nan());
14991502
/// ```
15001503
#[must_use]
15011504
#[inline]

0 commit comments

Comments
 (0)