Skip to content

Commit 6c4accd

Browse files
Merge pull request #236 from stephentyrone/further-complex-pow-tweaks
Further improvement to Complex.pow(_:Self,_:Self) edge cases.
2 parents 89cfa62 + 60a3edb commit 6c4accd

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

Sources/ComplexModule/Complex+ElementaryFunctions.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,16 +413,21 @@ extension Complex: ElementaryFunctions {
413413
}
414414

415415
// MARK: - pow-like functions
416+
/// `exp(w*log(z))`
417+
///
418+
/// Edge cases for this function are defined according to the defining
419+
/// expression exp(w log(z)), except that we define pow(0, w) to be 0
420+
/// instead of infinity when w is in the (strict) right half-plane, so that
421+
/// we agree with RealType.pow on the positive real line.
416422
@inlinable
417423
public static func pow(_ z: Complex, _ w: Complex) -> Complex {
424+
if z.isZero { return w.real > 0 ? zero : infinity }
418425
return exp(w * log(z))
419426
}
420427

421428
@inlinable
422429
public static func pow(_ z: Complex, _ n: Int) -> Complex {
423-
if z.isZero {
424-
return n < 0 ? .infinity : n == 0 ? .one : .zero
425-
}
430+
if z.isZero { return n < 0 ? infinity : n == 0 ? one : zero }
426431
// TODO: this implementation is not quite correct, because n may be
427432
// rounded in conversion to RealType. This only effects very extreme
428433
// cases, so we'll leave it alone for now.

Tests/ComplexTests/ElementaryFunctionTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ final class ElementaryFunctionTests: XCTestCase {
403403
func testPowR<T: Real & FixedWidthFloatingPoint>(_ type: T.Type) {
404404
XCTAssertEqual(Complex<T>.pow(.zero, -.one), .infinity)
405405
XCTAssertEqual(Complex<T>.pow(.zero, .zero), .infinity)
406-
XCTAssertEqual(Complex<T>.pow(.zero, +.one), .infinity)
406+
XCTAssertEqual(Complex<T>.pow(.zero, +.one), .zero)
407407
}
408408

409409
func testPowN<T: Real & FixedWidthFloatingPoint>(_ type: T.Type) {

0 commit comments

Comments
 (0)