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 of0
for the denominator.
-
Numerator.
Declaration
Swift
public let numerator: Int
-
Denominator.
Declaration
Swift
public let denominator: Int
-
Creates a
Fraction
value with a givennumerator
anddenominator
.Declaration
Swift
public init(_ numerator: Int, _ denominator: Int)
-
Creates a
Fraction
value with the givenrational
value.You may have created your own
Rational
-conforming type (e.g.,MetricalDuration
, etc.) which adds its own invariants to theRational
type. It may be useful to generalize your type to anotherRational
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)