Skip to content

Commit

Permalink
Merge pull request apple#61 from Wildchild9/master
Browse files Browse the repository at this point in the history
Fixed `Complex` division by zero to return .infinity
  • Loading branch information
stephentyrone authored Nov 9, 2019
2 parents 41bca53 + 6ac60de commit d065a84
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sources/Complex/Arithmetic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ extension Complex: Numeric {

@usableFromInline @_alwaysEmitIntoClient @inline(never)
internal static func rescaledDivide(_ z: Complex, _ w: Complex) -> Complex {
if w.isZero { return .infinity }
if z.isZero || !w.isFinite { return .zero }
// TODO: detect when RealType is Float and just promote to Double, then
// use the naive algorithm.
Expand Down
7 changes: 7 additions & 0 deletions Tests/ComplexTests/ArithmeticTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,11 @@ final class ArithmeticTests: XCTestCase {
if checkMultiply(test.b, test.c, expected: test.a, ulps: 1.0) { XCTFail() }
}
}

func testDivisionByZero() {
XCTAssertFalse((Complex(0, 0) / Complex(0, 0)).isFinite)
XCTAssertFalse((Complex(1, 1) / Complex(0, 0)).isFinite)
XCTAssertFalse((Complex.infinity / Complex(0, 0)).isFinite)
XCTAssertFalse((Complex.i / Complex(0, 0)).isFinite)
}
}

0 comments on commit d065a84

Please sign in to comment.