Skip to content

Commit 5541d1a

Browse files
authored
Rollup merge of #86951 - cyberia-ng:fp-negative-zero-sqrt-docs, r=Mark-Simulacrum
[docs] Clarify behaviour of f64 and f32::sqrt when argument is negative zero From IEEE 754 section 6.3: > Except that squareRoot(−0) shall be −0, every numeric squareRoot result shall have a positive sign.
2 parents a4e7a3c + a853a49 commit 5541d1a

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

library/std/src/f32.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -324,18 +324,20 @@ impl f32 {
324324

325325
/// Returns the square root of a number.
326326
///
327-
/// Returns NaN if `self` is a negative number.
327+
/// Returns NaN if `self` is a negative number other than `-0.0`.
328328
///
329329
/// # Examples
330330
///
331331
/// ```
332332
/// let positive = 4.0_f32;
333333
/// let negative = -4.0_f32;
334+
/// let negative_zero = -0.0_f32;
334335
///
335336
/// let abs_difference = (positive.sqrt() - 2.0).abs();
336337
///
337338
/// assert!(abs_difference <= f32::EPSILON);
338339
/// assert!(negative.sqrt().is_nan());
340+
/// assert!(negative_zero.sqrt() == negative_zero);
339341
/// ```
340342
#[must_use = "method returns a new number and does not mutate the original value"]
341343
#[stable(feature = "rust1", since = "1.0.0")]

library/std/src/f64.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -324,18 +324,20 @@ impl f64 {
324324

325325
/// Returns the square root of a number.
326326
///
327-
/// Returns NaN if `self` is a negative number.
327+
/// Returns NaN if `self` is a negative number other than `-0.0`.
328328
///
329329
/// # Examples
330330
///
331331
/// ```
332332
/// let positive = 4.0_f64;
333333
/// let negative = -4.0_f64;
334+
/// let negative_zero = -0.0_f64;
334335
///
335336
/// let abs_difference = (positive.sqrt() - 2.0).abs();
336337
///
337338
/// assert!(abs_difference < 1e-10);
338339
/// assert!(negative.sqrt().is_nan());
340+
/// assert!(negative_zero.sqrt() == negative_zero);
339341
/// ```
340342
#[must_use = "method returns a new number and does not mutate the original value"]
341343
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)