File tree Expand file tree Collapse file tree 4 files changed +32
-4
lines changed
Expand file tree Collapse file tree 4 files changed +32
-4
lines changed Original file line number Diff line number Diff 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]
Original file line number Diff line number Diff 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]
Original file line number Diff line number Diff 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" ) ]
Original file line number Diff line number Diff 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" ) ]
You can’t perform that action at this time.
0 commit comments