Skip to content

Commit 5936278

Browse files
committed
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.
2 parents 2067dd2 + 8379062 commit 5936278

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

src/libstd/num/f32.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl Float for f32 {
173173
#[inline]
174174
fn exp(self) -> f32 { num::Float::exp(self) }
175175
#[inline]
176-
fn exp2(self) -> f32 { num::Float::exp(self) }
176+
fn exp2(self) -> f32 { num::Float::exp2(self) }
177177
#[inline]
178178
fn ln(self) -> f32 { num::Float::ln(self) }
179179
#[inline]
@@ -554,6 +554,33 @@ mod tests {
554554
assert_approx_eq!((-1.7f32).fract(), -0.7f32);
555555
}
556556

557+
#[test]
558+
fn test_exp() {
559+
assert_eq!(1.0, 0.0f32.exp());
560+
assert_approx_eq!(2.718282, 1.0f32.exp());
561+
assert_approx_eq!(148.413162, 5.0f32.exp());
562+
563+
let inf: f32 = Float::infinity();
564+
let neg_inf: f32 = Float::neg_infinity();
565+
let nan: f32 = Float::nan();
566+
assert_eq!(inf, inf.exp());
567+
assert_eq!(0.0, neg_inf.exp());
568+
assert!(nan.exp().is_nan());
569+
}
570+
571+
#[test]
572+
fn test_exp2() {
573+
assert_eq!(32.0, 5.0f32.exp2());
574+
assert_eq!(1.0, 0.0f32.exp2());
575+
576+
let inf: f32 = Float::infinity();
577+
let neg_inf: f32 = Float::neg_infinity();
578+
let nan: f32 = Float::nan();
579+
assert_eq!(inf, inf.exp2());
580+
assert_eq!(0.0, neg_inf.exp2());
581+
assert!(nan.exp2().is_nan());
582+
}
583+
557584
#[test]
558585
fn test_asinh() {
559586
assert_eq!(0.0f32.asinh(), 0.0f32);

src/libstd/num/f64.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl Float for f64 {
183183
#[inline]
184184
fn exp(self) -> f64 { num::Float::exp(self) }
185185
#[inline]
186-
fn exp2(self) -> f64 { num::Float::exp(self) }
186+
fn exp2(self) -> f64 { num::Float::exp2(self) }
187187
#[inline]
188188
fn ln(self) -> f64 { num::Float::ln(self) }
189189
#[inline]
@@ -563,6 +563,33 @@ mod tests {
563563
assert_approx_eq!((-1.7f64).fract(), -0.7f64);
564564
}
565565

566+
#[test]
567+
fn test_exp() {
568+
assert_eq!(1.0, 0.0f64.exp());
569+
assert_approx_eq!(2.718282, 1.0f64.exp());
570+
assert_approx_eq!(148.413159, 5.0f64.exp());
571+
572+
let inf: f64 = Float::infinity();
573+
let neg_inf: f64 = Float::neg_infinity();
574+
let nan: f64 = Float::nan();
575+
assert_eq!(inf, inf.exp());
576+
assert_eq!(0.0, neg_inf.exp());
577+
assert!(nan.exp().is_nan());
578+
}
579+
580+
#[test]
581+
fn test_exp2() {
582+
assert_eq!(32.0, 5.0f64.exp2());
583+
assert_eq!(1.0, 0.0f64.exp2());
584+
585+
let inf: f64 = Float::infinity();
586+
let neg_inf: f64 = Float::neg_infinity();
587+
let nan: f64 = Float::nan();
588+
assert_eq!(inf, inf.exp2());
589+
assert_eq!(0.0, neg_inf.exp2());
590+
assert!(nan.exp2().is_nan());
591+
}
592+
566593
#[test]
567594
fn test_asinh() {
568595
assert_eq!(0.0f64.asinh(), 0.0f64);

0 commit comments

Comments
 (0)