about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-02-11 03:15:00 +0000
committerbors <bors@rust-lang.org>2015-02-11 03:15:00 +0000
commit5936278ed6bef736f6eb8c0dd4d650fd8e10461b (patch)
treea9dc9a5c81825e471ad7daefb59577f1de40e4ab
parent2067dd2a86c059d83377fd2ad87a579846c266e1 (diff)
parent8379062be53e79e5f4dc23753c0152700d20bfac (diff)
downloadrust-5936278ed6bef736f6eb8c0dd4d650fd8e10461b.tar.gz
rust-5936278ed6bef736f6eb8c0dd4d650fd8e10461b.zip
Auto merge of #22076 - carols10cents:exp2-calling-exp, r=huonw
I was working on adding examples to the documentation in `std::num::Float`. I got to `exp2`, which says "Returns 2 raised to the power of the number, `2^(self)`."

So I tried running this code:

```
use std::num::Float;

#[test]
fn test_exp2() {
    assert_eq!(32.0, 5.0.exp2());
}
```

and it resulted in a failure of `(left: `32`, right: `148.413159`)`. That 148.413159 is the value for e^5, which is `exp()`, not `exp2()`.

Sure enough, `exp2` is calling `exp` and shouldn't be, looks like a copy-paste error. 

I haven't added any tests for this since it's unlikely to break again, but I will happily do so if people think that would be a good idea. The doc examples are coming :)

I scanned through the other functions in these files for similar sorts of errors and didn't notice any.
-rw-r--r--src/libstd/num/f32.rs29
-rw-r--r--src/libstd/num/f64.rs29
2 files changed, 56 insertions, 2 deletions
diff --git a/src/libstd/num/f32.rs b/src/libstd/num/f32.rs
index 9b5b0e62a3c..58b93665fe1 100644
--- a/src/libstd/num/f32.rs
+++ b/src/libstd/num/f32.rs
@@ -173,7 +173,7 @@ impl Float for f32 {
     #[inline]
     fn exp(self) -> f32 { num::Float::exp(self) }
     #[inline]
-    fn exp2(self) -> f32 { num::Float::exp(self) }
+    fn exp2(self) -> f32 { num::Float::exp2(self) }
     #[inline]
     fn ln(self) -> f32 { num::Float::ln(self) }
     #[inline]
@@ -555,6 +555,33 @@ mod tests {
     }
 
     #[test]
+    fn test_exp() {
+        assert_eq!(1.0, 0.0f32.exp());
+        assert_approx_eq!(2.718282, 1.0f32.exp());
+        assert_approx_eq!(148.413162, 5.0f32.exp());
+
+        let inf: f32 = Float::infinity();
+        let neg_inf: f32 = Float::neg_infinity();
+        let nan: f32 = Float::nan();
+        assert_eq!(inf, inf.exp());
+        assert_eq!(0.0, neg_inf.exp());
+        assert!(nan.exp().is_nan());
+    }
+
+    #[test]
+    fn test_exp2() {
+        assert_eq!(32.0, 5.0f32.exp2());
+        assert_eq!(1.0, 0.0f32.exp2());
+
+        let inf: f32 = Float::infinity();
+        let neg_inf: f32 = Float::neg_infinity();
+        let nan: f32 = Float::nan();
+        assert_eq!(inf, inf.exp2());
+        assert_eq!(0.0, neg_inf.exp2());
+        assert!(nan.exp2().is_nan());
+    }
+
+    #[test]
     fn test_asinh() {
         assert_eq!(0.0f32.asinh(), 0.0f32);
         assert_eq!((-0.0f32).asinh(), -0.0f32);
diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs
index 1c955832529..8b17feeb70c 100644
--- a/src/libstd/num/f64.rs
+++ b/src/libstd/num/f64.rs
@@ -183,7 +183,7 @@ impl Float for f64 {
     #[inline]
     fn exp(self) -> f64 { num::Float::exp(self) }
     #[inline]
-    fn exp2(self) -> f64 { num::Float::exp(self) }
+    fn exp2(self) -> f64 { num::Float::exp2(self) }
     #[inline]
     fn ln(self) -> f64 { num::Float::ln(self) }
     #[inline]
@@ -564,6 +564,33 @@ mod tests {
     }
 
     #[test]
+    fn test_exp() {
+        assert_eq!(1.0, 0.0f64.exp());
+        assert_approx_eq!(2.718282, 1.0f64.exp());
+        assert_approx_eq!(148.413159, 5.0f64.exp());
+
+        let inf: f64 = Float::infinity();
+        let neg_inf: f64 = Float::neg_infinity();
+        let nan: f64 = Float::nan();
+        assert_eq!(inf, inf.exp());
+        assert_eq!(0.0, neg_inf.exp());
+        assert!(nan.exp().is_nan());
+    }
+
+    #[test]
+    fn test_exp2() {
+        assert_eq!(32.0, 5.0f64.exp2());
+        assert_eq!(1.0, 0.0f64.exp2());
+
+        let inf: f64 = Float::infinity();
+        let neg_inf: f64 = Float::neg_infinity();
+        let nan: f64 = Float::nan();
+        assert_eq!(inf, inf.exp2());
+        assert_eq!(0.0, neg_inf.exp2());
+        assert!(nan.exp2().is_nan());
+    }
+
+    #[test]
     fn test_asinh() {
         assert_eq!(0.0f64.asinh(), 0.0f64);
         assert_eq!((-0.0f64).asinh(), -0.0f64);