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
AMeasurement’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 implementationCreates an empty (zero) measurement.
Default Implementation
Creates an empty (zero) measurement.
Declaration
Swift
init()
-
×(_:_:)Default implementationReturns 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) -> SelfParameters
lhsThe measurement.
rhsThe scalar.
-
×=(_:_:)Default implementationModifies 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
lhsThe measurement to modify.
rhsThe scalar.
-
÷(_:_:)Default implementationReturns 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) -> SelfParameters
lhsThe measurement.
rhsThe scalar.
-
Returns the (rational) scalar quotient of the left divided by the right.
Declaration
Swift
static func ÷ (lhs: Self, rhs: Self) -> ScalarParameters
lhsThe dividend.
rhsThe divisor.
-
÷=(_:_:)Default implementationModifies 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
lhsThe measurement to modify.
rhsThe scalar divisor.
-
dividedAccordingToEuclid(by:)Default implementationReturns the integral quotient of
selfdivided bydivisor.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
selfdivided bydivisor.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) -> ScalarParameters
divisorThe divisor.
-
mod(_:)Default implementationReturns 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) -> SelfParameters
divisorThe divisor.
-
formRemainder(mod:)Default implementationSets
selfto the Euclidean remainder ofself÷divisor.Note
This is a true mathematical modulo operation. i.e. (−5) mod 3 = 1, not −2
Nonmutating variant
mod
Default Implementation
Sets
selfto the Euclidean remainder ofself÷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
divisorThe divisor.
-
isDivisible(by:)Default implementationReturns
trueifselfis evenly divisible bydivisor.Default Implementation
Returns
trueifselfis evenly divisible bydivisor.Declaration
Swift
func isDivisible(by divisor: Self) -> Bool -
gcd(_:_:)Default implementationReturns the greatest common divisor of
aandb.Mutating variant
formGreatestCommonDivisor
Default Implementation
Returns the greatest common divisor of
aandb.Mutating variant
formGreatestCommonDivisor
Declaration
Swift
static func gcd(_ a: Self, _ b: Self) -> SelfParameters
lhsA value.
rhsAnother value.
-
formGreatestCommonDivisor(with:)Default implementationSets
selfto the greatest common divisor ofselfandother.Nonmutating variant
gcd
Default Implementation
Sets
selfto the greatest common divisor ofselfandother.Nonmutating variant
gcd
Declaration
Swift
mutating func formGreatestCommonDivisor(with other: Self)Parameters
otherAnother value.
-
lcm(_:_:)Default implementationReturns the least common multiple of
aandb.Mutating variant
formGreatestCommonDivisor
Default Implementation
Returns the least common multiple of
aandb.Mutating variant
formGreatestCommonDivisor
Declaration
Swift
static func lcm(_ a: Self, _ b: Self) -> SelfParameters
lhsA value.
rhsAnother value.
-
formLeastCommonMultiple(with:)Default implementationSets
selfto the least common multiple ofselfandother.Nonmutating variant
lcm
Default Implementation
Sets
selfto the least common multiple ofselfandother.Nonmutating variant
lcm
Declaration
Swift
mutating func formLeastCommonMultiple(with other: Self)Parameters
otherAnother value.
-
round(_:toMultipleOf:)Default implementationRounds the value to a multiple of
factorusing the specified rounding rule.Nonmutating variant
rounded
Default Implementation
Rounds the value to a multiple of
factorusing the specified rounding rule.Nonmutating variant
rounded
Declaration
Swift
mutating func round(_ rule: RoundingRule, toMultipleOf factor: Self)Parameters
ruleThe rounding rule follow.
factorThe factor to round to a multiple of.
-
rounded(_:toMultipleOf:)Default implementationReturns the value rounded to a multiple of
factorusing the specified rounding rule.Mutating variant
round
Default Implementation
Returns the value rounded to a multiple of
factorusing the specified rounding rule.Mutating variant
round
Declaration
Swift
func rounded(_ rule: RoundingRule, toMultipleOf factor: Self) -> SelfParameters
ruleThe rounding rule follow.
factorThe factor to round to a multiple of.
-
init(randomInRange:)Default implementationCreates a random value within a particular range.
Precondition
rangeis not empty.Default Implementation
Creates a random value within a particular range.
Precondition
rangeis not empty.Creates a random value within a particular range.
Precondition
rangeis not empty.Declaration
Swift
init(randomInRange range: Range<Self>)Parameters
rangeThe allowed range for the random value.
-
Creates a random value within a particular range.
Precondition
rangeis not empty.Declaration
Swift
init(randomInRange range: ClosedRange<Self>)Parameters
rangeThe allowed range for the random value.
-
init(randomInRange:fromRandomizer:)Default implementationCreates a random value within a particular range using the specified randomizer.
Precondition
rangeis not empty.Default Implementation
Creates a random value within a particular range using the specified randomizer.
Precondition
rangeis not empty.Creates a random value within a particular range using the specified randomizer.
Precondition
rangeis not empty.Declaration
Swift
init(randomInRange range: Range<Self>, fromRandomizer randomizer: Randomizer)Parameters
rangeThe allowed range for the random value.
randomizerThe randomizer to use to generate the random value.
-
Creates a random value within a particular range using the specified randomizer.
Precondition
rangeis not empty.Declaration
Swift
init(randomInRange range: ClosedRange<Self>, fromRandomizer randomizer: Randomizer)Parameters
rangeThe allowed range for the random value.
randomizerThe randomizer to use to generate the random value.
View on GitHub
Measurement Protocol Reference