You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Add/Subtract support dimensionally equivalent units
More specifically, addition and subtraction no longer require that units match exactly. Instead, the units must only be dimensionally equivalent, and the left-hand unit is preserved as the unit of the output measurement.
Copy file name to clipboardExpand all lines: README.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ This package has no other dependencies.
23
23
24
24
## Usage
25
25
26
-
Users should interact primarily with the `Measurement` struct. Here are a few usage examples:
26
+
Users should interact primarily with the `Measurement` struct. Here are a examples of arithmetic:
27
27
28
28
```swift
29
29
let drivingSpeed =60.measured(in: .mile/ .hour)
@@ -34,6 +34,8 @@ let drivingDistance = drivingSpeed * drivingTime
34
34
print(drivingDistance.convert(to: .mile)) // Prints 30 mi
35
35
```
36
36
37
+
Note that a measurement may be multiplied or divided by another measurement with any unit, resulting in a measurement that has a new-dimensioned unit (5 meters / 10 seconds ✅). However, addition and subtraction requires that both measurements have the same dimensionality (5 meters - 10 seconds ❌), otherwise a runtime error is thrown. If adding or subtracting two measurements with different units but the same dimensionality, the result retains the first measurement's unit (5 meters - 5 millimeters = 4.995 meters).
38
+
37
39
The type names in this package align closely with the unit system provided by `Foundation`. This was intentional to provide a
38
40
familiar nomenclature for Swift developers. The APIs have been designed to avoid namespace ambiguity in files where both `Units`
39
41
and `Foundation` are imported as much as possible. However, if an issue arises, just qualify the desired package like so:
0 commit comments