@@ -342,6 +342,9 @@ impl<T: Float> Complex<T> {
342
342
/// Raises `self` to a floating point power.
343
343
#[ inline]
344
344
pub fn powf ( self , exp : T ) -> Self {
345
+ if exp. is_zero ( ) {
346
+ return Self :: one ( ) ;
347
+ }
345
348
// formula: x^y = (ρ e^(i θ))^y = ρ^y e^(i θ y)
346
349
// = from_polar(ρ^y, θ y)
347
350
let ( r, theta) = self . to_polar ( ) ;
@@ -361,6 +364,9 @@ impl<T: Float> Complex<T> {
361
364
/// Raises `self` to a complex power.
362
365
#[ inline]
363
366
pub fn powc ( self , exp : Self ) -> Self {
367
+ if exp. is_zero ( ) {
368
+ return Self :: one ( ) ;
369
+ }
364
370
// formula: x^y = exp(y * ln(x))
365
371
( exp * self . ln ( ) ) . exp ( )
366
372
}
@@ -1899,13 +1905,17 @@ pub(crate) mod test {
1899
1905
) ) ;
1900
1906
let z = Complex :: new ( 0.0 , 0.0 ) ;
1901
1907
assert ! ( close( z. powc( b) , z) ) ;
1902
- assert ! ( z. powc( Complex64 :: new( 0. , 0. ) ) . is_nan( ) ) ;
1903
1908
assert ! ( z. powc( Complex64 :: new( 0. , INFINITY ) ) . is_nan( ) ) ;
1904
1909
assert ! ( z. powc( Complex64 :: new( 10. , INFINITY ) ) . is_nan( ) ) ;
1905
1910
assert ! ( z. powc( Complex64 :: new( INFINITY , INFINITY ) ) . is_nan( ) ) ;
1906
1911
assert ! ( close( z. powc( Complex64 :: new( INFINITY , 0. ) ) , z) ) ;
1907
1912
assert ! ( z. powc( Complex64 :: new( -1. , 0. ) ) . re. is_infinite( ) ) ;
1908
1913
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) ;
1909
1919
}
1910
1920
1911
1921
#[ test]
@@ -1915,6 +1925,11 @@ pub(crate) mod test {
1915
1925
assert ! ( close_to_tol( c. powf( 3.5 ) , expected, 1e-5 ) ) ;
1916
1926
assert ! ( close_to_tol( Pow :: pow( c, 3.5_f64 ) , expected, 1e-5 ) ) ;
1917
1927
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) ;
1918
1933
}
1919
1934
1920
1935
#[ test]
0 commit comments