Fraction

public struct Fraction : Rational
extension Fraction: ExpressibleByIntegerLiteral

A representation of a Fraction.

Example Usage

Create a fraction.

let fraction = Fraction(5,13)

The Fraction is the most basic implementation of the Rational protocol, with the only added invariant that the denominator is not 0.

Warning

Your program will trap if you give a value of 0 for the denominator.
  • Numerator.

    Declaration

    Swift

    public let numerator: Int
  • Denominator.

    Declaration

    Swift

    public let denominator: Int

Initializers

  • Creates a Fraction value with a given numerator and denominator.

    Declaration

    Swift

    public init(_ numerator: Int, _ denominator: Int)
  • Creates a Fraction value with the given rational value.

    You may have created your own Rational-conforming type (e.g., MetricalDuration, etc.) which adds its own invariants to the Rational type. It may be useful to generalize your type to another Rational representation with less constraints.

    Declaration

    Swift

    public init<R>(_ rational: R) where R : Rational
  • Create a Fraction with an intger literal.

    Declaration

    Swift

    public init(integerLiteral value: Int)