about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-10-20 10:17:09 +0000
committerbors <bors@rust-lang.org>2014-10-20 10:17:09 +0000
commitddfe24d641261638961448959a07326dde09ef4d (patch)
treedd420ba7bcb3a774626de41e2c82856321916d6a
parent045bc283eceb414caeea28797b9b610bd33f33ac (diff)
parenta1d5cd204d75156ea520c361da6dbb45b926b4d0 (diff)
downloadrust-ddfe24d641261638961448959a07326dde09ef4d.tar.gz
rust-ddfe24d641261638961448959a07326dde09ef4d.zip
auto merge of #18174 : huonw/rust/fix-sqrt, r=alexcrichton
Closes #9987.
-rw-r--r--src/libcore/num/f32.rs6
-rw-r--r--src/libcore/num/f64.rs7
-rw-r--r--src/libcore/num/mod.rs2
-rw-r--r--src/libstd/num/f32.rs11
-rw-r--r--src/libstd/num/f64.rs11
5 files changed, 34 insertions, 3 deletions
diff --git a/src/libcore/num/f32.rs b/src/libcore/num/f32.rs
index b439f5577cb..bf362928f61 100644
--- a/src/libcore/num/f32.rs
+++ b/src/libcore/num/f32.rs
@@ -259,7 +259,11 @@ impl Float for f32 {
 
     #[inline]
     fn sqrt(self) -> f32 {
-        unsafe { intrinsics::sqrtf32(self) }
+        if self < 0.0 {
+            NAN
+        } else {
+            unsafe { intrinsics::sqrtf32(self) }
+        }
     }
 
     #[inline]
diff --git a/src/libcore/num/f64.rs b/src/libcore/num/f64.rs
index d980009c0db..5ad2e2f9f8b 100644
--- a/src/libcore/num/f64.rs
+++ b/src/libcore/num/f64.rs
@@ -266,7 +266,11 @@ impl Float for f64 {
 
     #[inline]
     fn sqrt(self) -> f64 {
-        unsafe { intrinsics::sqrtf64(self) }
+        if self < 0.0 {
+            NAN
+        } else {
+            unsafe { intrinsics::sqrtf64(self) }
+        }
     }
 
     #[inline]
@@ -377,4 +381,3 @@ impl Float for f64 {
         self * (value / 180.0)
     }
 }
-
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs
index 99d9d1df522..42ea138acd4 100644
--- a/src/libcore/num/mod.rs
+++ b/src/libcore/num/mod.rs
@@ -1501,6 +1501,8 @@ pub trait Float: Signed + Primitive {
     fn frac_1_sqrt2() -> Self;
 
     /// Take the square root of a number.
+    ///
+    /// Returns NaN if `self` is not a non-negative number.
     fn sqrt(self) -> Self;
     /// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`.
     fn rsqrt(self) -> Self;
diff --git a/src/libstd/num/f32.rs b/src/libstd/num/f32.rs
index b2a9f1b7b20..f98e81bb2c8 100644
--- a/src/libstd/num/f32.rs
+++ b/src/libstd/num/f32.rs
@@ -787,4 +787,15 @@ mod tests {
         assert_eq!(NEG_INFINITY.integer_decode(), (8388608u64, 105i16, -1i8));
         assert_eq!(NAN.integer_decode(), (12582912u64, 105i16, 1i8));
     }
+
+    #[test]
+    fn test_sqrt_domain() {
+        assert!(NAN.sqrt().is_nan());
+        assert!(NEG_INFINITY.sqrt().is_nan());
+        assert!((-1.0f32).sqrt().is_nan());
+        assert_eq!((-0.0f32).sqrt(), -0.0);
+        assert_eq!(0.0f32.sqrt(), 0.0);
+        assert_eq!(1.0f32.sqrt(), 1.0);
+        assert_eq!(INFINITY.sqrt(), INFINITY);
+    }
 }
diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs
index 6fe9fcad2aa..5a5ca65a36d 100644
--- a/src/libstd/num/f64.rs
+++ b/src/libstd/num/f64.rs
@@ -789,4 +789,15 @@ mod tests {
         assert_eq!(NEG_INFINITY.integer_decode(), (4503599627370496, 972, -1));
         assert_eq!(NAN.integer_decode(), (6755399441055744u64, 972i16, 1i8));
     }
+
+    #[test]
+    fn test_sqrt_domain() {
+        assert!(NAN.sqrt().is_nan());
+        assert!(NEG_INFINITY.sqrt().is_nan());
+        assert!((-1.0f64).sqrt().is_nan());
+        assert_eq!((-0.0f64).sqrt(), -0.0);
+        assert_eq!(0.0f64.sqrt(), 0.0);
+        assert_eq!(1.0f64.sqrt(), 1.0);
+        assert_eq!(INFINITY.sqrt(), INFINITY);
+    }
 }