mirror of
https://github.com/apple/swift-numerics
synced 2026-11-23 04:34:25 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d7462a834 | ||
|
|
0f8ef268d0 | ||
|
|
3811e3bc7a | ||
|
|
a0501be80a | ||
|
|
cda1b5e6f4 | ||
|
|
fa16d7db6a | ||
|
|
a4372aa8d6 | ||
|
|
145156c40e | ||
|
|
c2d6cb716d | ||
|
|
4077cd3461 | ||
|
|
337be4044e | ||
|
|
ddbc41f80b | ||
|
|
2342ba6314 | ||
|
|
6af70e51ea | ||
|
|
d32bb739d4 | ||
|
|
6b89e8d4b0 | ||
|
|
ce58d083a1 | ||
|
|
555340e882 | ||
|
|
7109a22972 | ||
|
|
ea6d7ffd5e | ||
|
|
97824411d9 | ||
|
|
d368762c95 | ||
|
|
7e032e20fc | ||
|
|
1ec79f5270 | ||
|
|
44cb9f2d4a | ||
|
|
af3912cd4a | ||
|
|
4771ef24d2 | ||
|
|
336740bd6d | ||
|
|
189f072825 | ||
|
|
e6a343a205 | ||
|
|
66ac02125c | ||
|
|
4cd11553db | ||
|
|
13b74ae5f4 | ||
|
|
2828bec340 | ||
|
|
e69f12617b | ||
|
|
80522b2bee | ||
|
|
a7ef6cd74c | ||
|
|
18ce9c0bbd | ||
|
|
06c17a9a0f | ||
|
|
2327363b24 | ||
|
|
8b73836979 | ||
|
|
1dcd7f314b | ||
|
|
30627f2d06 | ||
|
|
c07076e917 | ||
|
|
968d004d54 | ||
|
|
cfeda4df2d | ||
|
|
f275f7df53 | ||
|
|
7c0eb30664 | ||
|
|
91ecc8a3b6 | ||
|
|
40b5a03063 | ||
|
|
085f6dab2e | ||
|
|
e97070e36c | ||
|
|
93b8e9f0df | ||
|
|
a5cc950a86 | ||
|
|
8978159f21 | ||
|
|
59444343c4 | ||
|
|
ee082efdaf | ||
|
|
75e4116989 | ||
|
|
9e1645a495 | ||
|
|
29d25b62c5 | ||
|
|
a35f5ba889 |
No files matched your search
+5
-3
@@ -24,10 +24,12 @@ let package = Package(
|
||||
targets: [
|
||||
.target(name: "ComplexModule", dependencies: ["RealModule"]),
|
||||
.target(name: "Numerics", dependencies: ["ComplexModule", "RealModule"]),
|
||||
.target(name: "_NumericsShims", dependencies: []),
|
||||
.target(name: "RealModule", dependencies: ["_NumericsShims"]),
|
||||
|
||||
.testTarget(name: "ComplexTests", dependencies: ["ComplexModule", "_NumericsShims"]),
|
||||
.testTarget(name: "RealTests", dependencies: ["RealModule"]),
|
||||
.target(name: "_NumericsShims", dependencies: []),
|
||||
.target(name: "_TestSupport", dependencies: ["Numerics"]),
|
||||
|
||||
.testTarget(name: "ComplexTests", dependencies: ["_TestSupport"]),
|
||||
.testTarget(name: "RealTests", dependencies: ["_TestSupport"]),
|
||||
]
|
||||
)
|
||||
@@ -11,3 +11,6 @@ add_subdirectory(_NumericsShims)
|
||||
add_subdirectory(ComplexModule)
|
||||
add_subdirectory(Numerics)
|
||||
add_subdirectory(RealModule)
|
||||
if(BUILD_TESTING)
|
||||
add_subdirectory(_TestSupport)
|
||||
endif()
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if canImport(_Differentiation)
|
||||
#if swift(>=5.3) && canImport(_Differentiation)
|
||||
import _Differentiation
|
||||
|
||||
extension Complex: Differentiable
|
||||
@@ -24,6 +24,16 @@ where RealType: Differentiable, RealType.TangentVector == RealType {
|
||||
|
||||
extension Complex
|
||||
where RealType: Differentiable, RealType.TangentVector == RealType {
|
||||
@derivative(of: init(_:_:))
|
||||
@usableFromInline
|
||||
static func _derivativeInit(
|
||||
_ real: RealType,
|
||||
_ imaginary: RealType
|
||||
) -> (value: Complex, pullback: (Complex) -> (RealType, RealType)) {
|
||||
(value: .init(real, imaginary), pullback: { v in
|
||||
(v.real, v.imaginary)
|
||||
})
|
||||
}
|
||||
|
||||
@derivative(of: real)
|
||||
@usableFromInline
|
||||
|
||||
@@ -11,6 +11,13 @@ add_library(Numerics
|
||||
Numerics.swift)
|
||||
set_target_properties(Numerics PROPERTIES
|
||||
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
|
||||
# NOTE: generate the force load symbol to ensure that the import library is
|
||||
# generated on Windows for autolinking.
|
||||
target_compile_options(Numerics PUBLIC
|
||||
-autolink-force-load
|
||||
# SR-12254: workaround for the swift compiler not properly tracking the force
|
||||
# load symbol when validating the TBD
|
||||
-Xfrontend -validate-tbd-against-ir=none)
|
||||
target_link_libraries(Numerics PUBLIC
|
||||
ComplexModule
|
||||
RealModule)
|
||||
|
||||
@@ -0,0 +1,241 @@
|
||||
//===--- ApproximateEquality.swift ----------------------------*- swift -*-===//
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2020 Apple Inc. and the Swift Numerics project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension Numeric where Magnitude: FloatingPoint {
|
||||
/// Test if `self` and `other` are approximately equal.
|
||||
///
|
||||
/// `true` if `self` and `other` are equal, or if they are finite and
|
||||
/// ```
|
||||
/// norm(self - other) <= relativeTolerance * scale
|
||||
/// ```
|
||||
/// where `scale` is
|
||||
/// ```
|
||||
/// max(norm(self), norm(other), .leastNormalMagnitude)
|
||||
/// ```
|
||||
///
|
||||
/// The default value of `relativeTolerance` is `.ulpOfOne.squareRoot()`,
|
||||
/// which corresponds to expecting "about half the digits" in the computed results to be good.
|
||||
/// This is the usual guidance in numerical analysis, if you don't know anything about the
|
||||
/// computation being performed, but is not suitable for all use cases.
|
||||
///
|
||||
/// Mathematical Properties:
|
||||
/// ------------------------
|
||||
///
|
||||
/// - `isApproximatelyEqual(to:relativeTolerance:norm:)` is _reflexive_ for
|
||||
/// non-exceptional values (such as NaN).
|
||||
///
|
||||
/// - `isApproximatelyEqual(to:relativeTolerance:norm:)` is _symmetric_.
|
||||
///
|
||||
/// - `isApproximatelyEqual(to:relativeTolerance:norm:)` is __not__ _transitive_.
|
||||
/// Because of this, approximately equality is __not an equivalence relation__,
|
||||
/// even when restricted to non-exceptional values.
|
||||
///
|
||||
/// - For any point `a`, the set of values that compare approximately equal to `a` is _convex_.
|
||||
/// (Under the assumption that the `.magnitude` property implements a valid norm.)
|
||||
///
|
||||
/// - `isApproximatelyEqual(to:relativeTolerance:norm:)` is _scale invariant_,
|
||||
/// so long as no underflow or overflow has occured, and no exceptional value is produced
|
||||
/// by the scaling.
|
||||
///
|
||||
/// See Also:
|
||||
/// -------
|
||||
/// - `isApproximatelyEqual(to:absoluteTolerance:[relativeTolerance:norm:])`
|
||||
///
|
||||
/// - Parameters:
|
||||
///
|
||||
/// - other: The value to which `self` is compared.
|
||||
///
|
||||
/// - relativeTolerance: The tolerance to use for the comparison.
|
||||
/// Defaults to `.ulpOfOne.squareRoot()`.
|
||||
///
|
||||
/// This value should be non-negative and less than or equal to 1.
|
||||
/// This constraint on is only checked in debug builds, because a mathematically
|
||||
/// well-defined result exists for any tolerance, even one out of range.
|
||||
///
|
||||
/// - norm: The [norm] to use for the comparison.
|
||||
/// Defaults to `\.magnitude`.
|
||||
///
|
||||
/// [norm]: https://en.wikipedia.org/wiki/Norm_(mathematics)
|
||||
@inlinable @inline(__always)
|
||||
public func isApproximatelyEqual(
|
||||
to other: Self,
|
||||
relativeTolerance: Magnitude = Magnitude.ulpOfOne.squareRoot(),
|
||||
norm: (Self) -> Magnitude = \.magnitude
|
||||
) -> Bool {
|
||||
return isApproximatelyEqual(
|
||||
to: other,
|
||||
absoluteTolerance: relativeTolerance * Magnitude.leastNormalMagnitude,
|
||||
relativeTolerance: relativeTolerance,
|
||||
norm: norm
|
||||
)
|
||||
}
|
||||
|
||||
/// Test if `self` and `other` are approximately equal with specified tolerances.
|
||||
///
|
||||
/// `true` if `self` and `other` are equal, or if they are finite and either
|
||||
/// ```
|
||||
/// (self - other).magnitude <= absoluteTolerance
|
||||
/// ```
|
||||
/// or
|
||||
/// ```
|
||||
/// (self - other).magnitude <= relativeTolerance * scale
|
||||
/// ```
|
||||
/// where `scale` is `max(self.magnitude, other.magnitude)`.
|
||||
///
|
||||
/// Mathematical Properties:
|
||||
/// ------------------------
|
||||
///
|
||||
/// - `isApproximatelyEqual(to:absoluteTolerance:relativeTolerance:)`
|
||||
/// is _reflexive_ for non-exceptional values (such as NaN).
|
||||
///
|
||||
/// - `isApproximatelyEqual(to:absoluteTolerance:relativeTolerance:)`
|
||||
/// is _symmetric_.
|
||||
///
|
||||
/// - `isApproximatelyEqual(to:absoluteTolerance:relativeTolerance:)`
|
||||
/// is __not__ _transitive_. Because of this, approximately equality is
|
||||
/// __not an equivalence relation__, even when restricted to non-exceptional values.
|
||||
///
|
||||
/// - For any point `a`, the set of values that compare approximately equal to `a` is _convex_.
|
||||
/// (Under the assumption that `norm` implements a valid norm, which cannot be checked
|
||||
/// by this function.)
|
||||
///
|
||||
/// See Also:
|
||||
/// -------
|
||||
/// - `isApproximatelyEqual(to:[relativeTolerance:])`
|
||||
///
|
||||
/// - Parameters:
|
||||
///
|
||||
/// - other: The value to which `self` is compared.
|
||||
///
|
||||
/// - absoluteTolerance: The absolute tolerance to use in the comparison.
|
||||
///
|
||||
/// This value should be non-negative and finite.
|
||||
/// This constraint on is only checked in debug builds, because a mathematically
|
||||
/// well-defined result exists for any tolerance, even one out of range.
|
||||
///
|
||||
/// - relativeTolerance: The relative tolerance to use in the comparison.
|
||||
/// Defaults to zero.
|
||||
///
|
||||
/// This value should be non-negative and less than or equal to 1.
|
||||
/// This constraint on is only checked in debug builds, because a mathematically
|
||||
/// well-defined result exists for any tolerance, even one out of range.
|
||||
@inlinable @inline(__always)
|
||||
public func isApproximatelyEqual(
|
||||
to other: Self,
|
||||
absoluteTolerance: Magnitude,
|
||||
relativeTolerance: Magnitude = 0
|
||||
) -> Bool {
|
||||
self.isApproximatelyEqual(
|
||||
to: other,
|
||||
absoluteTolerance: absoluteTolerance,
|
||||
relativeTolerance: relativeTolerance,
|
||||
norm: \.magnitude
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension AdditiveArithmetic {
|
||||
/// Test if `self` and `other` are approximately equal with specified tolerances and norm.
|
||||
///
|
||||
/// `true` if `self` and `other` are equal, or if they are finite and either
|
||||
/// ```
|
||||
/// norm(self - other) <= absoluteTolerance
|
||||
/// ```
|
||||
/// or
|
||||
/// ```
|
||||
/// norm(self - other) <= relativeTolerance * scale
|
||||
/// ```
|
||||
/// where `scale` is `max(norm(self), norm(other))`.
|
||||
///
|
||||
/// Mathematical Properties:
|
||||
/// ------------------------
|
||||
///
|
||||
/// - `isApproximatelyEqual(to:absoluteTolerance:relativeTolerance:norm:)`
|
||||
/// is _reflexive_ for non-exceptional values (such as NaN).
|
||||
///
|
||||
/// - `isApproximatelyEqual(to:absoluteTolerance:relativeTolerance:norm:)`
|
||||
/// is _symmetric_.
|
||||
///
|
||||
/// - `isApproximatelyEqual(to:absoluteTolerance:relativeTolerance:norm:)`
|
||||
/// is __not__ _transitive_. Because of this, approximately equality is
|
||||
/// __not an equivalence relation__, even when restricted to non-exceptional values.
|
||||
///
|
||||
/// - For any point `a`, the set of values that compare approximately equal to `a` is _convex_.
|
||||
/// (Under the assumption that `norm` implements a valid norm, which cannot be checked
|
||||
/// by this function.)
|
||||
///
|
||||
/// See Also:
|
||||
/// -------
|
||||
/// - `isApproximatelyEqual(to:[relativeTolerance:norm:])`
|
||||
/// - `isApproximatelyEqual(to:absoluteTolerance:[relativeTolerance:])`
|
||||
///
|
||||
/// - Parameters:
|
||||
///
|
||||
/// - other: The value to which `self` is compared.
|
||||
///
|
||||
/// - absoluteTolerance: The absolute tolerance to use in the comparison.
|
||||
///
|
||||
/// This value should be non-negative and finite.
|
||||
/// This constraint on is only checked in debug builds, because a mathematically
|
||||
/// well-defined result exists for any tolerance, even one out of range.
|
||||
///
|
||||
/// - relativeTolerance: The relative tolerance to use in the comparison.
|
||||
/// Defaults to zero.
|
||||
///
|
||||
/// This value should be non-negative and less than or equal to 1.
|
||||
/// This constraint on is only checked in debug builds, because a mathematically
|
||||
/// well-defined result exists for any tolerance, even one out of range.
|
||||
///
|
||||
/// - norm: The norm to use for the comparison.
|
||||
/// Defaults to `\.magnitude`.
|
||||
///
|
||||
/// For example, if we wanted to test if a complex value was inside a circle of
|
||||
/// radius 0.001 centered at (1 + 0i), we could use:
|
||||
/// ```
|
||||
/// z.isApproximatelyEqual(
|
||||
/// to: 1,
|
||||
/// absoluteTolerance: 0.001,
|
||||
/// norm: \.length
|
||||
/// )
|
||||
/// ```
|
||||
/// (if we used the default, we would be testing if `z` were inside a square region
|
||||
/// instead.)
|
||||
@inlinable
|
||||
public func isApproximatelyEqual<Magnitude>(
|
||||
to other: Self,
|
||||
absoluteTolerance: Magnitude,
|
||||
relativeTolerance: Magnitude = 0,
|
||||
norm: (Self) -> Magnitude
|
||||
) -> Bool
|
||||
// TODO: constraint should really be weaker than FloatingPoint,
|
||||
// but we need to have `isFinite` for it to work correctly with
|
||||
// floating-point magnitudes in generic contexts, which is the
|
||||
// most common case. The fix for this is to lift the isFinite
|
||||
// requirement to Numeric in the standard library, but that's
|
||||
// source-breaking, so requires an ABI rumspringa.
|
||||
where Magnitude: FloatingPoint {
|
||||
assert(
|
||||
absoluteTolerance >= 0 && absoluteTolerance.isFinite,
|
||||
"absoluteTolerance should be non-negative and finite, " +
|
||||
"but is \(absoluteTolerance)."
|
||||
)
|
||||
assert(
|
||||
relativeTolerance >= 0 && relativeTolerance <= 1,
|
||||
"relativeTolerance should be non-negative and <= 1, " +
|
||||
"but is \(relativeTolerance)."
|
||||
)
|
||||
if self == other { return true }
|
||||
let delta = norm(self - other)
|
||||
let scale = max(norm(self), norm(other))
|
||||
let bound = max(absoluteTolerance, scale*relativeTolerance)
|
||||
return delta.isFinite && delta <= bound
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,11 @@ See https://swift.org/LICENSE.txt for license information
|
||||
|
||||
add_library(RealModule
|
||||
AlgebraicField.swift
|
||||
ApproximateEquality.swift
|
||||
Double+Real.swift
|
||||
ElementaryFunctions.swift
|
||||
Float+Real.swift
|
||||
Float16+Real.swift
|
||||
Float80+Real.swift
|
||||
Real.swift
|
||||
RealFunctions.swift)
|
||||
|
||||
@@ -218,4 +218,9 @@ extension Double: Real {
|
||||
return libm_lgamma(x, &dontCare)
|
||||
}
|
||||
#endif
|
||||
|
||||
@_transparent
|
||||
public static func _mulAdd(_ a: Double, _ b: Double, _ c: Double) -> Double {
|
||||
_numerics_muladd(a, b, c)
|
||||
}
|
||||
}
|
||||
@@ -191,4 +191,9 @@ extension Float: Real {
|
||||
return libm_lgammaf(x, &dontCare)
|
||||
}
|
||||
#endif
|
||||
|
||||
@_transparent
|
||||
public static func _mulAdd(_ a: Float, _ b: Float, _ c: Float) -> Float {
|
||||
_numerics_muladdf(a, b, c)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
//===--- Float16+Real.swift -----------------------------------*- swift -*-===//
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2020 Apple Inc. and the Swift Numerics project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if swift(>=5.3)
|
||||
import _NumericsShims
|
||||
|
||||
@available(iOS 14.0, tvOS 14.0, watchOS 7.0, *)
|
||||
@available(macOS, unavailable)
|
||||
@available(macCatalyst, unavailable)
|
||||
extension Float16: Real {
|
||||
@_transparent
|
||||
public static func cos(_ x: Float16) -> Float16 {
|
||||
Float16(.cos(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func sin(_ x: Float16) -> Float16 {
|
||||
Float16(.sin(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func tan(_ x: Float16) -> Float16 {
|
||||
Float16(.tan(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func acos(_ x: Float16) -> Float16 {
|
||||
Float16(.acos(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func asin(_ x: Float16) -> Float16 {
|
||||
Float16(.asin(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func atan(_ x: Float16) -> Float16 {
|
||||
Float16(.atan(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func cosh(_ x: Float16) -> Float16 {
|
||||
Float16(.cosh(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func sinh(_ x: Float16) -> Float16 {
|
||||
Float16(.sinh(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func tanh(_ x: Float16) -> Float16 {
|
||||
Float16(.tanh(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func acosh(_ x: Float16) -> Float16 {
|
||||
Float16(.acosh(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func asinh(_ x: Float16) -> Float16 {
|
||||
Float16(.asinh(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func atanh(_ x: Float16) -> Float16 {
|
||||
Float16(.atanh(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func exp(_ x: Float16) -> Float16 {
|
||||
Float16(.exp(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func expMinusOne(_ x: Float16) -> Float16 {
|
||||
Float16(.expMinusOne(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func log(_ x: Float16) -> Float16 {
|
||||
Float16(.log(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func log(onePlus x: Float16) -> Float16 {
|
||||
Float16(.log(onePlus: Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func erf(_ x: Float16) -> Float16 {
|
||||
Float16(.erf(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func erfc(_ x: Float16) -> Float16 {
|
||||
Float16(.erfc(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func exp2(_ x: Float16) -> Float16 {
|
||||
Float16(.exp2(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func exp10(_ x: Float16) -> Float16 {
|
||||
Float16(.exp10(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func hypot(_ x: Float16, _ y: Float16) -> Float16 {
|
||||
if x.isInfinite || y.isInfinite { return .infinity }
|
||||
let xf = Float(x)
|
||||
let yf = Float(y)
|
||||
return Float16(.sqrt(xf*xf + yf*yf))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func gamma(_ x: Float16) -> Float16 {
|
||||
Float16(.gamma(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func log2(_ x: Float16) -> Float16 {
|
||||
Float16(.log2(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func log10(_ x: Float16) -> Float16 {
|
||||
Float16(.log10(Float(x)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func pow(_ x: Float16, _ y: Float16) -> Float16 {
|
||||
Float16(.pow(Float(x), Float(y)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func pow(_ x: Float16, _ n: Int) -> Float16 {
|
||||
// Float16 is simpler than Float or Double, because the range of
|
||||
// "interesting" exponents is pretty small; anything outside of
|
||||
// -22707 ... 34061 simply overflows or underflows for every
|
||||
// x that isn't zero or one. This whole range is representable
|
||||
// as Float, so we can just use powf as long as we're a little
|
||||
// bit (get it?) careful to preserve parity.
|
||||
let clamped = min(max(n, -0x10000), 0x10000) | (n & 1)
|
||||
return Float16(libm_powf(Float(x), Float(clamped)))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func root(_ x: Float16, _ n: Int) -> Float16 {
|
||||
Float16(.root(Float(x), n))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func atan2(y: Float16, x: Float16) -> Float16 {
|
||||
Float16(.atan2(y: Float(y), x: Float(x)))
|
||||
}
|
||||
|
||||
#if !os(Windows)
|
||||
@_transparent
|
||||
public static func logGamma(_ x: Float16) -> Float16 {
|
||||
Float16(.logGamma(Float(x)))
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
@@ -40,6 +40,17 @@ extension Real {
|
||||
return pow(10, x)
|
||||
}
|
||||
|
||||
/// cos(x) - 1, computed in such a way as to maintain accuracy for small x.
|
||||
///
|
||||
/// See also:
|
||||
/// -
|
||||
/// - `ElementaryFunctions.expMinusOne()`
|
||||
@_transparent
|
||||
public static func cosMinusOne(_ x: Self) -> Self {
|
||||
let sinxOver2 = sin(x/2)
|
||||
return -2*sinxOver2*sinxOver2
|
||||
}
|
||||
|
||||
#if !os(Windows)
|
||||
public static func signGamma(_ x: Self) -> FloatingPointSign {
|
||||
// Gamma is strictly positive for x >= 0.
|
||||
@@ -76,6 +87,11 @@ extension Real {
|
||||
}
|
||||
#endif
|
||||
|
||||
@_transparent
|
||||
public static func _mulAdd(_ a: Self, _ b: Self, _ c: Self) -> Self {
|
||||
a*b + c
|
||||
}
|
||||
|
||||
@_transparent
|
||||
public static func sqrt(_ x: Self) -> Self {
|
||||
return x.squareRoot()
|
||||
|
||||
@@ -112,4 +112,9 @@ public protocol RealFunctions: ElementaryFunctions {
|
||||
/// - `logGamma()`
|
||||
static func signGamma(_ x: Self) -> FloatingPointSign
|
||||
#endif
|
||||
|
||||
/// a*b + c, computed _either_ with an FMA or with separate multiply and add.
|
||||
///
|
||||
/// Whichever is faster should be chosen by the compiler statically.
|
||||
static func _mulAdd(_ a: Self, _ b: Self, _ c: Self) -> Self
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors
|
||||
// Copyright (c) 2019-2020 Apple Inc. and the Swift Numerics project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
@@ -382,12 +382,26 @@ HEADER_SHIM long double libm_lgammal(long double x, int *signp) {
|
||||
}
|
||||
#endif
|
||||
|
||||
// MARK: - shims to import C complex operations for timing purposes
|
||||
#if !defined _WIN32
|
||||
// Clang doesn't have support for complex arithmetic on Windows, so
|
||||
// it doesn't make sense to benchmark against it (and these will
|
||||
// fail to link if used there).
|
||||
// MARK: - fast mul-add inlines
|
||||
/// a*b + c evaluated _either_ as two operations or fma, whichever is faster.
|
||||
HEADER_SHIM float _numerics_muladdf(float a, float b, float c) {
|
||||
#pragma STDC FP_CONTRACT ON
|
||||
return a*b + c;
|
||||
}
|
||||
|
||||
/// a*b + c evaluated _either_ as two operations or fma, whichever is faster.
|
||||
HEADER_SHIM double _numerics_muladd(double a, double b, double c) {
|
||||
#pragma STDC FP_CONTRACT ON
|
||||
return a*b + c;
|
||||
}
|
||||
|
||||
// No long-double muladd operation, because no one has built an FMA for it
|
||||
// (except for Itanium, which Swift doesn't support).
|
||||
|
||||
// MARK: - shims to import C complex operations for timing purposes
|
||||
// Clang doesn't provide complex arithmetic on Windows (because MSVC
|
||||
// doesn't), so we can't define these there, or we'll get link errors.
|
||||
#if !defined _WIN32
|
||||
typedef struct { double real; double imag; } CComplex;
|
||||
|
||||
HEADER_SHIM CComplex libm_cdiv(CComplex z, CComplex w) {
|
||||
@@ -403,5 +417,4 @@ HEADER_SHIM CComplex libm_cmul(CComplex z, CComplex w) {
|
||||
double _Complex c = a*b;
|
||||
return (CComplex){ __real__ c, __imag__ c };
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif // !defined _WIN32
|
||||
@@ -0,0 +1,19 @@
|
||||
#[[
|
||||
This source file is part of the Swift Numerics open source project
|
||||
|
||||
Copyright (c) 2019-2020 Apple Inc. and the Swift Numerics project authors
|
||||
Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
|
||||
See https://swift.org/LICENSE.txt for license information
|
||||
#]]
|
||||
|
||||
add_library(_TestSupport
|
||||
RealTestSupport.swift)
|
||||
set_target_properties(_TestSupport PROPERTIES
|
||||
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
|
||||
target_link_libraries(_TestSupport PUBLIC
|
||||
Numerics)
|
||||
|
||||
|
||||
_install_target(_TestSupport)
|
||||
set_property(GLOBAL APPEND PROPERTY SWIFT_NUMERICS_EXPORTS _TestSupport)
|
||||
@@ -13,13 +13,13 @@ import XCTest
|
||||
import RealModule
|
||||
|
||||
#if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android)
|
||||
typealias TestLiteralType = Float80
|
||||
public typealias TestLiteralType = Float80
|
||||
#else
|
||||
typealias TestLiteralType = Double
|
||||
public typealias TestLiteralType = Double
|
||||
#endif
|
||||
|
||||
@discardableResult
|
||||
func assertClose<T>(
|
||||
public func assertClose<T>(
|
||||
_ expected: TestLiteralType,
|
||||
_ observed: T,
|
||||
allowedError: T = 16,
|
||||
@@ -69,7 +69,7 @@ func assertClose<T>(
|
||||
return ulps
|
||||
}
|
||||
|
||||
func assertClose<T>(
|
||||
public func assertClose<T>(
|
||||
_ expected: TestLiteralType,
|
||||
_ observed: T,
|
||||
allowedError: T = 16,
|
||||
@@ -81,3 +81,26 @@ func assertClose<T>(
|
||||
expected, observed, allowedError: allowedError, file: file, line: line
|
||||
))
|
||||
}
|
||||
|
||||
public protocol FixedWidthFloatingPoint: BinaryFloatingPoint
|
||||
where Exponent: FixedWidthInteger,
|
||||
RawSignificand: FixedWidthInteger { }
|
||||
|
||||
#if swift(>=5.3)
|
||||
@available(iOS 14.0, watchOS 14.0, tvOS 7.0, *)
|
||||
@available(macOS, unavailable)
|
||||
@available(macCatalyst, unavailable)
|
||||
extension Float16: FixedWidthFloatingPoint { }
|
||||
#endif
|
||||
|
||||
extension Float: FixedWidthFloatingPoint { }
|
||||
extension Double: FixedWidthFloatingPoint { }
|
||||
#if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android)
|
||||
extension Float80: FixedWidthFloatingPoint { }
|
||||
#endif
|
||||
|
||||
extension FloatingPointSign {
|
||||
static func random<G: RandomNumberGenerator>(using g: inout G) -> FloatingPointSign {
|
||||
[.plus,.minus].randomElement(using: &g)!
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
//===--- ApproximateEqualityTests.swift -----------------------*- swift -*-===//
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2020 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Numerics
|
||||
import XCTest
|
||||
|
||||
final class ApproximateEqualityTests: XCTestCase {
|
||||
|
||||
func testSpecials<T: Real>(absolute tol: T) {
|
||||
let zero = Complex<T>.zero
|
||||
let inf = Complex<T>.infinity
|
||||
XCTAssertTrue(zero.isApproximatelyEqual(to: zero, absoluteTolerance: tol))
|
||||
XCTAssertTrue(zero.isApproximatelyEqual(to:-zero, absoluteTolerance: tol))
|
||||
XCTAssertTrue((-zero).isApproximatelyEqual(to: zero, absoluteTolerance: tol))
|
||||
XCTAssertTrue((-zero).isApproximatelyEqual(to:-zero, absoluteTolerance: tol))
|
||||
// Complex has a single point at infinity.
|
||||
XCTAssertTrue(inf.isApproximatelyEqual(to: inf, absoluteTolerance: tol))
|
||||
XCTAssertTrue(inf.isApproximatelyEqual(to:-inf, absoluteTolerance: tol))
|
||||
XCTAssertTrue((-inf).isApproximatelyEqual(to: inf, absoluteTolerance: tol))
|
||||
XCTAssertTrue((-inf).isApproximatelyEqual(to:-inf, absoluteTolerance: tol))
|
||||
}
|
||||
|
||||
func testSpecials<T: Real>(relative tol: T) {
|
||||
let zero = Complex<T>.zero
|
||||
let inf = Complex<T>.infinity
|
||||
XCTAssertTrue(zero.isApproximatelyEqual(to: zero, relativeTolerance: tol))
|
||||
XCTAssertTrue(zero.isApproximatelyEqual(to:-zero, relativeTolerance: tol))
|
||||
XCTAssertTrue((-zero).isApproximatelyEqual(to: zero, relativeTolerance: tol))
|
||||
XCTAssertTrue((-zero).isApproximatelyEqual(to:-zero, relativeTolerance: tol))
|
||||
// Complex has a single point at infinity.
|
||||
XCTAssertTrue(inf.isApproximatelyEqual(to: inf, relativeTolerance: tol))
|
||||
XCTAssertTrue(inf.isApproximatelyEqual(to:-inf, relativeTolerance: tol))
|
||||
XCTAssertTrue((-inf).isApproximatelyEqual(to: inf, relativeTolerance: tol))
|
||||
XCTAssertTrue((-inf).isApproximatelyEqual(to:-inf, relativeTolerance: tol))
|
||||
}
|
||||
|
||||
func testSpecials<T: Real>(_ type: T.Type) {
|
||||
XCTAssertTrue(Complex<T>.zero.isApproximatelyEqual(to: .zero))
|
||||
XCTAssertTrue(Complex<T>.zero.isApproximatelyEqual(to:-.zero))
|
||||
testSpecials(absolute: T.zero)
|
||||
testSpecials(absolute: T.greatestFiniteMagnitude)
|
||||
testSpecials(relative: T.ulpOfOne)
|
||||
testSpecials(relative: T(1))
|
||||
}
|
||||
|
||||
func testFloat() {
|
||||
testSpecials(Float.self)
|
||||
}
|
||||
|
||||
func testDouble() {
|
||||
testSpecials(Double.self)
|
||||
}
|
||||
|
||||
#if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android)
|
||||
func testFloat80() {
|
||||
testSpecials(Float80.self)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -165,12 +165,10 @@ final class ArithmeticTests: XCTestCase {
|
||||
let a: Complex<Double>
|
||||
let b: Complex<Double>
|
||||
let c: Complex<Double>
|
||||
let allowSmallError: Bool
|
||||
init(_ a: Complex<Double>, _ b: Complex<Double>, _ c: Complex<Double>, allowSmallError: Bool = false) {
|
||||
init(_ a: Complex<Double>, _ b: Complex<Double>, _ c: Complex<Double>) {
|
||||
self.a = a
|
||||
self.b = b
|
||||
self.c = c
|
||||
self.allowSmallError = allowSmallError
|
||||
}
|
||||
}
|
||||
// The ten test cases from Baudin & Smith's paper. These only apply to
|
||||
|
||||
@@ -8,6 +8,7 @@ See https://swift.org/LICENSE.txt for license information
|
||||
#]]
|
||||
|
||||
add_library(ComplexTests
|
||||
ApproximateEqualityTests.swift
|
||||
ArithmeticTests.swift
|
||||
DifferentiableTests.swift
|
||||
PropertyTests.swift)
|
||||
@@ -18,4 +19,6 @@ target_compile_options(ComplexTests PRIVATE
|
||||
target_link_libraries(ComplexTests PUBLIC
|
||||
$<$<NOT:$<PLATFORM_ID:Darwin>>:Foundation>
|
||||
ComplexModule
|
||||
Numerics
|
||||
_TestSupport
|
||||
XCTest)
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if canImport(_Differentiation)
|
||||
#if swift(>=5.3) && canImport(_Differentiation)
|
||||
|
||||
import XCTest
|
||||
import ComplexModule
|
||||
@@ -28,6 +28,18 @@ final class DifferentiableTests: XCTestCase {
|
||||
Complex(5, 2))
|
||||
}
|
||||
|
||||
func testInitializer() {
|
||||
let φ1 = pullback(at: 4, -3) { r, i in Complex<Float>(r, i) }
|
||||
let tan1 = φ1(Complex(-1, 2))
|
||||
XCTAssertEqual(tan1.0, -1)
|
||||
XCTAssertEqual(tan1.1, 2)
|
||||
|
||||
let φ2 = pullback(at: 4, -3) { r, i in Complex<Float>(r * r, i + i) }
|
||||
let tan2 = φ2(Complex(-1, 1))
|
||||
XCTAssertEqual(tan2.0, -8)
|
||||
XCTAssertEqual(tan2.1, 2)
|
||||
}
|
||||
|
||||
func testConjugate() {
|
||||
let φ = pullback(at: Complex<Float>(20, -4)) { x in x.conjugate }
|
||||
XCTAssertEqual(φ(Complex(1, 0)), Complex(1, 0))
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
//===--- ApproximateEqualityTests.swift -----------------------*- swift -*-===//
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2020 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import RealModule
|
||||
import XCTest
|
||||
import _TestSupport
|
||||
|
||||
final class ApproximateEqualityTests: XCTestCase {
|
||||
|
||||
func testSpecials<T: Real>(absolute tol: T) {
|
||||
let zero = T.zero
|
||||
let gfm = T.greatestFiniteMagnitude
|
||||
let inf = T.infinity
|
||||
let nan = T.nan
|
||||
XCTAssertTrue(zero.isApproximatelyEqual(to: zero, absoluteTolerance: tol))
|
||||
XCTAssertTrue(zero.isApproximatelyEqual(to:-zero, absoluteTolerance: tol))
|
||||
XCTAssertFalse(inf.isApproximatelyEqual(to: gfm, absoluteTolerance: tol))
|
||||
XCTAssertFalse(gfm.isApproximatelyEqual(to: inf, absoluteTolerance: tol))
|
||||
XCTAssertTrue(inf.isApproximatelyEqual(to: inf, absoluteTolerance: tol))
|
||||
XCTAssertTrue(inf.isApproximatelyEqual(to: inf, absoluteTolerance: tol))
|
||||
XCTAssertFalse(nan.isApproximatelyEqual(to: nan, absoluteTolerance: tol))
|
||||
}
|
||||
|
||||
func testSpecials<T: Real>(relative tol: T) {
|
||||
let zero = T.zero
|
||||
let gfm = T.greatestFiniteMagnitude
|
||||
let inf = T.infinity
|
||||
let nan = T.nan
|
||||
XCTAssertTrue(zero.isApproximatelyEqual(to: zero, relativeTolerance: tol))
|
||||
XCTAssertTrue(zero.isApproximatelyEqual(to:-zero, relativeTolerance: tol))
|
||||
XCTAssertFalse(inf.isApproximatelyEqual(to: gfm, relativeTolerance: tol))
|
||||
XCTAssertFalse(gfm.isApproximatelyEqual(to: inf, relativeTolerance: tol))
|
||||
XCTAssertTrue(inf.isApproximatelyEqual(to: inf, relativeTolerance: tol))
|
||||
XCTAssertTrue(inf.isApproximatelyEqual(to: inf, relativeTolerance: tol))
|
||||
XCTAssertFalse(nan.isApproximatelyEqual(to: nan, relativeTolerance: tol))
|
||||
}
|
||||
|
||||
func testSpecials<T: Real>(_ type: T.Type) {
|
||||
XCTAssertTrue(T.zero.isApproximatelyEqual(to: .zero))
|
||||
XCTAssertTrue(T.zero.isApproximatelyEqual(to:-.zero))
|
||||
testSpecials(absolute: T.zero)
|
||||
testSpecials(absolute: T.leastNormalMagnitude)
|
||||
testSpecials(absolute: T.greatestFiniteMagnitude)
|
||||
testSpecials(relative: T.zero)
|
||||
testSpecials(relative: T.ulpOfOne)
|
||||
testSpecials(relative: T(1).nextDown)
|
||||
testSpecials(relative: T(1))
|
||||
}
|
||||
|
||||
func testDefaults<T: Real>(_ type: T.Type) {
|
||||
let e = T.ulpOfOne.squareRoot()
|
||||
XCTAssertTrue(T(1).isApproximatelyEqual(to: 1 + e))
|
||||
XCTAssertTrue(T(1).isApproximatelyEqual(to: 1 - e/2))
|
||||
XCTAssertFalse(T(1).isApproximatelyEqual(to: 1 + 2*e))
|
||||
XCTAssertFalse(T(1).isApproximatelyEqual(to: 1 - 3*e/2))
|
||||
}
|
||||
|
||||
func testRandom<T>(_ type: T.Type) where T: FixedWidthFloatingPoint & Real {
|
||||
var g = SystemRandomNumberGenerator()
|
||||
// Generate a bunch of random values in a small interval and a tolerance
|
||||
// and use them to check that various properties that we would like to
|
||||
// hold actually do.
|
||||
var x = [1] + (0 ..< 64).map {
|
||||
_ in T.random(in: 1 ..< 2, using: &g)
|
||||
} + [2]
|
||||
x.sort()
|
||||
// We have 66 values in 1 ... 2, so if we use a tolerance of around 1/64,
|
||||
// at least some of the pairs will compare equal with tolerance.
|
||||
let tol = T.random(in: 1/64 ... 1/32, using: &g)
|
||||
// We're going to walk the values in order, validating that some common-
|
||||
// sense properties hold.
|
||||
for i in x.indices {
|
||||
// reflexivity
|
||||
XCTAssertTrue(x[i].isApproximatelyEqual(to: x[i]))
|
||||
XCTAssertTrue(x[i].isApproximatelyEqual(to: x[i], relativeTolerance: tol))
|
||||
XCTAssertTrue(x[i].isApproximatelyEqual(to: x[i], absoluteTolerance: tol))
|
||||
for j in i ..< x.endIndex {
|
||||
// commutativity
|
||||
XCTAssertTrue(
|
||||
x[i].isApproximatelyEqual(to: x[j], relativeTolerance: tol) ==
|
||||
x[j].isApproximatelyEqual(to: x[i], relativeTolerance: tol)
|
||||
)
|
||||
XCTAssertTrue(
|
||||
x[i].isApproximatelyEqual(to: x[j], absoluteTolerance: tol) ==
|
||||
x[j].isApproximatelyEqual(to: x[i], absoluteTolerance: tol)
|
||||
)
|
||||
// scale invariance for relative comparisons
|
||||
let scale = T(
|
||||
sign:.plus,
|
||||
exponent: T.Exponent.random(in: T.leastNormalMagnitude.exponent ..< T.greatestFiniteMagnitude.exponent),
|
||||
significand: 1
|
||||
)
|
||||
XCTAssertTrue(
|
||||
x[i].isApproximatelyEqual(to: x[j], relativeTolerance: tol) ==
|
||||
(scale*x[i]).isApproximatelyEqual(to: scale*x[j], relativeTolerance: tol)
|
||||
)
|
||||
}
|
||||
// if a ≤ b ≤ c, and a ≈ c, then a ≈ b and b ≈ c (relative tolerance)
|
||||
var left = x.firstIndex { x[i].isApproximatelyEqual(to: $0, relativeTolerance: tol) }
|
||||
var right = x.lastIndex { x[i].isApproximatelyEqual(to: $0, relativeTolerance: tol) }
|
||||
if let l = left, let r = right {
|
||||
for j in l ..< r {
|
||||
XCTAssertTrue(x[i].isApproximatelyEqual(to: x[j], relativeTolerance: tol))
|
||||
}
|
||||
}
|
||||
// if a ≤ b ≤ c, and a ≈ c, then a ≈ b and b ≈ c (absolute tolerance)
|
||||
left = x.firstIndex { x[i].isApproximatelyEqual(to: $0, absoluteTolerance: tol) }
|
||||
right = x.lastIndex { x[i].isApproximatelyEqual(to: $0, absoluteTolerance: tol) }
|
||||
if let l = left, let r = right {
|
||||
for j in l ..< r {
|
||||
XCTAssertTrue(x[i].isApproximatelyEqual(to: x[j], absoluteTolerance: tol))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testFloat() {
|
||||
testSpecials(Float.self)
|
||||
testDefaults(Float.self)
|
||||
testRandom(Float.self)
|
||||
}
|
||||
|
||||
func testDouble() {
|
||||
testSpecials(Double.self)
|
||||
testDefaults(Double.self)
|
||||
testRandom(Double.self)
|
||||
}
|
||||
|
||||
#if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android)
|
||||
func testFloat80() {
|
||||
testSpecials(Float80.self)
|
||||
testDefaults(Float80.self)
|
||||
testRandom(Float80.self)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -8,12 +8,13 @@ See https://swift.org/LICENSE.txt for license information
|
||||
#]]
|
||||
|
||||
add_library(RealTests
|
||||
IntegerExponentTests.swift
|
||||
RealTests.swift
|
||||
RealTestSupport.swift)
|
||||
ApproximateEqualityTests.swift
|
||||
ElementaryFunctionChecks.swift
|
||||
IntegerExponentTests.swift)
|
||||
target_compile_options(RealTests PRIVATE
|
||||
-enable-testing)
|
||||
target_link_libraries(RealTests PUBLIC
|
||||
$<$<NOT:$<PLATFORM_ID:Darwin>>:Foundation>
|
||||
RealModule
|
||||
_TestSupport
|
||||
XCTest)
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
import XCTest
|
||||
import RealModule
|
||||
import _TestSupport
|
||||
|
||||
internal extension ElementaryFunctions where Self: BinaryFloatingPoint {
|
||||
static func elementaryFunctionChecks() {
|
||||
@@ -59,6 +60,15 @@ internal extension Real where Self: BinaryFloatingPoint {
|
||||
|
||||
final class ElementaryFunctionChecks: XCTestCase {
|
||||
|
||||
#if swift(>=5.3) && !os(macOS)
|
||||
func testFloat16() {
|
||||
if #available(iOS 14.0, watchOS 14.0, tvOS 7.0, *) {
|
||||
Float16.elementaryFunctionChecks()
|
||||
Float16.realFunctionChecks()
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
func testFloat() {
|
||||
Float.elementaryFunctionChecks()
|
||||
Float.realFunctionChecks()
|
||||
@@ -11,10 +11,13 @@
|
||||
|
||||
import XCTest
|
||||
import RealModule
|
||||
import _TestSupport
|
||||
|
||||
internal extension Real where Self: BinaryFloatingPoint,
|
||||
Self.RawSignificand: FixedWidthInteger {
|
||||
internal extension Real where Self: FixedWidthFloatingPoint {
|
||||
|
||||
static func testIntegerExponentCommon() {
|
||||
// TODO: replace with seedable generator, print seed.
|
||||
var g = SystemRandomNumberGenerator()
|
||||
// If x is -1, then the result is ±1 with sign chosen by parity of n.
|
||||
// Simply converting n to Real will flip parity when n is large, so
|
||||
// first check that we get those cases right.
|
||||
@@ -32,8 +35,9 @@ internal extension Real where Self: BinaryFloatingPoint,
|
||||
// underflow; we want to be sure that we get the right ±0 or ±∞
|
||||
// result.
|
||||
for _ in 0 ..< 10 {
|
||||
let x = Self.random(in: 2 ..< 4)
|
||||
let n = Int.random(in: 1 - Int(Self.leastNonzeroMagnitude.exponent) ..< .max)
|
||||
let x = Self.random(in: 2 ..< 4, using: &g)
|
||||
let nLowerBound = 1 - Int(Self.leastNonzeroMagnitude.exponent)
|
||||
let n = Int.random(in: nLowerBound ..< .max, using: &g)
|
||||
let even = n & -2
|
||||
let odd = even | 1
|
||||
assertClose( .infinity, Self.pow(x, even))
|
||||
@@ -73,6 +77,36 @@ internal extension Real where Self: BinaryFloatingPoint,
|
||||
}
|
||||
}
|
||||
|
||||
#if swift(>=5.3)
|
||||
@available(iOS 14.0, watchOS 14.0, tvOS 7.0, *)
|
||||
@available(macOS, unavailable)
|
||||
extension Float16 {
|
||||
static func testIntegerExponent() {
|
||||
testIntegerExponentCommon()
|
||||
testIntegerExponentDoubleAndSmaller()
|
||||
let u = Float16(1).nextUp
|
||||
let d = Float16(1).nextDown
|
||||
// Smallest exponents not exactly representable as Float16.
|
||||
assertClose(-7.3890572722436554354625993393835304, Float16.pow(-u, 0x801))
|
||||
assertClose(-0.3676100238077049750885141244927184, Float16.pow(-d, 0x801))
|
||||
// Exponents close to overflow boundary.
|
||||
assertClose( 65403.86633107, Float16.pow(-u, 11360))
|
||||
assertClose(-65467.73729429, Float16.pow(-u, 11361))
|
||||
assertClose( 65531.67063149, Float16.pow(-u, 11362))
|
||||
assertClose( 65487.96799785, Float16.pow(-d, -22706))
|
||||
assertClose(-65519.96016590, Float16.pow(-d, -22707))
|
||||
assertClose( 65551.96796276, Float16.pow(-d, -22708))
|
||||
// Exponents close to underflow boundary.
|
||||
assertClose( 5.966876499900e-8, Float16.pow(-u, -17042))
|
||||
assertClose(-5.961055156973e-8, Float16.pow(-u, -17043))
|
||||
assertClose( 5.955239493405e-8, Float16.pow(-u, -17044))
|
||||
assertClose( 5.964109628044e-8, Float16.pow(-d, 34060))
|
||||
assertClose(-5.961197465140e-8, Float16.pow(-d, 34061))
|
||||
assertClose( 5.958286724190e-8, Float16.pow(-d, 34062))
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
extension Float {
|
||||
static func testIntegerExponent() {
|
||||
testIntegerExponentCommon()
|
||||
@@ -113,12 +147,21 @@ extension Double {
|
||||
assertClose(-0.3678794411714422603312898889458068, Double.pow(-d, 0x20000000000001))
|
||||
assertClose(-2.7182818284590456880451484776630468, Double.pow(-d, -0x20000000000001))
|
||||
// Exponents close to overflow boundary.
|
||||
assertClose( 1.7976931348623151738531864721534215e308, Double.pow(-u, 3196577161300664268))
|
||||
assertClose(-1.7976931348623155730212483790972209e308, Double.pow(-u, 3196577161300664269))
|
||||
assertClose( 1.7976931348623159721893102860411089e308, Double.pow(-u, 3196577161300664270))
|
||||
assertClose( 1.7976931348623157075547244136070910e308, Double.pow(-d, -6393154322601327474))
|
||||
assertClose(-1.7976931348623159071387553670790721e308, Double.pow(-d, -6393154322601327475))
|
||||
assertClose( 1.7976931348623161067227863205510754e308, Double.pow(-d, -6393154322601327476))
|
||||
#if os(Windows)
|
||||
// TODO: It appears that the Windows pow doesn't carry enough precision
|
||||
// through the computation of log(1.nextDown) to produce a good result
|
||||
// for these cases; we'll want to provide a better implementation at some
|
||||
// point in the future. It's acceptable in the short term, however.
|
||||
let tol: Double = 360
|
||||
#else
|
||||
let tol: Double = 16
|
||||
#endif
|
||||
assertClose( 1.7976931348623151738531864721534215e308, Double.pow(-u, 3196577161300664268), allowedError: tol)
|
||||
assertClose(-1.7976931348623155730212483790972209e308, Double.pow(-u, 3196577161300664269), allowedError: tol)
|
||||
assertClose( 1.7976931348623159721893102860411089e308, Double.pow(-u, 3196577161300664270), allowedError: tol)
|
||||
assertClose( 1.7976931348623157075547244136070910e308, Double.pow(-d, -6393154322601327474), allowedError: tol)
|
||||
assertClose(-1.7976931348623159071387553670790721e308, Double.pow(-d, -6393154322601327475), allowedError: tol)
|
||||
assertClose( 1.7976931348623161067227863205510754e308, Double.pow(-d, -6393154322601327476), allowedError: tol)
|
||||
// Exponents close to underflow boundary.
|
||||
assertClose( 2.4703282292062334560337346683707907e-324, Double.pow(-u, -3355781687888880946))
|
||||
assertClose(-2.4703282292062329075106789791206172e-324, Double.pow(-u, -3355781687888880947))
|
||||
@@ -131,6 +174,13 @@ extension Double {
|
||||
}
|
||||
|
||||
final class IntegerExponentTests: XCTestCase {
|
||||
|
||||
#if swift(>=5.3) && !os(macOS)
|
||||
func testFloat16() {
|
||||
Float16.testIntegerExponent()
|
||||
}
|
||||
#endif
|
||||
|
||||
func testFloat() {
|
||||
Float.testIntegerExponent()
|
||||
}
|
||||
|
||||
+46
-11
@@ -18,19 +18,53 @@ import RealTests
|
||||
@testable
|
||||
import ComplexTests
|
||||
|
||||
extension ComplexTests.ApproximateEqualityTests {
|
||||
static var all = testCase([
|
||||
("testFloat", ComplexTests.ApproximateEqualityTests.testFloat),
|
||||
("testDouble", ComplexTests.ApproximateEqualityTests.testDouble),
|
||||
])
|
||||
}
|
||||
|
||||
extension RealTests.ApproximateEqualityTests {
|
||||
static var all = testCase([
|
||||
("testFloat", RealTests.ApproximateEqualityTests.testFloat),
|
||||
("testDouble", RealTests.ApproximateEqualityTests.testDouble),
|
||||
])
|
||||
}
|
||||
|
||||
#if swift(>=5.3)
|
||||
extension ElementaryFunctionChecks {
|
||||
static var all = testCase([
|
||||
("testFloat16", ElementaryFunctionChecks.testFloat16),
|
||||
("testFloat", ElementaryFunctionChecks.testFloat),
|
||||
("testDouble", ElementaryFunctionChecks.testDouble),
|
||||
])
|
||||
}
|
||||
#else
|
||||
extension ElementaryFunctionChecks {
|
||||
static var all = testCase([
|
||||
("testFloat", ElementaryFunctionChecks.testFloat),
|
||||
("testDouble", ElementaryFunctionChecks.testDouble),
|
||||
])
|
||||
}
|
||||
#endif
|
||||
|
||||
#if swift(>=5.3)
|
||||
extension IntegerExponentTests {
|
||||
static var all = testCase([
|
||||
("testFloat16", IntegerExponentTests.testFloat16),
|
||||
("testFloat", IntegerExponentTests.testFloat),
|
||||
("testDouble", IntegerExponentTests.testDouble),
|
||||
])
|
||||
}
|
||||
#else
|
||||
extension IntegerExponentTests {
|
||||
static var all = testCase([
|
||||
("testFloat", IntegerExponentTests.testFloat),
|
||||
("testDouble", IntegerExponentTests.testDouble),
|
||||
])
|
||||
}
|
||||
#endif
|
||||
|
||||
extension ArithmeticTests {
|
||||
static var all = testCase([
|
||||
@@ -40,16 +74,6 @@ extension ArithmeticTests {
|
||||
])
|
||||
}
|
||||
|
||||
#if canImport(_Differentiation)
|
||||
extension DifferentiableTests {
|
||||
static var all = testCase([
|
||||
("testComponentGetter", DifferentiableTests.testComponentGetter),
|
||||
("testConjugate", DifferentiableTests.testConjugate),
|
||||
("testArithmetics", DifferentiableTests.testArithmetics),
|
||||
])
|
||||
}
|
||||
#endif
|
||||
|
||||
extension PropertyTests {
|
||||
static var all = testCase([
|
||||
("testProperties", PropertyTests.testProperties),
|
||||
@@ -59,13 +83,24 @@ extension PropertyTests {
|
||||
}
|
||||
|
||||
var testCases = [
|
||||
ComplexTests.ApproximateEqualityTests.all,
|
||||
RealTests.ApproximateEqualityTests.all,
|
||||
ElementaryFunctionChecks.all,
|
||||
IntegerExponentTests.all,
|
||||
ArithmeticTests.all,
|
||||
PropertyTests.all,
|
||||
]
|
||||
|
||||
#if canImport(_Differentiation)
|
||||
#if swift(>=5.3) && canImport(_Differentiation)
|
||||
extension DifferentiableTests {
|
||||
static var all = testCase([
|
||||
("testComponentGetter", DifferentiableTests.testComponentGetter),
|
||||
("testInitializer", DifferentiableTests.testInitializer),
|
||||
("testConjugate", DifferentiableTests.testConjugate),
|
||||
("testArithmetics", DifferentiableTests.testArithmetics),
|
||||
])
|
||||
}
|
||||
|
||||
testCases += [
|
||||
DifferentiableTests.all
|
||||
]
|
||||
|
||||
Reference in new issue
Block a user