Skip to content

Commit 068283b

Browse files
committed
powc and powf to zero power => one
This is consistent with the standard library powf -- even for NaN!
1 parent 21b5ec5 commit 068283b

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ impl<T: Float> Complex<T> {
342342
/// Raises `self` to a floating point power.
343343
#[inline]
344344
pub fn powf(self, exp: T) -> Self {
345+
if exp.is_zero() {
346+
return Self::one();
347+
}
345348
// formula: x^y = (ρ e^(i θ))^y = ρ^y e^(i θ y)
346349
// = from_polar(ρ^y, θ y)
347350
let (r, theta) = self.to_polar();
@@ -361,6 +364,9 @@ impl<T: Float> Complex<T> {
361364
/// Raises `self` to a complex power.
362365
#[inline]
363366
pub fn powc(self, exp: Self) -> Self {
367+
if exp.is_zero() {
368+
return Self::one();
369+
}
364370
// formula: x^y = exp(y * ln(x))
365371
(exp * self.ln()).exp()
366372
}
@@ -1899,13 +1905,17 @@ pub(crate) mod test {
18991905
));
19001906
let z = Complex::new(0.0, 0.0);
19011907
assert!(close(z.powc(b), z));
1902-
assert!(z.powc(Complex64::new(0., 0.)).is_nan());
19031908
assert!(z.powc(Complex64::new(0., INFINITY)).is_nan());
19041909
assert!(z.powc(Complex64::new(10., INFINITY)).is_nan());
19051910
assert!(z.powc(Complex64::new(INFINITY, INFINITY)).is_nan());
19061911
assert!(close(z.powc(Complex64::new(INFINITY, 0.)), z));
19071912
assert!(z.powc(Complex64::new(-1., 0.)).re.is_infinite());
19081913
assert!(z.powc(Complex64::new(-1., 0.)).im.is_nan());
1914+
1915+
for c in all_consts.iter() {
1916+
assert_eq!(c.powc(_0_0i), _1_0i);
1917+
}
1918+
assert_eq!(_nan_nani.powc(_0_0i), _1_0i);
19091919
}
19101920

19111921
#[test]
@@ -1915,6 +1925,11 @@ pub(crate) mod test {
19151925
assert!(close_to_tol(c.powf(3.5), expected, 1e-5));
19161926
assert!(close_to_tol(Pow::pow(c, 3.5_f64), expected, 1e-5));
19171927
assert!(close_to_tol(Pow::pow(c, 3.5_f32), expected, 1e-5));
1928+
1929+
for c in all_consts.iter() {
1930+
assert_eq!(c.powf(0.0), _1_0i);
1931+
}
1932+
assert_eq!(_nan_nani.powf(0.0), _1_0i);
19181933
}
19191934

19201935
#[test]

0 commit comments

Comments
 (0)