Added operations
This commit is contained in:
@@ -339,6 +339,37 @@ public struct UnitValue<ValueType: ConvertibleToDouble>:
|
|||||||
!(lhs < rhs) && !(lhs == rhs)
|
!(lhs < rhs) && !(lhs == rhs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The result will have the `unit` of the left hand side value.
|
||||||
|
public static func - <T: Numeric>(lhs: UnitValue<T>, rhs: UnitValue<T>)
|
||||||
|
-> UnitValue<Double>
|
||||||
|
{
|
||||||
|
(lhs.value - rhs.value)[lhs.unit]
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The result will have the `unit` of the left hand side value.
|
||||||
|
public static func + <T: Numeric>(lhs: UnitValue<T>, rhs: UnitValue<T>)
|
||||||
|
-> UnitValue<Double>
|
||||||
|
{
|
||||||
|
(lhs.value + rhs.value)[lhs.unit]
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The result will have the `unit` of the left hand side value.
|
||||||
|
public static func * <T: Numeric>(lhs: UnitValue<T>, rhs: UnitValue<T>)
|
||||||
|
-> UnitValue<Double>
|
||||||
|
{
|
||||||
|
(lhs.value * rhs.value)[lhs.unit]
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The result will have the `unit` of the left hand side value.
|
||||||
|
public static func / <T: Numeric>(lhs: UnitValue<T>, rhs: UnitValue<T>)
|
||||||
|
-> UnitValue<Double>
|
||||||
|
{
|
||||||
|
guard rhs.value != 0 else {
|
||||||
|
fatalError("Cannot divide by zero.")
|
||||||
|
}
|
||||||
|
return (lhs.value * rhs.value)[lhs.unit]
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - Subscripts for UnitValue
|
// MARK: - Subscripts for UnitValue
|
||||||
/// Allows converting the UnitValue to another Unit using subscript syntax.
|
/// Allows converting the UnitValue to another Unit using subscript syntax.
|
||||||
/// Example: `tenKilos[.pound]`
|
/// Example: `tenKilos[.pound]`
|
||||||
|
|||||||
Reference in New Issue
Block a user