Clarify behaviour of f64 and f32::sqrt when argument is negative zero

This commit is contained in:
cyberia 2021-07-07 18:22:17 +01:00
parent c5e344f774
commit a853a49425
2 changed files with 6 additions and 2 deletions

View File

@ -324,18 +324,20 @@ impl f32 {
/// Returns the square root of a number. /// Returns the square root of a number.
/// ///
/// Returns NaN if `self` is a negative number. /// Returns NaN if `self` is a negative number other than `-0.0`.
/// ///
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// let positive = 4.0_f32; /// let positive = 4.0_f32;
/// let negative = -4.0_f32; /// let negative = -4.0_f32;
/// let negative_zero = -0.0_f32;
/// ///
/// let abs_difference = (positive.sqrt() - 2.0).abs(); /// let abs_difference = (positive.sqrt() - 2.0).abs();
/// ///
/// assert!(abs_difference <= f32::EPSILON); /// assert!(abs_difference <= f32::EPSILON);
/// assert!(negative.sqrt().is_nan()); /// assert!(negative.sqrt().is_nan());
/// assert!(negative_zero.sqrt() == negative_zero);
/// ``` /// ```
#[must_use = "method returns a new number and does not mutate the original value"] #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]

View File

@ -324,18 +324,20 @@ impl f64 {
/// Returns the square root of a number. /// Returns the square root of a number.
/// ///
/// Returns NaN if `self` is a negative number. /// Returns NaN if `self` is a negative number other than `-0.0`.
/// ///
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// let positive = 4.0_f64; /// let positive = 4.0_f64;
/// let negative = -4.0_f64; /// let negative = -4.0_f64;
/// let negative_zero = -0.0_f64;
/// ///
/// let abs_difference = (positive.sqrt() - 2.0).abs(); /// let abs_difference = (positive.sqrt() - 2.0).abs();
/// ///
/// assert!(abs_difference < 1e-10); /// assert!(abs_difference < 1e-10);
/// assert!(negative.sqrt().is_nan()); /// assert!(negative.sqrt().is_nan());
/// assert!(negative_zero.sqrt() == negative_zero);
/// ``` /// ```
#[must_use = "method returns a new number and does not mutate the original value"] #[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]