Skip to content

Instantly share code, notes, and snippets.

@kazk
Created January 29, 2015 11:39
Show Gist options
  • Save kazk/cc765d01727b06bfd479 to your computer and use it in GitHub Desktop.
Save kazk/cc765d01727b06bfd479 to your computer and use it in GitHub Desktop.
Type supporting functions defined in <math.h>
import Darwin
// MARK: - MathFunctionsType -
/// Type supporting functions defined in <math.h>
public protocol MathFunctionsType {
// MARK: Basic operations
/// Computes absolute value of a floating-point value (|x|)
class func fabs(x: Self) -> Self
/// Computes remainder of the floating-point division operation
class func fmod(x: Self, _ y: Self) -> Self
/// Computes signed remainder of the floating-point division operation
class func remainder(x: Self, _ y: Self) -> Self
/// Computes signed remainder as well as the three last bits of the division operation
class func remquo(x: Self, _ y: Self) -> (Self, Int)
/// Computes fused multiply-add operation
class func fma(x: Self, _ y: Self, _ z: Self) -> Self
/// Computes larger of two floating-point values
class func fmax(x: Self, _ y: Self) -> Self
/// Computes smaller of two floating-point values
class func fmin(x: Self, _ y: Self) -> Self
/// Computes positive difference of two floating-point values (max(0, x - y))
class func fdim(x: Self, _ y: Self) -> Self
/// Returns a NaN (not-a-number)
class func nan(tag: String) -> Self
// MARK: Exponential functions
/// Computes e raised to the given power (eⁿ)
class func exp(x: Self) -> Self
/// Computes e raised to the given power, minus one (eⁿ - 1)
class func expm1(x: Self) -> Self
/// Computes 2 raised to the given power (2ⁿ)
class func exp2(x: Self) -> Self
/// Computes natural (base-e) logarithm (ln(x))
class func log(x: Self) -> Self
/// Computes natural (base-e) logarithm of 1 plus the given number (ln(1 + x))
class func log1p(x: Self) -> Self
/// Computes base-2 logarithm (log₂(x))
class func log2(x: Self) -> Self
/// Computes common (base-10) logarithm (log₁₀(x))
class func log10(x: Self) -> Self
// MARK: Power functions
/// Computes a number raised to the given power (xⁿ)
class func pow(x: Self, _ y: Self) -> Self
/// Computes square root (√x)
class func sqrt(x: Self) -> Self
/// Computes cube root (∛x)
class func cbrt(x: Self) -> Self
/// Computes square root of the sum of the squares of two given numbers ((√(x² + y²))
class func hypot(x: Self, _ y: Self) -> Self
// MARK: Trigonometric functions
/// Computes sine (sin(x))
class func sin(x: Self) -> Self
/// Computes cosine (cos(x))
class func cos(x: Self) -> Self
/// Computes tangent (tan(x))
class func tan(x: Self) -> Self
/// Computes arc sine (arcsine(x))
class func asin(x: Self) -> Self
/// Computes arc cosine (arccos(x))
class func acos(x: Self) -> Self
/// Computes arc tangent (arctan(x))
class func atan(x: Self) -> Self
/// Computes arc tangent, using signs to determine quadrants
class func atan2(y: Self, _ x: Self) -> Self
// MARK: Hyperbolic functions
/// Computes hyperbolic sine (sinh(x))
class func sinh(x: Self) -> Self
/// Computes hyperbolic cosine (cosh(x))
class func cosh(x: Self) -> Self
/// Computes hyperbolic tangent (tanh(x))
class func tanh(x: Self) -> Self
/// Computes inverse hyperbolic sine (arcsinh(x))
class func asinh(x: Self) -> Self
/// Computes inverse hyperbolic cosine (arccosh(x))
class func acosh(x: Self) -> Self
/// Computes inverse hyperbolic tangent (arctanh(x))
class func atanh(x: Self) -> Self
// MARK: Error and gamma functions
/// Computes error function
class func erf(x: Self) -> Self
/// Computes complementary error function
class func erfc(x: Self) -> Self
/// Computes gamma function
class func tgamma(x: Self) -> Self
/// Computes natural (base-e) logarithm of the gamma function
class func lgamma(x: Self) -> Self
// MARK: Nearest integer floating-point operations
/// Computes smallest integer not less than the given value (⌈x⌉)
class func ceil(x: Self) -> Self
/// Computes largest integer not greater than the given value (⌊x⌋)
class func floor(x: Self) -> Self
/// Rounds to nearest integer not greater in magnitude than the given value
class func trunc(x: Self) -> Self
/// Rounds to nearest integer, rounding away from zero in halfway cases
class func round(x: Self) -> Self
/// Rounds to an integer using current rounding mode
class func nearbyint(x: Self) -> Self
/// Rounds to an integer using current rounding mode with exception if the result differs
class func rint(x: Self) -> Self
// class func lrint(x: Self) -> Int
// class func llrint(x: Self) -> Int64
// class func lround(x: Self) -> Int
// class func llround(x: Self) -> Int64
// MARK: Floating-point manipulation functions
/// Breaks a number into significand and a power of 2
class func frexp(x: Self) -> (Self, Int)
/// Multiplies a number by 2 raised to a power
class func ldexp(x: Self, _ n: Int) -> Self
/// Breaks a number into integer and fractional parts
class func modf(x: Self) -> (Self, Self)
/// Computes efficiently a number times FLT_RADIX raised to a power
class func scalbn(x: Self, _ n: Int) -> Self
// class func scalbln(x: Self, _ n: Int) -> Self
/// Extracts exponent of the given number
class func ilogb(x: Self) -> Int
/// Extracts exponent of the given number
class func logb(x: Self) -> Self
/// Determines next representable floating-point value towards the given value
class func nextafter(from: Self, _ to: Self) -> Self
/// Produces a value with the magnitude of a given value and the sign of another given value
class func copysign(x: Self, _ y: Self) -> Self
// MARK: Classification and comparison
/// Checks if the given number is negative.
///
/// Nonzero integral value if x is negative, ​0​ otherwise
class func signbit(x: Self) -> Int
// class func fpclassify(x: Self) -> Int
// class func isfinite(x: Self) -> Bool
// class func isinf(x: Self) -> Bool
// class func isnan(x: Self) -> Bool
// class func isnormal(x: Self) -> Bool
// class func isgreater(x: Self, y: Self) -> Bool
// class func isgreaterequal(x: Self, y: Self) -> Bool
// class func isless(x: Self, y: Self) -> Bool
// class func islessequal(x: Self, y: Self) -> Bool
// class func islessgreater(x: Self, y: Self) -> Bool
// class func isunordered(x: Self, y: Self) -> Bool
}
// MARK: - `Double` conforms to `MathFunctionsType`
extension Double : MathFunctionsType {
public static func fabs(x: Double) -> Double { return Darwin.fabs(x) }
public static func fmod(x: Double, _ y: Double) -> Double { return Darwin.fmod(x, y) }
public static func remainder(x: Double, _ y: Double) -> Double { return Darwin.remainder(x, y) }
public static func remquo(x: Double, _ y: Double) -> (Double, Int) { return Darwin.remquo(x, y) }
public static func fma(x: Double, _ y: Double, _ z: Double) -> Double { return Darwin.fma(x, y, z) }
public static func fmax(x: Double, _ y: Double) -> Double { return Darwin.fmax(x, y) }
public static func fmin(x: Double, _ y: Double) -> Double { return Darwin.fmin(x, y) }
public static func fdim(x: Double, _ y: Double) -> Double { return Darwin.fdim(x, y) }
public static func nan(tag: String) -> Double { return Darwin.nan(tag) }
public static func exp(x: Double) -> Double { return Darwin.exp(x) }
public static func expm1(x: Double) -> Double { return Darwin.expm1(x) }
public static func exp2(x: Double) -> Double { return Darwin.exp2(x) }
public static func log(x: Double) -> Double { return Darwin.log(x) }
public static func log1p(x: Double) -> Double { return Darwin.log1p(x) }
public static func log2(x: Double) -> Double { return Darwin.log2(x) }
public static func log10(x: Double) -> Double { return Darwin.log10(x) }
public static func pow(x: Double, _ y: Double) -> Double { return Darwin.pow(x, y) }
public static func sqrt(x: Double) -> Double { return Darwin.sqrt(x) }
public static func cbrt(x: Double) -> Double { return Darwin.cbrt(x) }
public static func hypot(x: Double, _ y: Double) -> Double { return Darwin.hypot(x, y) }
public static func sin(x: Double) -> Double { return Darwin.sin(x) }
public static func cos(x: Double) -> Double { return Darwin.cos(x) }
public static func tan(x: Double) -> Double { return Darwin.tan(x) }
public static func asin(x: Double) -> Double { return Darwin.asin(x) }
public static func acos(x: Double) -> Double { return Darwin.acos(x) }
public static func atan(x: Double) -> Double { return Darwin.atan(x) }
public static func atan2(y: Double, _ x: Double) -> Double { return Darwin.atan2(y, x) }
public static func sinh(x: Double) -> Double { return Darwin.sinh(x) }
public static func cosh(x: Double) -> Double { return Darwin.cosh(x) }
public static func tanh(x: Double) -> Double { return Darwin.tanh(x) }
public static func asinh(x: Double) -> Double { return Darwin.asinh(x) }
public static func acosh(x: Double) -> Double { return Darwin.acosh(x) }
public static func atanh(x: Double) -> Double { return Darwin.atanh(x) }
public static func erf(x: Double) -> Double { return Darwin.erf(x) }
public static func erfc(x: Double) -> Double { return Darwin.erfc(x) }
public static func tgamma(x: Double) -> Double { return Darwin.tgamma(x) }
public static func lgamma(x: Double) -> Double { return Darwin.lgamma(x) }
public static func ceil(x: Double) -> Double { return Darwin.ceil(x) }
public static func floor(x: Double) -> Double { return Darwin.floor(x) }
public static func trunc(x: Double) -> Double { return Darwin.trunc(x) }
public static func round(x: Double) -> Double { return Darwin.round(x) }
public static func nearbyint(x: Double) -> Double { return Darwin.nearbyint(x) }
public static func rint(x: Double) -> Double { return Darwin.rint(x) }
public static func frexp(x: Double) -> (Double, Int) { return Darwin.frexp(x) }
public static func ldexp(x: Double, _ n: Int) -> Double { return Darwin.ldexp(x, n) }
public static func modf(x: Double) -> (Double, Double) { return Darwin.modf(x) }
public static func scalbn(x: Double, _ n: Int) -> Double { return Darwin.scalbn(x, n) }
public static func ilogb(x: Double) -> Int { return Darwin.ilogb(x) }
public static func logb(x: Double) -> Double { return Darwin.logb(x) }
public static func nextafter(from: Double, _ to: Double) -> Double { return Darwin.nextafter(from, to) }
public static func copysign(x: Double, _ y: Double) -> Double { return Darwin.copysign(x, y) }
public static func signbit(x: Double) -> Int { return Darwin.signbit(x) }
}
// MARK: - `Float` conforms to `MathFunctionsType`
extension Float : MathFunctionsType {
public static func fabs(x: Float) -> Float { return Darwin.fabs(x) }
public static func fmod(x: Float, _ y: Float) -> Float { return Darwin.fmod(x, y) }
public static func remainder(x: Float, _ y: Float) -> Float { return Darwin.remainder(x, y) }
public static func remquo(x: Float, _ y: Float) -> (Float, Int) { return Darwin.remquo(x, y) }
public static func fma(x: Float, _ y: Float, _ z: Float) -> Float { return Darwin.fma(x, y, z) }
public static func fmax(x: Float, _ y: Float) -> Float { return Darwin.fmax(x, y) }
public static func fmin(x: Float, _ y: Float) -> Float { return Darwin.fmin(x, y) }
public static func fdim(x: Float, _ y: Float) -> Float { return Darwin.fdim(x, y) }
public static func nan(tag: String) -> Float { return Darwin.nan(tag) }
public static func exp(x: Float) -> Float { return Darwin.exp(x) }
public static func expm1(x: Float) -> Float { return Darwin.expm1(x) }
public static func exp2(x: Float) -> Float { return Darwin.exp2(x) }
public static func log(x: Float) -> Float { return Darwin.log(x) }
public static func log1p(x: Float) -> Float { return Darwin.log1p(x) }
public static func log2(x: Float) -> Float { return Darwin.log2(x) }
public static func log10(x: Float) -> Float { return Darwin.log10(x) }
public static func pow(x: Float, _ y: Float) -> Float { return Darwin.pow(x, y) }
public static func sqrt(x: Float) -> Float { return Darwin.sqrt(x) }
public static func cbrt(x: Float) -> Float { return Darwin.cbrt(x) }
public static func hypot(x: Float, _ y: Float) -> Float { return Darwin.hypot(x, y) }
public static func sin(x: Float) -> Float { return Darwin.sin(x) }
public static func cos(x: Float) -> Float { return Darwin.cos(x) }
public static func tan(x: Float) -> Float { return Darwin.tan(x) }
public static func asin(x: Float) -> Float { return Darwin.asin(x) }
public static func acos(x: Float) -> Float { return Darwin.acos(x) }
public static func atan(x: Float) -> Float { return Darwin.atan(x) }
public static func atan2(y: Float, _ x: Float) -> Float { return Darwin.atan2(y, x) }
public static func sinh(x: Float) -> Float { return Darwin.sinh(x) }
public static func cosh(x: Float) -> Float { return Darwin.cosh(x) }
public static func tanh(x: Float) -> Float { return Darwin.tanh(x) }
public static func asinh(x: Float) -> Float { return Darwin.asinh(x) }
public static func acosh(x: Float) -> Float { return Darwin.acosh(x) }
public static func atanh(x: Float) -> Float { return Darwin.atanh(x) }
public static func erf(x: Float) -> Float { return Darwin.erf(x) }
public static func erfc(x: Float) -> Float { return Darwin.erfc(x) }
public static func tgamma(x: Float) -> Float { return Darwin.tgamma(x) }
public static func lgamma(x: Float) -> Float { return Darwin.lgammaf(x) }
public static func ceil(x: Float) -> Float { return Darwin.ceil(x) }
public static func floor(x: Float) -> Float { return Darwin.floor(x) }
public static func trunc(x: Float) -> Float { return Darwin.trunc(x) }
public static func round(x: Float) -> Float { return Darwin.round(x) }
public static func nearbyint(x: Float) -> Float { return Darwin.nearbyint(x) }
public static func rint(x: Float) -> Float { return Darwin.rint(x) }
public static func frexp(x: Float) -> (Float, Int) { return Darwin.frexp(x) }
public static func ldexp(x: Float, _ n: Int) -> Float { return Darwin.ldexp(x, n) }
public static func modf(x: Float) -> (Float, Float) { return Darwin.modf(x) }
public static func scalbn(x: Float, _ n: Int) -> Float { return Darwin.scalbn(x, n) }
public static func ilogb(x: Float) -> Int { return Darwin.ilogb(x) }
public static func logb(x: Float) -> Float { return Darwin.logb(x) }
public static func nextafter(from: Float, _ to: Float) -> Float { return Darwin.nextafter(from, to) }
public static func copysign(x: Float, _ y: Float) -> Float { return Darwin.copysign(x, y) }
public static func signbit(x: Float) -> Int { return Darwin.signbit(x) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment