diff --git a/Sources/ComplexModule/Complex+ElementaryFunctions.swift b/Sources/ComplexModule/Complex+ElementaryFunctions.swift index efa685b..712ddf2 100644 --- a/Sources/ComplexModule/Complex+ElementaryFunctions.swift +++ b/Sources/ComplexModule/Complex+ElementaryFunctions.swift @@ -420,7 +420,9 @@ extension Complex: ElementaryFunctions { @inlinable public static func pow(_ z: Complex, _ n: Int) -> Complex { - if z.isZero { return .zero } + if z.isZero { + return n < 0 ? .infinity : n == 0 ? .one : .zero + } // TODO: this implementation is not quite correct, because n may be // rounded in conversion to RealType. This only effects very extreme // cases, so we'll leave it alone for now. diff --git a/Tests/ComplexTests/ElementaryFunctionTests.swift b/Tests/ComplexTests/ElementaryFunctionTests.swift index bf3f15b..2872636 100644 --- a/Tests/ComplexTests/ElementaryFunctionTests.swift +++ b/Tests/ComplexTests/ElementaryFunctionTests.swift @@ -399,6 +399,18 @@ final class ElementaryFunctionTests: XCTestCase { } } } + + func testPowR(_ type: T.Type) { + XCTAssertEqual(Complex.pow(.zero, -.one), .infinity) + XCTAssertEqual(Complex.pow(.zero, .zero), .infinity) + XCTAssertEqual(Complex.pow(.zero, +.one), .infinity) + } + + func testPowN(_ type: T.Type) { + XCTAssertEqual(Complex.pow(.zero, -1), .infinity) + XCTAssertEqual(Complex.pow(.zero, 0), .one) + XCTAssertEqual(Complex.pow(.zero, +1), .zero) + } func testFloat() { testExp(Float.self) @@ -411,6 +423,8 @@ final class ElementaryFunctionTests: XCTestCase { testAcosh(Float.self) testAsinh(Float.self) testAtanh(Float.self) + testPowR(Float.self) + testPowN(Float.self) } func testDouble() { @@ -424,6 +438,8 @@ final class ElementaryFunctionTests: XCTestCase { testAcosh(Double.self) testAsinh(Double.self) testAtanh(Double.self) + testPowR(Double.self) + testPowN(Double.self) } #if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android) @@ -438,6 +454,8 @@ final class ElementaryFunctionTests: XCTestCase { testAcosh(Float80.self) testAsinh(Float80.self) testAtanh(Float80.self) + testPowR(Float80.self) + testPowN(Float80.self) } #endif }