From c2d05532d3a60fe3ba987bf12644368aff599af6 Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Thu, 10 Sep 2026 10:27:51 -0400 Subject: [PATCH] Fix signs of cosh and sinh near the overflow boundary. Resovles https://github.com/apple/swift-numerics/issues/347 --- .../ComplexModule/Complex+ElementaryFunctions.swift | 12 +++++++++--- Tests/ComplexTests/ElementaryFunctionTests.swift | 12 ++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Sources/ComplexModule/Complex+ElementaryFunctions.swift b/Sources/ComplexModule/Complex+ElementaryFunctions.swift index 38c8cdc..22a9fee 100644 --- a/Sources/ComplexModule/Complex+ElementaryFunctions.swift +++ b/Sources/ComplexModule/Complex+ElementaryFunctions.swift @@ -141,7 +141,10 @@ extension Complex: ElementaryFunctions { public static func cosh(_ z: Complex) -> Complex { guard z.isFinite else { return z } guard z.x.magnitude < -RealType.log(.ulpOfOne) else { - let phase = Complex(RealType.cos(z.y), RealType.sin(z.y)) + let phase = Complex( + RealType.cos(z.y), + RealType(signOf: z.x, magnitudeOf: 1) * RealType.sin(z.y) + ) let firstScale = RealType.exp(z.x.magnitude/2) let secondScale = firstScale/2 return phase.multiplied(by: firstScale).multiplied(by: secondScale) @@ -171,9 +174,12 @@ extension Complex: ElementaryFunctions { public static func sinh(_ z: Complex) -> Complex { guard z.isFinite else { return z } guard z.x.magnitude < -RealType.log(.ulpOfOne) else { - let phase = Complex(RealType.cos(z.y), RealType.sin(z.y)) + let phase = Complex( + RealType(signOf: z.x, magnitudeOf: RealType.cos(z.y)), + RealType.sin(z.y) + ) let firstScale = RealType.exp(z.x.magnitude/2) - let secondScale = RealType(signOf: z.x, magnitudeOf: firstScale/2) + let secondScale = firstScale/2 return phase.multiplied(by: firstScale).multiplied(by: secondScale) } return Complex( diff --git a/Tests/ComplexTests/ElementaryFunctionTests.swift b/Tests/ComplexTests/ElementaryFunctionTests.swift index 2dd9b15..a4e8399 100644 --- a/Tests/ComplexTests/ElementaryFunctionTests.swift +++ b/Tests/ComplexTests/ElementaryFunctionTests.swift @@ -180,7 +180,13 @@ final class ElementaryFunctionTests: XCTestCase { XCTAssert(huge.imaginary.isApproximatelyEqual(to: mag)) huge = Complex.cosh(Complex(-x, .pi/4)) XCTAssert(huge.real.isApproximatelyEqual(to: mag)) + XCTAssert(huge.imaginary.isApproximatelyEqual(to: -mag)) + huge = Complex.cosh(Complex(-x, -.pi/4)) + XCTAssert(huge.real.isApproximatelyEqual(to: mag)) XCTAssert(huge.imaginary.isApproximatelyEqual(to: mag)) + huge = Complex.cosh(Complex(x, -.pi/4)) + XCTAssert(huge.real.isApproximatelyEqual(to: mag)) + XCTAssert(huge.imaginary.isApproximatelyEqual(to: -mag)) } func testSinh(_ type: T.Type) { @@ -212,6 +218,12 @@ final class ElementaryFunctionTests: XCTestCase { XCTAssert(huge.imaginary.isApproximatelyEqual(to: mag)) huge = Complex.sinh(Complex(-x, .pi/4)) XCTAssert(huge.real.isApproximatelyEqual(to: -mag)) + XCTAssert(huge.imaginary.isApproximatelyEqual(to: mag)) + huge = Complex.sinh(Complex(-x, -.pi/4)) + XCTAssert(huge.real.isApproximatelyEqual(to: -mag)) + XCTAssert(huge.imaginary.isApproximatelyEqual(to: -mag)) + huge = Complex.sinh(Complex(x, -.pi/4)) + XCTAssert(huge.real.isApproximatelyEqual(to: mag)) XCTAssert(huge.imaginary.isApproximatelyEqual(to: -mag)) // For randomly-chosen well-scaled finite values, we expect to have // cosh² - sinh² ≈ 1. Note that this test would break down due to