From 3fa18ae9b5fa382b1d9565f315f0832c8e9de145 Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Thu, 16 May 2024 08:39:12 -0400 Subject: [PATCH] Test improvements Improved how tolerance is applied close to infinity for complex division (this still could use a more principled approach, but at least we're in the right ballpark now). Tightened up the tolerance for semi-exhaustive testing too, since that can be done now. --- .../Complex+AlgebraicField.swift | 2 ++ Tests/ComplexTests/ArithmeticTests.swift | 29 ++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/Sources/ComplexModule/Complex+AlgebraicField.swift b/Sources/ComplexModule/Complex+AlgebraicField.swift index b42ada7..57452a4 100644 --- a/Sources/ComplexModule/Complex+AlgebraicField.swift +++ b/Sources/ComplexModule/Complex+AlgebraicField.swift @@ -42,6 +42,8 @@ extension Complex: AlgebraicField { return z * w.conjugate.divided(by: lenSq) } + @_alwaysEmitIntoClient // specialization + @inline(never) public static func rescaledDivide(_ z: Complex, _ w: Complex) -> Complex { if w.isZero { return .infinity } if !w.isFinite { return .zero } diff --git a/Tests/ComplexTests/ArithmeticTests.swift b/Tests/ComplexTests/ArithmeticTests.swift index 387b24e..77ac681 100644 --- a/Tests/ComplexTests/ArithmeticTests.swift +++ b/Tests/ComplexTests/ArithmeticTests.swift @@ -13,11 +13,23 @@ import XCTest import ComplexModule import RealModule +func ulpsFromInfinity(_ a: T) -> T { + (.greatestFiniteMagnitude - a) / .greatestFiniteMagnitude.ulp + 1 +} + // TODO: improve this to be a general-purpose complex comparison with tolerance func relativeError(_ a: Complex, _ b: Complex) -> T { if a == b { return 0 } - let scale = max(a.magnitude, b.magnitude, T.leastNormalMagnitude).ulp - return (a - b).magnitude / scale + if a.isFinite && b.isFinite { + let scale = max(a.magnitude, b.magnitude, T.leastNormalMagnitude).ulp + return (a - b).magnitude / scale + } else { + if a.isFinite { + return ulpsFromInfinity(a.magnitude) + } else { + return ulpsFromInfinity(b.magnitude) + } + } } func closeEnough(_ a: T, _ b: T, ulps allowed: T) -> Bool { @@ -32,7 +44,7 @@ func checkMultiply( let rel = relativeError(observed, expected) // Even if the expected result is finite, we allow overflow if // the two-norm of the expected result overflows. - if !expected.length.isFinite && !observed.isFinite { return false } + if !observed.isFinite && !expected.length.isFinite { return false } guard rel <= allowed else { print("Over-large error in \(a)*\(b)") print("Expected: \(expected)\nObserved: \(observed)") @@ -49,7 +61,7 @@ func checkDivide( let rel = relativeError(observed, expected) // Even if the expected result is finite, we allow overflow if // the two-norm of the expected result overflows. - if !expected.length.isFinite && !observed.isFinite { return false } + if !observed.isFinite && !expected.length.isFinite { return false } guard rel <= allowed else { print("Over-large error in \(a)/\(b)") print("Expected: \(expected)\nObserved: \(observed)") @@ -217,9 +229,7 @@ final class ArithmeticTests: XCTestCase { } -#if !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64)) - - /* +#if !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64)) && LONG_TESTS @available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *) func testFloat16DivisionSemiExhaustive() { func complex(bitPattern: UInt32) -> Complex { @@ -233,11 +243,10 @@ final class ArithmeticTests: XCTestCase { if bits & 0xfffff == 0 { print(a) } let b = complex(bitPattern: UInt32.random(in: 0 ... .max)) var q = Complex(a)/Complex(b) - if checkDivide(a, b, expected: Complex(q), ulps: 32) { XCTFail() } + if checkDivide(a, b, expected: Complex(q), ulps: 4) { XCTFail() } q = Complex(b)/Complex(a) - if checkDivide(b, a, expected: Complex(q), ulps: 32) { XCTFail() } + if checkDivide(b, a, expected: Complex(q), ulps: 4) { XCTFail() } } } - */ #endif }