14 Commits
Author SHA1 Message Date
Stephen Canon 7ff707f6f8 Merge pull request #83 from stephentyrone/pown
Initial pass at pow(Self, Int) for Float and Double
2019-11-29 23:13:19 -05:00
Stephen Canon de5399615c Additional test coverage of saturating values.
Also skip out of tests that won't compile and wouldn't make sense anyway on 32b systems.
2019-11-29 23:11:15 -05:00
Stephen Canon f7842a76db Capitalization search/replace-o 2019-11-29 23:00:45 -05:00
Stephen Canon 5bf2f828ef Initial pass at implementing pow(Self,Int) with desired semantics.
We don't need to do anything special for Float80, but Float and Double require some handholding because Int values are not always representable.
2019-11-29 22:56:14 -05:00
Stephen Canon 65aac0be46 Add test cases to stress pow(_:Float/Double,:Int)
These are the hardest cases to get right; most of the fail at present.

Also some improvements to sanityCheck to handle negative values correctly and treat infinity as the next binade up (which isn't ideal for general use, but is good for this sort of rough testing.)
2019-11-29 22:54:03 -05:00
Stephen Canon 55f33b7769 Merge pull request #81 from stephentyrone/lgamma-shims-linux
Add inline decls of lgamma_r and friends on Linux, too.
2019-11-22 09:55:28 -05:00
Stephen Canon 5e978d85a2 Add inline decls of lgamma_r and friends on Linux, too. 2019-11-22 09:47:36 -05:00
Stephen Canon 2b63fc844e Merge pull request #78 from stephentyrone/lengthSquared
Rename unsafeLengthSquared -> lengthSquared
2019-11-20 16:27:40 -05:00
Stephen Canon f6c2c34910 Rename unsafeLengthSquared -> lengthSquared
Also further tweak polar init for Complex to allow (-infinity, θ) when θ is not finite.
2019-11-20 16:15:10 -05:00
Stephen Canon f1d78c4d98 Merge pull request #77 from stephentyrone/zero-length-polar-init
Address behavior of Complex.init(length:phase:)
2019-11-20 16:03:09 -05:00
Stephen CanonandRichard Wei 5931d036ab Update Sources/Complex/Complex.swift
Co-Authored-By: Richard Wei <rxrwei@gmail.com>
2019-11-20 14:56:31 -05:00
Stephen Canon 565839d673 Address behavior of Complex.init(length:phase:)
Instead of being failable, adopt the following policy:
- If length is zero or infinity, the phase does not matter.
- Otherwise, the phase must be finite or a precondition failure occurs.

Discussion here: https://github.com/apple/swift-numerics/issues/51
2019-11-20 14:32:19 -05:00
Stephen Canon f96c6c2066 Rename swift.yml to macos.yml 2019-11-18 16:34:11 -05:00
Stephen Canon ee54cc2075 Let's tryout workflows 2019-11-13 17:17:32 -05:00
8 changed files with 262 additions and 70 deletions

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
name: Swift
on: [push]
jobs:
build:
runs-on: macOS-latest
steps:
- uses: actions/checkout@v1
- name: Build
run: swift build -v
- name: Run tests
run: swift test -v -c release
+4 -4
View File
@@ -73,9 +73,9 @@ extension Complex: Numeric {
// Try the naive expression z/w = z*conj(w) / |w|^2; if we can compute
// this without over/underflow, everything is fine and the result is
// correct. If not, we have to rescale and do the computation carefully.
let lengthSquared = w.unsafeLengthSquared
guard lengthSquared.isNormal else { return rescaledDivide(z, w) }
return z * (w.conjugate.divided(by: lengthSquared))
let lenSq = w.lengthSquared
guard lenSq.isNormal else { return rescaledDivide(z, w) }
return z * (w.conjugate.divided(by: lenSq))
}
@_transparent
@@ -98,7 +98,7 @@ extension Complex: Numeric {
let wScale = w.magnitude
let zNorm = z.divided(by: zScale)
let wNorm = w.divided(by: wScale)
let r = (zNorm * wNorm.conjugate).divided(by: wNorm.unsafeLengthSquared)
let r = (zNorm * wNorm.conjugate).divided(by: wNorm.lengthSquared)
// At this point, the result is (r * zScale)/wScale computed without
// undue overflow or underflow. We know that r is close to unity, so
// the question is simply what order in which to do this computation
+33 -17
View File
@@ -208,7 +208,7 @@ extension Complex {
/// The ∞-norm of the value (`max(abs(real), abs(imaginary))`).
///
/// If you need the euclidean norm (a.k.a. 2-norm) use the `length` or `unsafeLengthSquared`
/// If you need the euclidean norm (a.k.a. 2-norm) use the `length` or `lengthSquared`
/// properties instead.
///
/// Edge cases:
@@ -220,7 +220,7 @@ extension Complex {
/// See also:
/// -
/// - `.length`
/// - `.unsafeLengthSquared`
/// - `.lengthSquared`
@_transparent
public var magnitude: RealType {
guard isFinite else { return .infinity }
@@ -394,13 +394,13 @@ extension Complex {
/// See also:
/// -
/// - `.magnitude`
/// - `.unsafeLengthSquared`
/// - `.lengthSquared`
/// - `.phase`
/// - `.polar`
/// - `init(r:θ:)`
@_transparent
public var length: RealType {
let naive = unsafeLengthSquared
let naive = lengthSquared
guard naive.isNormal else { return carefulLength }
return .sqrt(naive)
}
@@ -419,7 +419,7 @@ extension Complex {
///
/// This property is more efficient to compute than `length`, but is
/// highly prone to overflow or underflow; for finite values that are
/// not well-scaled, `unsafeLengthSquared` is often either zero or
/// not well-scaled, `lengthSquared` is often either zero or
/// infinity, even when `length` is a finite number. Use this property
/// only when you are certain that this value is well-scaled.
///
@@ -431,10 +431,13 @@ extension Complex {
/// - `.length`
/// - `.magnitude`
@_transparent
public var unsafeLengthSquared: RealType {
public var lengthSquared: RealType {
x*x + y*y
}
@available(*, unavailable, renamed: "lengthSquared")
public var unsafeLengthSquared: RealType { lengthSquared }
/// The phase (angle, or "argument").
///
/// Returns the angle (measured above the real axis) in radians. If
@@ -472,26 +475,39 @@ extension Complex {
(length, phase)
}
/// Constructs a complex value from polar coordinates.
/// Creates a complex value specified with polar coordinates.
///
/// Edge cases:
/// -
/// If the phase is non-finite, but length is finite, this initializer
/// fails and returns nil. In all other cases, a non-nil value is constructed.
/// - Negative lengths are interpreted as reflecting the point through the origin, i.e.:
/// ```
/// Complex(length: -r, phase: θ) == -Complex(length: r, phase: θ)
/// ```
/// - For any `θ`, even `.infinity` or `.nan`:
/// ```
/// Complex(length: .zero, phase: θ) == .zero
/// ```
/// - For any `θ`, even `.infinity` or `.nan`, if `r` is infinite then:
/// ```
/// Complex(length: r, phase: θ) == .infinity
/// ```
/// - Otherwise, `θ` must be finite, or a precondition failure occurs.
///
/// See also:
/// -
/// - `.length`
/// - `.phase`
/// - `.polar`
public init?(length: RealType, phase: RealType) {
guard phase.isFinite else {
// There's no way to make sense of finite magnitude and non-finite phase.
if length.isFinite { return nil }
// r is infinite so phase doesn't matter.
self = .infinity
return
@inlinable
public init(length: RealType, phase: RealType) {
if phase.isFinite {
self = Complex(.cos(phase), .sin(phase)).multiplied(by: length)
} else {
precondition(
length.isZero || length.isInfinite,
"Either phase must be finite, or length must be zero or infinite."
)
self = Complex(length)
}
self.init(length * .cos(phase), length * .sin(phase))
}
}
+15 -5
View File
@@ -11,8 +11,22 @@
#define HEADER_SHIM static inline __attribute__((__always_inline__))
// MARK: - math functions for float
// This header uses most of the libm functions, but we don't want to end up
// exporting the libm declarations to modules that include NumericsShims, so
// we don't want to actually #include <math.h>.
//
// For most of the functions, we can get around this by using __builtin_func
// instead of func, since the compiler knows about these operations, but for
// the non-standard extensions, we need to include our own declarations. This
// is a little bit risky, in that we might end up missing an attribute that
// gets added and effects calling conventions, etc, but that's expected to be
// exceedingly rare.
//
// Still, we'll eventually want to find a better solution to this problem,
// especially if people start using this package on systems that are not
// Darwin or Ubuntu.
// MARK: - math functions for float
HEADER_SHIM float libm_cosf(float x) {
return __builtin_cosf(x);
}
@@ -131,9 +145,7 @@ HEADER_SHIM float libm_log10f(float x) {
#if !defined _WIN32
HEADER_SHIM float libm_lgammaf(float x, int *signp) {
#if __APPLE__
extern float lgammaf_r(float, int *);
#endif
return lgammaf_r(x, signp);
}
#endif
@@ -253,9 +265,7 @@ HEADER_SHIM double libm_log10(double x) {
#if !defined _WIN32
HEADER_SHIM double libm_lgamma(double x, int *signp) {
#if __APPLE__
extern double lgamma_r(double, int *);
#endif
return lgamma_r(x, signp);
}
#endif
+34 -14
View File
@@ -50,11 +50,21 @@ extension Float: Real {
}
@_transparent public static func pow(_ x: Float, _ n: Int) -> Float {
// TODO: this implementation is not quite correct, because n may be
// rounded in conversion to Float. This only effects very extreme cases,
// so we'll leave it alone for now; however, it gets the sign wrong if
// it rounds an odd number to an even number, so we should fix it soon.
return libm_powf(x, Float(n))
// If n is exactly representable as Float, we can just call powf:
if let y = Float(exactly: n) {
return libm_powf(x, y)
}
// Otherwise, n is too large to losslessly represent as Float.
// The range of "interesting" n is -1488522191 ... 1744361944; outside
// of this range, all x != 1 overflow or underflow, so only the parity
// of x matters. We don't really care about the specific range at all,
// only that the bounds fit exactly into two Floats. Mask the low 24
// bits of n, get pow with that exponent (this contains the parity),
// then get pow with the rest (this may round, but if it does we've
// saturated anyway, so it doesn't matter).
let low = n & 0xffffff
let high = n - low
return libm_powf(x, Float(low)) * libm_powf(x, Float(high))
}
@_transparent public static func root(_ x: Float, _ n: Int) -> Float {
@@ -129,11 +139,22 @@ extension Double: Real {
}
@_transparent public static func pow(_ x: Double, _ n: Int) -> Double {
// TODO: this implementation is not quite correct, because n may be
// rounded in conversion to Double. This only effects very extreme cases,
// so we'll leave it alone for now; however, it gets the sign wrong if
// it rounds an odd number to an even number, so we should fix it soon.
return libm_pow(x, Double(n))
// If n is exactly representable as Double, we can just call pow:
// Note that all calls on a 32b platform go down this path.
if let y = Double(exactly: n) {
return libm_pow(x, y)
}
// Otherwise, n is too large to losslessly represent as Double, so we
// just split it into two parts, high and low. This is always exact,
// so the only source of error is pow itself and the multiplication.
//
// mask constant is spelled in this funny way because if we just anded
// with the hex value, we'd get a compile error on 32b platforms, even
// though this whole branch is dead code on 32b.
let mask = Int(truncatingIfNeeded: 0x1fffffffffffff as UInt64)
let low = n & mask
let high = n - low
return libm_pow(x, Double(low)) * libm_pow(x, Double(high))
}
@_transparent public static func root(_ x: Double, _ n: Int) -> Double {
@@ -191,10 +212,9 @@ extension Float80: Real {
}
@_transparent public static func pow(_ x: Float80, _ n: Int) -> Float80 {
// TODO: this implementation is not quite correct, because n may be
// rounded in conversion to Float80. This only effects very extreme cases,
// so we'll leave it alone for now; however, it gets the sign wrong if
// it rounds an odd number to an even number, so we should fix it soon.
// Every Int value is exactly representable as Float80, so we don't need
// to do anything fancy--unlike Float and Double, we can just call the
// libm pow function.
return libm_powl(x, Float80(n))
}
@@ -28,7 +28,7 @@ extension Complex where RealType == Double {
let wellScaledDoubles: [Complex<Double>] = (0 ..< 1024).map { _ in
Complex(length: Double.random(in: 0.5 ..< 2.0),
phase: Double.random(in: -.pi ..< .pi))!
phase: Double.random(in: -.pi ..< .pi))
}
let poorlyScaledDoubles: [Complex<Double>] = (0 ..< 1024).map { _ in
@@ -37,7 +37,7 @@ let poorlyScaledDoubles: [Complex<Double>] = (0 ..< 1024).map { _ in
exponent: .random(in: -970 ... 1023),
significand: .random(in: 1 ..< 2)),
phase: Double.random(in: -.pi ..< .pi)
)!
)
}
final class ArithmeticBenchmarkTests: XCTestCase {
+30 -9
View File
@@ -63,6 +63,20 @@ final class ArithmeticTests: XCTestCase {
func testPolar<T>(_ type: T.Type)
where T: BinaryFloatingPoint, T: Real,
T.Exponent: FixedWidthInteger, T.RawSignificand: FixedWidthInteger {
// In order to support round-tripping from rectangular to polar coordinate
// systems, as a special case phase can be non-finite when length is
// either zero or infinity.
XCTAssertEqual(Complex<T>(length: .zero, phase: .infinity), .zero)
XCTAssertEqual(Complex<T>(length: .zero, phase:-.infinity), .zero)
XCTAssertEqual(Complex<T>(length: .zero, phase: .nan ), .zero)
XCTAssertEqual(Complex<T>(length: .infinity, phase: .infinity), .infinity)
XCTAssertEqual(Complex<T>(length: .infinity, phase:-.infinity), .infinity)
XCTAssertEqual(Complex<T>(length: .infinity, phase: .nan ), .infinity)
XCTAssertEqual(Complex<T>(length:-.infinity, phase: .infinity), .infinity)
XCTAssertEqual(Complex<T>(length:-.infinity, phase:-.infinity), .infinity)
XCTAssertEqual(Complex<T>(length:-.infinity, phase: .nan ), .infinity)
let exponentRange =
(T.leastNormalMagnitude.exponent + T.Exponent(T.significandBitCount)) ...
T.greatestFiniteMagnitude.exponent
@@ -81,7 +95,7 @@ final class ArithmeticTests: XCTestCase {
// this is good--more test coverage!--and bad, because without tight
// bounds on every platform's libm, we can't get tight bounds on the
// accuracy of these operations, so we need to relax them gradually).
let z = Complex(length: p.length, phase: p.phase)!
let z = Complex(length: p.length, phase: p.phase)
if !closeEnough(z.length, p.length, ulps: 16) {
print("p = \(p)\nz = \(z)\nz.length = \(z.length)")
XCTFail()
@@ -90,23 +104,30 @@ final class ArithmeticTests: XCTestCase {
print("p = \(p)\nz = \(z)\nz.phase = \(z.phase)")
XCTFail()
}
// if length*length is normal, it should be unsafeLengthSquared, up
// Complex(length: -r, phase: θ) = -Complex(length: r, phase: θ).
let w = Complex(length: -p.length, phase: p.phase)
if w != -z {
print("p = \(p)\nw = \(w)\nz = \(z)")
XCTFail()
}
XCTAssertEqual(w, -z)
// if length*length is normal, it should be lengthSquared, up
// to small error.
if (p.length*p.length).isNormal {
if !closeEnough(z.unsafeLengthSquared, p.length*p.length, ulps: 16) {
print("p = \(p)\nz = \(z)\nz.unsafeLengthSquared = \(z.unsafeLengthSquared)")
if !closeEnough(z.lengthSquared, p.length*p.length, ulps: 16) {
print("p = \(p)\nz = \(z)\nz.lengthSquared = \(z.lengthSquared)")
XCTFail()
}
}
// Test reciprocal and normalized:
let r = Complex(length: 1/p.length, phase: -p.phase)!
let r = Complex(length: 1/p.length, phase: -p.phase)
if r.isNormal {
if relativeError(r, z.reciprocal!) > 16 {
print("p = \(p)\nz = \(z)\nz.reciprocal = \(r)")
XCTFail()
}
} else { XCTAssertNil(z.reciprocal) }
let n = Complex(length: 1, phase: p.phase)!
let n = Complex(length: 1, phase: p.phase)
if relativeError(n, z.normalized!) > 16 {
print("p = \(p)\nz = \(z)\nz.normalized = \(n)")
XCTFail()
@@ -114,10 +135,10 @@ final class ArithmeticTests: XCTestCase {
// Now test multiplication and division using the polar inputs:
for q in inputs {
let w = Complex(length: q.length, phase: q.phase)!
let product = Complex(length: p.length * q.length, phase: p.phase + q.phase)!
let w = Complex(length: q.length, phase: q.phase)
let product = Complex(length: p.length * q.length, phase: p.phase + q.phase)
if checkMultiply(z, w, expected: product, ulps: 16) { XCTFail() }
let quotient = Complex(length: p.length / q.length, phase: p.phase - q.phase)!
let quotient = Complex(length: p.length / q.length, phase: p.phase - q.phase)
if checkDivide(z, w, expected: quotient, ulps: 16) { XCTFail() }
}
}
+129 -19
View File
@@ -1,4 +1,4 @@
//===--- ElementaryFunctionTests.swift ------------------------*- swift -*-===//
//===--- ElementaryFunctionChecks.swift ------------------------*- swift -*-===//
//
// This source file is part of the Swift Numerics open source project
//
@@ -22,11 +22,15 @@ func sanityCheck<T>(_ expected: TestLiteralType, _ actual: T,
ulps allowed: T = 16,
file: StaticString = #file, line: UInt = #line)
where T: BinaryFloatingPoint {
// Shortcut relative-error check if we got the sign wrong; it's OK to
// underflow to zero a little bit early, but we don't want to allow going
// right through zero to the other side.
XCTAssert(actual.sign == expected.sign, "\(actual) != \(expected) as \(T.self)", file: file, line: line)
// Default tolerance is 16 ulps; It's OK to relax this as needed for new
// platforms, as these tests are *not* intended to validate the math
// platforms, as these checks are *not* intended to validate the math
// library--they are only intended to check that the Swift bindings are
// calling the right functions in the math library. It's important, however
// not to relax the tolerance beyond a few hundred ulps, because these tests
// not to relax the tolerance beyond a few hundred ulps, because these checks
// need to detect errors where the *wrong function* is being called; e.g.
// we need to flag an implentation that inadvertently called the C hypotf
// function instead of hypot. This is especially important because the C
@@ -34,16 +38,26 @@ func sanityCheck<T>(_ expected: TestLiteralType, _ actual: T,
if actual == T(expected) || actual.isNaN && expected.isNaN {
return
}
// Compute error in ulp, compare to tolerance.
let absoluteError = T(abs(TestLiteralType(actual) - expected))
let ulpError = absoluteError / T(expected).ulp
XCTAssert(ulpError <= allowed, "\(actual) != \(expected) as \(T.self)" +
"\n \(ulpError)-ulp error exceeds \(allowed)-ulp tolerance.",
file: file, line: line)
// Special-case where expected or observed is infinity.
// Artificially knock everything down a binade, treat actual infinity as
// the base of the next binade up.
if actual.isInfinite || T(expected).isInfinite {
let scaledExpected = TestLiteralType(signOf: expected,
magnitudeOf: expected.isInfinite ? TestLiteralType.greatestFiniteMagnitude.binade : 0.5 * expected
)
let scaledActual = T(signOf: actual,
magnitudeOf: actual.isInfinite ? T.greatestFiniteMagnitude.binade : 0.5 * actual
)
return sanityCheck(scaledExpected, scaledActual, ulps: allowed, file: file, line: line)
}
// Compute error in ulp, compare to tolerance.
let absoluteError = T(abs(TestLiteralType(actual) - expected)).magnitude
let ulpError = absoluteError / max(T(expected).magnitude, T.leastNormalMagnitude).ulp
XCTAssert(ulpError <= allowed, "\(actual) != \(expected) as \(T.self)\n\(ulpError)-ulp error exceeds \(allowed)-ulp tolerance.", file: file, line: line)
}
internal extension ElementaryFunctions where Self: BinaryFloatingPoint {
static func elementaryFunctionTests() {
static func elementaryFunctionChecks() {
sanityCheck(1.1863995522992575361931268186727044683, Self.acos(0.375))
sanityCheck(0.3843967744956390830381948729670469737, Self.asin(0.375))
sanityCheck(0.3587706702705722203959200639264604997, Self.atan(0.375))
@@ -69,7 +83,7 @@ internal extension ElementaryFunctions where Self: BinaryFloatingPoint {
}
internal extension Real where Self: BinaryFloatingPoint {
static func realFunctionTests() {
static func realFunctionChecks() {
sanityCheck(1.2968395546510096659337541177924511598, Self.exp2(0.375))
sanityCheck(2.3713737056616552616517527574788898386, Self.exp10(0.375))
sanityCheck(-1.415037499278843818546261056052183491, Self.log2(0.375))
@@ -85,25 +99,121 @@ internal extension Real where Self: BinaryFloatingPoint {
XCTAssertEqual(.minus, Self.signGamma(-2.375))
#endif
}
static func testPownCommon() {
// 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.
XCTAssertEqual(Self.pow(-1, 0), 1)
XCTAssertEqual(Self.pow(-1, 1), -1)
XCTAssertEqual(Self.pow(-1, -1), -1)
XCTAssertEqual(Self.pow(-1, 2), 1)
XCTAssertEqual(Self.pow(-1, -2), 1)
XCTAssertEqual(Self.pow(-1, Int.max - 1), 1)
XCTAssertEqual(Self.pow(-1, -Int.max + 1), 1)
XCTAssertEqual(Self.pow(-1, Int.max), -1)
XCTAssertEqual(Self.pow(-1, -Int.max), -1)
XCTAssertEqual(Self.pow(-1, Int.min), 1)
}
}
final class ElementaryFunctionTests: XCTestCase {
extension Float {
static func testPown() {
testPownCommon()
let u = Float(1).nextUp
let d = Float(1).nextDown
// Smallest exponents not exactly representable as Float.
sanityCheck(-7.3890560989306677280287919329569359, Float.pow(-u, 0x1000001))
sanityCheck(-0.3678794082804575860056608283059288, Float.pow(-d, 0x1000001))
// Exponents close to overflow boundary.
sanityCheck(-3.4028231352500001570898203463449749e38, Float.pow(-u, 744261161))
sanityCheck( 3.4028235408981285772043562848249166e38, Float.pow(-u, 744261162))
sanityCheck(-3.4028239465463053543440887892352174e38, Float.pow(-u, 744261163))
sanityCheck( 3.4028233551634475284795244782720072e38, Float.pow(-d, -1488522190))
sanityCheck(-3.4028235579875369356575053576685267e38, Float.pow(-d, -1488522191))
sanityCheck( 3.4028237608116384320940078199368685e38, Float.pow(-d, -1488522192))
// Exponents close to underflow boundary.
sanityCheck( 7.0064936491761438872280296737844625e-46, Float.pow(-u, -872181048))
sanityCheck(-7.0064928139371132951305928725186420e-46, Float.pow(-u, -872181049))
sanityCheck( 7.0064919786981822712727285793333389e-46, Float.pow(-u, -872181050))
sanityCheck(-7.0064924138100205091278464932003585e-46, Float.pow(-d, 1744361943))
sanityCheck( 7.0064919961905290625123586120258840e-46, Float.pow(-d, 1744361944))
sanityCheck(-7.0064915785710625079583096856510544e-46, Float.pow(-d, 1744361945))
// Just hammer max/min exponents, these always saturate, but this will reveal
// errors in some implementations that one could try.
sanityCheck( .infinity, Self.pow(-u, Int.max - 1))
sanityCheck( 0.0, Self.pow(-d, Int.max - 1))
sanityCheck( 0.0, Self.pow(-u, -Int.max + 1))
sanityCheck( .infinity, Self.pow(-d, -Int.max + 1))
sanityCheck(-.infinity, Self.pow(-u, Int.max))
sanityCheck(-0.0, Self.pow(-d, Int.max))
sanityCheck(-0.0, Self.pow(-u, -Int.max))
sanityCheck(-.infinity, Self.pow(-d, -Int.max))
sanityCheck( 0.0, Self.pow(-u, Int.min))
sanityCheck( .infinity, Self.pow(-d, Int.min))
}
}
extension Double {
static func testPown() {
testPownCommon()
// Following tests only make sense (and are only necessary) on 64b platforms.
#if arch(arm64) || arch(x86_64)
let u: Double = 1.nextUp
let d: Double = 1.nextDown
// Smallest exponent not exactly representable as Double.
sanityCheck(-7.3890560989306502272304274605750685, Double.pow(-u, 0x20000000000001))
sanityCheck(-0.1353352832366126918939994949724833, Double.pow(-u, -0x20000000000001))
sanityCheck(-0.3678794411714422603312898889458068, Double.pow(-d, 0x20000000000001))
sanityCheck(-2.7182818284590456880451484776630468, Double.pow(-d, -0x20000000000001))
// Exponents close to overflow boundary.
sanityCheck( 1.7976931348623151738531864721534215e308, Double.pow(-u, 3196577161300664268))
sanityCheck(-1.7976931348623155730212483790972209e308, Double.pow(-u, 3196577161300664269))
sanityCheck( 1.7976931348623159721893102860411089e308, Double.pow(-u, 3196577161300664270))
sanityCheck( 1.7976931348623157075547244136070910e308, Double.pow(-d, -6393154322601327474))
sanityCheck(-1.7976931348623159071387553670790721e308, Double.pow(-d, -6393154322601327475))
sanityCheck( 1.7976931348623161067227863205510754e308, Double.pow(-d, -6393154322601327476))
// Exponents close to underflow boundary.
sanityCheck( 2.4703282292062334560337346683707907e-324, Double.pow(-u, -3355781687888880946))
sanityCheck(-2.4703282292062329075106789791206172e-324, Double.pow(-u, -3355781687888880947))
sanityCheck( 2.4703282292062323589876232898705654e-324, Double.pow(-u, -3355781687888880948))
sanityCheck(-2.4703282292062332640976590913373022e-324, Double.pow(-d, 6711563375777760775))
sanityCheck( 2.4703282292062329898361312467121758e-324, Double.pow(-d, 6711563375777760776))
sanityCheck(-2.4703282292062327155746034020870799e-324, Double.pow(-d, 6711563375777760777))
// Just hammer max/min exponents, these always saturate, but this will reveal
// errors in some implementations that one could try.
sanityCheck( .infinity, Self.pow(-u, Int.max - 1))
sanityCheck( 0.0, Self.pow(-d, Int.max - 1))
sanityCheck( 0.0, Self.pow(-u, -Int.max + 1))
sanityCheck( .infinity, Self.pow(-d, -Int.max + 1))
sanityCheck(-.infinity, Self.pow(-u, Int.max))
sanityCheck(-0.0, Self.pow(-d, Int.max))
sanityCheck(-0.0, Self.pow(-u, -Int.max))
sanityCheck(-.infinity, Self.pow(-d, -Int.max))
sanityCheck( 0.0, Self.pow(-u, Int.min))
sanityCheck( .infinity, Self.pow(-d, Int.min))
#endif
}
}
final class ElementaryFunctionChecks: XCTestCase {
func testFloat() {
Float.elementaryFunctionTests()
Float.realFunctionTests()
Float.elementaryFunctionChecks()
Float.realFunctionChecks()
Float.testPown()
}
func testDouble() {
Double.elementaryFunctionTests()
Double.realFunctionTests()
Double.elementaryFunctionChecks()
Double.realFunctionChecks()
Double.testPown()
}
#if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android)
func testFloat80() {
Float80.elementaryFunctionTests()
Float80.realFunctionTests()
Float80.elementaryFunctionChecks()
Float80.realFunctionChecks()
}
#endif
}