Files
swift-numerics/Sources/Real/Real.swift
T
Stephen Canon e24586e2b1 Defer to libm cbrt for root(x,3).
This lets us get exact cases right only platforms where cbrt(x) has sub-ulp accuracy. It doesn't get all the other non-power-of-two roots quite right, and is still dependent on the host system, but 2 and 3 are the most common roots people take by far, so it's a good first step.
2019-11-10 22:02:53 -05:00

83 lines
3.1 KiB
Swift

//===--- Real.swift -------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift Numerics open source project
//
// Copyright (c) 2019 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
//
//===----------------------------------------------------------------------===//
/// A type that models the real numbers.
///
/// Types conforming to this protocol provide the arithmetic and utility operations defined by
/// the `FloatingPoint` protocol, and provide all of the math functions defined by the
/// `ElementaryFunctions` and `RealFunctions` protocols. This protocol does not
/// add any additional conformances itself, but is very useful as a protocol against which to
/// write generic code. For example, we can naturally write a generic version of the a sigmoid
/// function:
/// ```
/// func sigmoid<T: Real>(_ x: T) -> T {
/// return 1/(1 + .exp(-x))
/// }
/// ```
/// See Also:
/// -
/// - `ElementaryFunctions`
/// - `RealFunctions`
public protocol Real: FloatingPoint, RealFunctions {
}
// While `Real` does not provide any additional customization points,
// it does allow us to default the implementation of a few operations,
// and also provides `signGamma`.
extension Real {
// Most math libraries do not provide exp10, so we need a default implementation.
@_transparent
public static func exp10(_ x: Self) -> Self {
return pow(10, x)
}
#if !os(Windows)
public static func signGamma(_ x: Self) -> FloatingPointSign {
// Gamma is strictly positive for x >= 0.
if x >= 0 { return .plus }
// For negative x, we arbitrarily choose to assign a sign of .plus to the
// poles.
let trunc = x.rounded(.towardZero)
if x == trunc { return .plus }
// Otherwise, signGamma is .minus if the integral part of x is even.
return trunc.isEven ? .minus : .plus
}
// Determines if this value is even, assuming that it is an integer.
@inline(__always)
private var isEven: Bool {
if Self.radix == 2 {
// For binary types, we can just check if x/2 is an integer. This works
// because x/2 is always computed exactly.
let half = self/2
return half == half.rounded(.towardZero)
} else {
// For decimal types, it's not quite that simple, because x/2 is not
// necessarily computed exactly. As an example, suppose that we had a
// decimal type with a one digit significand, and self = 7. Then self/2
// would round to 4, and we would (wrongly) conclude that it was an
// integer, and hence that self was even.
//
// Instead, for decimal types, we check if 2*trunc(self/2) == self,
// using an FMA; this is always correct; this approach works for any
// radix, but the previous method is more efficient for radix == 2.
let half = self/2
return self.addingProduct(-2, half.rounded(.towardZero)) == 0
}
}
#endif
@_transparent
public static func sqrt(_ x: Self) -> Self {
return x.squareRoot()
}
}