Skip to content

Commit 13c2640

Browse files
authored
Rollup merge of #149238 - RalfJung:clamp-signed-zeros, r=Amanieu
float::clamp: make treatment of signed zeros unspecified Fixes #83984 by explicitly documenting that we do not specify the treatment of signed zeros in `clamp`. `@rust-lang/libs-api` Is this what you'd like to see? Cc `@tgross35` `@thomcc`
2 parents 1be6b13 + 69d3218 commit 13c2640

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

library/core/src/num/f128.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,8 @@ impl f128 {
12361236
/// less than `min`. Otherwise this returns `self`.
12371237
///
12381238
/// Note that this function returns NaN if the initial value was NaN as
1239-
/// well.
1239+
/// well. If the result is zero and among the three inputs `self`, `min`, and `max` there are
1240+
/// zeros with different sign, either `0.0` or `-0.0` is returned non-deterministically.
12401241
///
12411242
/// # Panics
12421243
///
@@ -1253,6 +1254,12 @@ impl f128 {
12531254
/// assert!((0.0f128).clamp(-2.0, 1.0) == 0.0);
12541255
/// assert!((2.0f128).clamp(-2.0, 1.0) == 1.0);
12551256
/// assert!((f128::NAN).clamp(-2.0, 1.0).is_nan());
1257+
///
1258+
/// // These always returns zero, but the sign (which is ignored by `==`) is non-deterministic.
1259+
/// assert!((0.0f128).clamp(-0.0, -0.0) == 0.0);
1260+
/// assert!((1.0f128).clamp(-0.0, 0.0) == 0.0);
1261+
/// // This is definitely a negative zero.
1262+
/// assert!((-1.0f128).clamp(-0.0, 1.0).is_sign_negative());
12561263
/// # }
12571264
/// ```
12581265
#[inline]

library/core/src/num/f16.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,8 @@ impl f16 {
12151215
/// less than `min`. Otherwise this returns `self`.
12161216
///
12171217
/// Note that this function returns NaN if the initial value was NaN as
1218-
/// well.
1218+
/// well. If the result is zero and among the three inputs `self`, `min`, and `max` there are
1219+
/// zeros with different sign, either `0.0` or `-0.0` is returned non-deterministically.
12191220
///
12201221
/// # Panics
12211222
///
@@ -1231,6 +1232,12 @@ impl f16 {
12311232
/// assert!((0.0f16).clamp(-2.0, 1.0) == 0.0);
12321233
/// assert!((2.0f16).clamp(-2.0, 1.0) == 1.0);
12331234
/// assert!((f16::NAN).clamp(-2.0, 1.0).is_nan());
1235+
///
1236+
/// // These always returns zero, but the sign (which is ignored by `==`) is non-deterministic.
1237+
/// assert!((0.0f16).clamp(-0.0, -0.0) == 0.0);
1238+
/// assert!((1.0f16).clamp(-0.0, 0.0) == 0.0);
1239+
/// // This is definitely a negative zero.
1240+
/// assert!((-1.0f16).clamp(-0.0, 1.0).is_sign_negative());
12341241
/// # }
12351242
/// ```
12361243
#[inline]

library/core/src/num/f32.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,8 @@ impl f32 {
13951395
/// less than `min`. Otherwise this returns `self`.
13961396
///
13971397
/// Note that this function returns NaN if the initial value was NaN as
1398-
/// well.
1398+
/// well. If the result is zero and among the three inputs `self`, `min`, and `max` there are
1399+
/// zeros with different sign, either `0.0` or `-0.0` is returned non-deterministically.
13991400
///
14001401
/// # Panics
14011402
///
@@ -1408,6 +1409,12 @@ impl f32 {
14081409
/// assert!((0.0f32).clamp(-2.0, 1.0) == 0.0);
14091410
/// assert!((2.0f32).clamp(-2.0, 1.0) == 1.0);
14101411
/// assert!((f32::NAN).clamp(-2.0, 1.0).is_nan());
1412+
///
1413+
/// // These always returns zero, but the sign (which is ignored by `==`) is non-deterministic.
1414+
/// assert!((0.0f32).clamp(-0.0, -0.0) == 0.0);
1415+
/// assert!((1.0f32).clamp(-0.0, 0.0) == 0.0);
1416+
/// // This is definitely a negative zero.
1417+
/// assert!((-1.0f32).clamp(-0.0, 1.0).is_sign_negative());
14111418
/// ```
14121419
#[must_use = "method returns a new number and does not mutate the original value"]
14131420
#[stable(feature = "clamp", since = "1.50.0")]

library/core/src/num/f64.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,8 @@ impl f64 {
13931393
/// less than `min`. Otherwise this returns `self`.
13941394
///
13951395
/// Note that this function returns NaN if the initial value was NaN as
1396-
/// well.
1396+
/// well. If the result is zero and among the three inputs `self`, `min`, and `max` there are
1397+
/// zeros with different sign, either `0.0` or `-0.0` is returned non-deterministically.
13971398
///
13981399
/// # Panics
13991400
///
@@ -1406,6 +1407,12 @@ impl f64 {
14061407
/// assert!((0.0f64).clamp(-2.0, 1.0) == 0.0);
14071408
/// assert!((2.0f64).clamp(-2.0, 1.0) == 1.0);
14081409
/// assert!((f64::NAN).clamp(-2.0, 1.0).is_nan());
1410+
///
1411+
/// // These always returns zero, but the sign (which is ignored by `==`) is non-deterministic.
1412+
/// assert!((0.0f64).clamp(-0.0, -0.0) == 0.0);
1413+
/// assert!((1.0f64).clamp(-0.0, 0.0) == 0.0);
1414+
/// // This is definitely a negative zero.
1415+
/// assert!((-1.0f64).clamp(-0.0, 1.0).is_sign_negative());
14091416
/// ```
14101417
#[must_use = "method returns a new number and does not mutate the original value"]
14111418
#[stable(feature = "clamp", since = "1.50.0")]

0 commit comments

Comments
 (0)