Added operations

This commit is contained in:
cdricms
2025-08-08 10:20:07 +02:00
parent 9f08712ae7
commit caf19895f3

View File

@@ -339,6 +339,37 @@ public struct UnitValue<ValueType: ConvertibleToDouble>:
!(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
/// Allows converting the UnitValue to another Unit using subscript syntax.
/// Example: `tenKilos[.pound]`