NumericAnnex Reference (100% documented)

View on GitHub

Install in Dash

Structures

The following structures are available globally.

Topics
  • Complex

    A type to represent a complex value in Cartesian form.

    Note

    Complex64 is a type alias for Complex<Float> and Complex128 is a type alias for Complex<Double>.

    Create new instances of Complex<T> using integer or floating-point literals and the imaginary unit Complex<T>.i. For example:

    let x = 2 + 4 * .i // `x` is of type `Complex<Double>`
    let y = 3.5 + 7 * .i // `y` is of type `Complex<Double>`
    
    let z: Complex64 = .e + .pi * .i // `z` is of type `Complex<Float>`
    

    Additional Considerations

    Floating-point types have special values that represent infinity or NaN (not a number). Complex functions in different languages may return different results when working with special values.

    Many complex functions have branch cuts, which are curves in the complex plane across which a function is discontinuous. Different languages may adopt different branch cut structures for the same complex function.

    Implementations in Complex<T> adhere to the C standard (Annex G) as closely as possible with respect to special values and branch cuts.

    To users unfamiliar with complex functions, the principal value returned by some complex functions may be unexpected. For example, Double.cbrt(-8) == -2, which is the real root, while Complex.cbrt(-8) == 2 * Complex.exp(.i * .pi / 3), which is the principal root.

    See more…
    Declaration

    Swift

    public struct Complex<T : Real> : Codable
    where T : Codable & _ExpressibleByBuiltinFloatLiteral
  • Rational

    A type to represent a rational value.

    Note

    Ratio is a type alias for Rational<Int>.

    Create new instances of Rational<T> by using integer literals and the division (/) operator. For example:

    let x = 1 / 3 as Ratio // `x` is of type `Rational<Int>`
    let y = 2 as Ratio // `y` is of type `Rational<Int>`
    let z: Ratio = 2 / 3 // `z` is also of type `Rational<Int>`
    
    print(x + y + z) // Prints "3"
    

    You can create an unreduced fraction by using the initializer Rational<T>.init(numerator:denominator:). For example:

    let a = Ratio(numerator: 3, denominator: 3)
    print(a) // Prints "3/3"
    

    All arithmetic operations on values in canonical form (i.e. reduced to lowest terms) return results in canonical form. However, operations on values not in canonical form may or may not return results that are themselves in canonical form. The property canonicalized is the canonical form of any value.

    Additional Considerations

    Special Values

    Rational<T> does not prohibit zero as a denominator. Any instance with a positive numerator and zero denominator represents (positive) infinity; any instance with a negative numerator and zero denominator represents negative infinity; and any instance with zero numerator and zero denominator represents NaN (not a number).

    As with floating-point types, Rational<T>.infinity compares greater than every finite value and negative infinity, and -Rational<T>.infinity compares less than every finite value and positive infinity. Infinite values of the same sign compare equal to each other.

    As with floating-point types, Rational<T>.nan does not compare equal to any other value, including another NaN. Use the property isNaN to test if a value is NaN. Rational<T> arithmetic operations are intended to propagate NaN in the same manner as analogous floating-point operations.

    Numerical Limits

    When a value of type Rational<T> is in canonical form, the sign of the numerator is the sign of the value; that is, in canonical form, the sign of the denominator is always positive. Therefore, -1 / T.min cannot be represented as a value of type Rational<T> because abs(T.min) cannot be represented as a value of type T.

    To ensure that every representable value of type Rational<T> has a representable magnitude and reciprocal of the same type, an overflow trap occurs when the division (/) operator is used to create a value of type Rational<T> with numerator T.min.

    See more…
    Declaration

    Swift

    public struct Rational<T : SignedInteger> : Codable
    where T : Codable & _ExpressibleByBuiltinIntegerLiteral,
      T.Magnitude : UnsignedInteger,
      T.Magnitude.Magnitude == T.Magnitude