diff --git a/src/libcore/num/f32.rs b/src/libcore/num/f32.rs index 8ff78166a9f2d..d888b7505d502 100644 --- a/src/libcore/num/f32.rs +++ b/src/libcore/num/f32.rs @@ -576,4 +576,31 @@ impl f32 { pub fn from_ne_bytes(bytes: [u8; 4]) -> Self { Self::from_bits(u32::from_ne_bytes(bytes)) } + + /// Takes the square root of a number. + /// + /// Returns NaN if `self` is a negative number. + /// + /// # Examples + /// + /// ``` + /// use core::f32; + /// + /// let positive = 4.0_f32; + /// let negative = -4.0_f32; + /// + /// let abs_difference = (positive.sqrt() - 2.0).abs(); + /// + /// assert!(abs_difference <= f32::EPSILON); + /// assert!(negative.sqrt().is_nan()); + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn sqrt(self) -> f32 { + if self < 0.0 { + NAN + } else { + unsafe { intrinsics::sqrtf32(self) } + } + } } diff --git a/src/libcore/num/f64.rs b/src/libcore/num/f64.rs index d45c04f45afc2..48a57d85fcad9 100644 --- a/src/libcore/num/f64.rs +++ b/src/libcore/num/f64.rs @@ -589,4 +589,29 @@ impl f64 { pub fn from_ne_bytes(bytes: [u8; 8]) -> Self { Self::from_bits(u64::from_ne_bytes(bytes)) } + + /// Takes the square root of a number. + /// + /// Returns NaN if `self` is a negative number. + /// + /// # Examples + /// + /// ``` + /// let positive = 4.0_f64; + /// let negative = -4.0_f64; + /// + /// let abs_difference = (positive.sqrt() - 2.0).abs(); + /// + /// assert!(abs_difference < 1e-10); + /// assert!(negative.sqrt().is_nan()); + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn sqrt(self) -> f64 { + if self < 0.0 { + NAN + } else { + unsafe { intrinsics::sqrtf64(self) } + } + } } diff --git a/src/libstd/f32.rs b/src/libstd/f32.rs index f649170c40372..52c8a4bb778e2 100644 --- a/src/libstd/f32.rs +++ b/src/libstd/f32.rs @@ -350,33 +350,6 @@ impl f32 { return unsafe { intrinsics::powf32(self, n) }; } - /// Takes the square root of a number. - /// - /// Returns NaN if `self` is a negative number. - /// - /// # Examples - /// - /// ``` - /// use std::f32; - /// - /// let positive = 4.0_f32; - /// let negative = -4.0_f32; - /// - /// let abs_difference = (positive.sqrt() - 2.0).abs(); - /// - /// assert!(abs_difference <= f32::EPSILON); - /// assert!(negative.sqrt().is_nan()); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn sqrt(self) -> f32 { - if self < 0.0 { - NAN - } else { - unsafe { intrinsics::sqrtf32(self) } - } - } - /// Returns `e^(self)`, (the exponential function). /// /// # Examples diff --git a/src/libstd/f64.rs b/src/libstd/f64.rs index f61630997dcdb..bf488a5c67c82 100644 --- a/src/libstd/f64.rs +++ b/src/libstd/f64.rs @@ -317,31 +317,6 @@ impl f64 { unsafe { intrinsics::powf64(self, n) } } - /// Takes the square root of a number. - /// - /// Returns NaN if `self` is a negative number. - /// - /// # Examples - /// - /// ``` - /// let positive = 4.0_f64; - /// let negative = -4.0_f64; - /// - /// let abs_difference = (positive.sqrt() - 2.0).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// assert!(negative.sqrt().is_nan()); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - #[inline] - pub fn sqrt(self) -> f64 { - if self < 0.0 { - NAN - } else { - unsafe { intrinsics::sqrtf64(self) } - } - } - /// Returns `e^(self)`, (the exponential function). /// /// # Examples