Skip to content

Commit 0e7f10e

Browse files
authored
Add custom dimensions example
1 parent 1b80c05 commit 0e7f10e

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

README.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,32 @@ true
288288

289289
## Types
290290

291-
Both a `Quantity`'s values and dimensions are of arbitrary type.
292-
By default, dimensions are stored as a `Dimensions{FixedRational{Int32,C}}`
293-
object, whose exponents are stored as rational numbers
291+
Both a `Quantity`'s values and dimensions are of arbitrary type. The default
292+
`Dimensions` (for the `u"..."` macro) performs exponent tracking for SI units,
293+
and `SymbolicDimensions` (for the `us"..."` macro) performs exponent tracking
294+
for all known unit and constant symbols, using a sparse array.
295+
296+
You can create custom spaces dimension spaces by simply creating
297+
a Julia struct subtyped to `AbstractDimensions`:
298+
299+
```julia
300+
julia> struct CookiesAndMilk{R} <: AbstractDimensions{R}
301+
cookies::R
302+
milk::R
303+
end
304+
305+
julia> cookie_rate = Quantity(0.9, CookiesAndMilk(cookies=1, milk=-1))
306+
0.9 cookies milk⁻¹
307+
308+
julia> total_milk = Quantity(103, CookiesAndMilk(milk=1))
309+
103 milk
310+
311+
julia> total_cookies = cookie_rate * total_milk
312+
92.7 cookies
313+
```
314+
315+
Exponents are tracked with the `FixedRational{Int32,C}}`
316+
object, which represents rational numbers
294317
with a fixed denominator `C`. This is much faster than `Rational`.
295318

296319
```julia

0 commit comments

Comments
 (0)