From e0ec0f5f3af6f3e4d5e7a19d2af26b481acb6ba8 Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Mon, 3 Mar 2025 11:13:12 -0500 Subject: [PATCH] Cherry-pick pow edge case fixes from main. --- .../ComplexModule/ElementaryFunctions.swift | 9 ++++++++- Sources/RealModule/Double+Real.swift | 1 + Sources/RealModule/ElementaryFunctions.swift | 20 +++++++++++-------- Sources/RealModule/Float+Real.swift | 1 + Sources/RealModule/Float80+Real.swift | 1 + .../RealTests/ElementaryFunctionChecks.swift | 20 +++++++++++++++++++ 6 files changed, 43 insertions(+), 9 deletions(-) diff --git a/Sources/ComplexModule/ElementaryFunctions.swift b/Sources/ComplexModule/ElementaryFunctions.swift index de81f32..641a349 100644 --- a/Sources/ComplexModule/ElementaryFunctions.swift +++ b/Sources/ComplexModule/ElementaryFunctions.swift @@ -413,14 +413,21 @@ extension Complex: ElementaryFunctions { } // MARK: - pow-like functions + /// `exp(w*log(z))` + /// + /// Edge cases for this function are defined according to the defining + /// expression exp(w log(z)), except that we define pow(0, w) to be 0 + /// instead of infinity when w is in the (strict) right half-plane, so that + /// we agree with RealType.pow on the positive real line. @inlinable public static func pow(_ z: Complex, _ w: Complex) -> Complex { + if z.isZero { return w.real > 0 ? zero : infinity } return exp(w * log(z)) } @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/Sources/RealModule/Double+Real.swift b/Sources/RealModule/Double+Real.swift index 3e060f3..827c4ba 100644 --- a/Sources/RealModule/Double+Real.swift +++ b/Sources/RealModule/Double+Real.swift @@ -150,6 +150,7 @@ extension Double: Real { @_transparent public static func pow(_ x: Double, _ y: Double) -> Double { guard x >= 0 else { return .nan } + if x == 0 && y == 0 { return .nan } return libm_pow(x, y) } diff --git a/Sources/RealModule/ElementaryFunctions.swift b/Sources/RealModule/ElementaryFunctions.swift index b56ee1b..5332cc3 100644 --- a/Sources/RealModule/ElementaryFunctions.swift +++ b/Sources/RealModule/ElementaryFunctions.swift @@ -292,20 +292,24 @@ public protocol ElementaryFunctions: AdditiveArithmetic { /// exp(y * log(x)) computed with additional internal precision. /// - /// See also: - /// - - /// - `sqrt()` - /// - `root()` + /// The edge-cases of this function are defined based on the behavior of the + /// expression `exp(y log x)`, matching IEEE 754's `powr` operation. + /// In particular, this means that if `x` and `y` are both zero, `pow(x,y)` + /// is `nan` for real types and `infinity` for complex types, rather than 1. /// + /// There is also a `pow(_:Self,_:Int)` overload, whose behavior is defined + /// in terms of repeated multiplication, and hence returns 1 for this case. + /// + /// See also `sqrt()` and `root()`. static func pow(_ x: Self, _ y: Self) -> Self /// `x` raised to the nth power. /// - /// See also: - /// - - /// - `sqrt()` - /// - `root()` + /// The edge-cases of this function are defined in terms of repeated + /// multiplication or division, rather than exp(n log x). In particular, + /// `Float.pow(0, 0)` is 1. /// + /// See also `sqrt()` and `root()`. static func pow(_ x: Self, _ n: Int) -> Self /// The [square root][wiki] of `x`. diff --git a/Sources/RealModule/Float+Real.swift b/Sources/RealModule/Float+Real.swift index 4147e8b..fe7849f 100644 --- a/Sources/RealModule/Float+Real.swift +++ b/Sources/RealModule/Float+Real.swift @@ -137,6 +137,7 @@ extension Float: Real { @_transparent public static func pow(_ x: Float, _ y: Float) -> Float { guard x >= 0 else { return .nan } + if x == 0 && y == 0 { return .nan } return libm_powf(x, y) } diff --git a/Sources/RealModule/Float80+Real.swift b/Sources/RealModule/Float80+Real.swift index 9ae7e92..9a85d5d 100644 --- a/Sources/RealModule/Float80+Real.swift +++ b/Sources/RealModule/Float80+Real.swift @@ -132,6 +132,7 @@ extension Float80: Real { @_transparent public static func pow(_ x: Float80, _ y: Float80) -> Float80 { guard x >= 0 else { return .nan } + if x == 0 && y == 0 { return .nan } return libm_powl(x, y) } diff --git a/Tests/RealTests/ElementaryFunctionChecks.swift b/Tests/RealTests/ElementaryFunctionChecks.swift index 7dfd7ca..1ef1ace 100644 --- a/Tests/RealTests/ElementaryFunctionChecks.swift +++ b/Tests/RealTests/ElementaryFunctionChecks.swift @@ -128,6 +128,22 @@ internal extension Real where Self: BinaryFloatingPoint { } } +extension Real { + static func powZeroChecks() { + // pow(_:Self,_:Self) is defined by exp(y log(x)) and has edge-cases to + // match. In particular, if x is zero, log(x) is -infinity, so pow(0,0) + // is exp(0 * -infinity) = exp(nan) = nan. + XCTAssertEqual(pow(0, -1 as Self), infinity) + XCTAssert(pow(0, 0 as Self).isNaN) + XCTAssertEqual(pow(0, 1 as Self), zero) + // pow(_:Self,_:Int) is defined by repeated multiplication or division, + // and hence pow(0, 0) is 1. + XCTAssertEqual(pow(0, -1), infinity) + XCTAssertEqual(pow(0, 0), 1) + XCTAssertEqual(pow(0, 1), zero) + } +} + final class ElementaryFunctionChecks: XCTestCase { #if swift(>=5.4) && !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64)) @@ -135,6 +151,7 @@ final class ElementaryFunctionChecks: XCTestCase { if #available(macOS 11.0, iOS 14.0, watchOS 14.0, tvOS 7.0, *) { Float16.elementaryFunctionChecks() Float16.realFunctionChecks() + Float16.powZeroChecks() } } #endif @@ -142,17 +159,20 @@ final class ElementaryFunctionChecks: XCTestCase { func testFloat() { Float.elementaryFunctionChecks() Float.realFunctionChecks() + Float.powZeroChecks() } func testDouble() { Double.elementaryFunctionChecks() Double.realFunctionChecks() + Double.powZeroChecks() } #if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android) func testFloat80() { Float80.elementaryFunctionChecks() Float80.realFunctionChecks() + Float80.powZeroChecks() } #endif }