Measurement

public protocol Measurement : Addable, Comparable, Equatable, Negatable, NumericAdditiveArithmetic

A type that represents a measurement that can be expressed in various units.

See Angle for an example.

Requires

A Measurement’s units must be definable as ratios of one another. (For example, Measurement can describe angles as radians, degrees and gradians, but not temperature as Kelvins, Celsius and Fahrenheit.)

Conformance Requirements:

  • init(rawValue: Scalar)
  • var rawValue: Scalar { get set }
  • The numeric type used to express the value in any given unit.

    Declaration

    Swift

    associatedtype Scalar : RationalArithmetic
  • A rule for rounding.

    Declaration

    Swift

    typealias RoundingRule = FloatingPointRoundingRule
  • Creates a measurement from a raw value in undefined but consistent units.

    Used by Measurement’s default implementation of methods where various units make no difference (such as multiplication by a scalar).

    Declaration

    Swift

    init(rawValue: Scalar)
  • A raw value in undefined but consistent units.

    Used by Measurement’s default implementation of methods where various units make no difference (such as multiplication by a scalar).

    Declaration

    Swift

    var rawValue: Scalar
  • init() Default implementation

    Creates an empty (zero) measurement.

    Default Implementation

    Creates an empty (zero) measurement.

    Declaration

    Swift

    init()
  • ×(_:_:) Default implementation

    Returns the result of multipling the measurement by the scalar.

    Mutating variant

    ×=

    Default Implementation

    Returns the result of multipling the measurement by the scalar.

    Mutating variant

    ×=

    Returns the result of multipling the measurement by the scalar.

    Mutating variant

    ×=

    Declaration

    Swift

    static func × (lhs: Self, rhs: Scalar) -> Self

    Parameters

    lhs

    The measurement.

    rhs

    The scalar.

  • ×=(_:_:) Default implementation

    Modifies the measurement by multiplication with a scalar.

    Nonmutating variant

    ×

    Default Implementation

    Modifies the measurement by multiplication with a scalar.

    Nonmutating variant

    ×

    Declaration

    Swift

    static func ×= (lhs: inout Self, rhs: Scalar)

    Parameters

    lhs

    The measurement to modify.

    rhs

    The scalar.

  • ÷(_:_:) Default implementation

    Returns the (rational) quotient of a measurement divided by a scalar.

    Mutating variant

    ×

    Default Implementation

    Returns the (rational) quotient of a measurement divided by a scalar.

    Mutating variant

    ×

    Returns the (rational) scalar quotient of the left divided by the right.

    Declaration

    Swift

    static func ÷ (lhs: Self, rhs: Scalar) -> Self

    Parameters

    lhs

    The measurement.

    rhs

    The scalar.

  • Returns the (rational) scalar quotient of the left divided by the right.

    Declaration

    Swift

    static func ÷ (lhs: Self, rhs: Self) -> Scalar

    Parameters

    lhs

    The dividend.

    rhs

    The divisor.

  • ÷=(_:_:) Default implementation

    Modifies the left by dividing it by the right.

    Nonmutating variant

    ÷

    Default Implementation

    Modifies the left by dividing it by the right.

    Nonmutating variant

    ÷

    Declaration

    Swift

    static func ÷= (lhs: inout Self, rhs: Scalar)

    Parameters

    lhs

    The measurement to modify.

    rhs

    The scalar divisor.

  • dividedAccordingToEuclid(by:) Default implementation

    Returns the integral quotient of self divided by divisor.

    Note

    This is a true mathematical quotient. i.e. (−5) ÷ 3 = −2 remainder 1, not −1 remainder −2

    Mutating variant

    divideAccordingToEuclid

    Default Implementation

    Returns the integral quotient of self divided by divisor.

    Note

    This is a true mathematical quotient. i.e. (−5) ÷ 3 = −2 remainder 1, not −1 remainder −2

    Mutating variant

    divideAccordingToEuclid

    Declaration

    Swift

    func dividedAccordingToEuclid(by divisor: Self) -> Scalar

    Parameters

    divisor

    The divisor.

  • mod(_:) Default implementation

    Returns the Euclidean remainder of self ÷ divisor.

    Note

    This is a true mathematical modulo operation. i.e. (−5) mod 3 = 1, not −2

    Mutating variant

    formRemainder

    Default Implementation

    Returns the Euclidean remainder of self ÷ divisor.

    Note

    This is a true mathematical modulo operation. i.e. (−5) mod 3 = 1, not −2

    Mutating variant

    formRemainder

    Declaration

    Swift

    func mod(_ divisor: Self) -> Self

    Parameters

    divisor

    The divisor.

  • formRemainder(mod:) Default implementation

    Sets self to the Euclidean remainder of self ÷ divisor.

    Note

    This is a true mathematical modulo operation. i.e. (−5) mod 3 = 1, not −2

    Nonmutating variant

    mod

    Default Implementation

    Sets self to the Euclidean remainder of self ÷ divisor.

    Note

    This is a true mathematical modulo operation. i.e. (−5) mod 3 = 1, not −2

    Nonmutating variant

    mod

    Declaration

    Swift

    mutating func formRemainder(mod divisor: Self)

    Parameters

    divisor

    The divisor.

  • isDivisible(by:) Default implementation

    Returns true if self is evenly divisible by divisor.

    Default Implementation

    Returns true if self is evenly divisible by divisor.

    Declaration

    Swift

    func isDivisible(by divisor: Self) -> Bool
  • gcd(_:_:) Default implementation

    Returns the greatest common divisor of a and b.

    Mutating variant

    formGreatestCommonDivisor

    Default Implementation

    Returns the greatest common divisor of a and b.

    Mutating variant

    formGreatestCommonDivisor

    Declaration

    Swift

    static func gcd(_ a: Self, _ b: Self) -> Self

    Parameters

    lhs

    A value.

    rhs

    Another value.

  • formGreatestCommonDivisor(with:) Default implementation

    Sets self to the greatest common divisor of self and other.

    Nonmutating variant

    gcd

    Default Implementation

    Sets self to the greatest common divisor of self and other.

    Nonmutating variant

    gcd

    Declaration

    Swift

    mutating func formGreatestCommonDivisor(with other: Self)

    Parameters

    other

    Another value.

  • lcm(_:_:) Default implementation

    Returns the least common multiple of a and b.

    Mutating variant

    formGreatestCommonDivisor

    Default Implementation

    Returns the least common multiple of a and b.

    Mutating variant

    formGreatestCommonDivisor

    Declaration

    Swift

    static func lcm(_ a: Self, _ b: Self) -> Self

    Parameters

    lhs

    A value.

    rhs

    Another value.

  • formLeastCommonMultiple(with:) Default implementation

    Sets self to the least common multiple of self and other.

    Nonmutating variant

    lcm

    Default Implementation

    Sets self to the least common multiple of self and other.

    Nonmutating variant

    lcm

    Declaration

    Swift

    mutating func formLeastCommonMultiple(with other: Self)

    Parameters

    other

    Another value.

  • round(_:toMultipleOf:) Default implementation

    Rounds the value to a multiple of factor using the specified rounding rule.

    Nonmutating variant

    rounded

    Default Implementation

    Rounds the value to a multiple of factor using the specified rounding rule.

    Nonmutating variant

    rounded

    Declaration

    Swift

    mutating func round(_ rule: RoundingRule, toMultipleOf factor: Self)

    Parameters

    rule

    The rounding rule follow.

    factor

    The factor to round to a multiple of.

  • rounded(_:toMultipleOf:) Default implementation

    Returns the value rounded to a multiple of factor using the specified rounding rule.

    Mutating variant

    round

    Default Implementation

    Returns the value rounded to a multiple of factor using the specified rounding rule.

    Mutating variant

    round

    Declaration

    Swift

    func rounded(_ rule: RoundingRule, toMultipleOf factor: Self) -> Self

    Parameters

    rule

    The rounding rule follow.

    factor

    The factor to round to a multiple of.

  • init(randomInRange:) Default implementation

    Creates a random value within a particular range.

    Precondition

    range is not empty.

    Default Implementation

    Creates a random value within a particular range.

    Precondition

    range is not empty.

    Creates a random value within a particular range.

    Precondition

    range is not empty.

    Declaration

    Swift

    init(randomInRange range: Range<Self>)

    Parameters

    range

    The allowed range for the random value.

  • Creates a random value within a particular range.

    Precondition

    range is not empty.

    Declaration

    Swift

    init(randomInRange range: ClosedRange<Self>)

    Parameters

    range

    The allowed range for the random value.

  • init(randomInRange:fromRandomizer:) Default implementation

    Creates a random value within a particular range using the specified randomizer.

    Precondition

    range is not empty.

    Default Implementation

    Creates a random value within a particular range using the specified randomizer.

    Precondition

    range is not empty.

    Creates a random value within a particular range using the specified randomizer.

    Precondition

    range is not empty.

    Declaration

    Swift

    init(randomInRange range: Range<Self>, fromRandomizer randomizer: Randomizer)

    Parameters

    range

    The allowed range for the random value.

    randomizer

    The randomizer to use to generate the random value.

  • Creates a random value within a particular range using the specified randomizer.

    Precondition

    range is not empty.

    Declaration

    Swift

    init(randomInRange range: ClosedRange<Self>, fromRandomizer randomizer: Randomizer)

    Parameters

    range

    The allowed range for the random value.

    randomizer

    The randomizer to use to generate the random value.