Unitful.jl
A Julia package for physical units. Available here. Inspired by:
We want to support not only SI units but also any other unit system. We also want to minimize or in some cases eliminate the run-time penalty of units. There should be facilities for dimensional analysis. All of this should integrate easily with the usual mathematical operations and collections that are defined in Julia.
Quick start
- This package requires Julia 0.7. Older versions will not be supported.
] add Unitful
using Unitful
Unitful aims for generality, but has some useful functionality out of the box.
- Base dimensions like length, mass, time, etc. are defined.
- Derived dimensions like volume, energy, momentum, etc. are defined.
- Base and derived SI units with their power-of-ten prefixes are defined.
- Some other common units are defined, without power-of-ten prefixes.
- Sensible default promotion behavior is specified.
Take a look at src/pkgdefaults.jl
for a complete list. Note that some unit abbreviations conflict with other definitions or syntax:
inch
is used instead ofin
, sincein
conflicts with Julia syntaxminute
is used instead ofmin
, sincemin
is a commonly used functionhr
is used instead ofh
, sinceh
is revered as the Planck constant
Important note on namespaces
Units, dimensions, and fundamental constants are not exported from Unitful. This is to avoid proliferating symbols in your namespace unnecessarily. You can retrieve them from Unitful in one of three ways:
- Use the
@u_str
string macro. - Explicitly import from the
Unitful
package to bring specific symbols into the calling namespace. -
using Unitful.DefaultSymbols
will bring the following symbols into the calling namespace:- Dimensions
𝐋,𝐌,𝐓,𝐈,𝚯,𝐉,𝐍
for length, mass, time, current, temperature,
luminosity, and amount, respectively.
- Base and derived SI units, with SI prefixes (except for
cd
, which conflicts withBase.cd
) °
(degrees)
- Dimensions
If you have been using the SIUnits.jl package, this is not unlike typing using SIUnits.ShortUnits
with that package.
Usage examples
julia> 1u"kg" == 1000u"g" # Equivalence implies unit conversion true julia> !(1u"kg" === 1000u"g") # ...and yet we can distinguish these... true julia> 1u"kg" === 1u"kg" # ...and these are indistinguishable. true
In the next examples we assume we have brought some units into our namespace, e.g. const m = u"m"
, etc.
julia> uconvert(°C, 212°F) 100//1 °C julia> uconvert(μm/(m*Ra), 9μm/(m*K)) 5//1 μm m^-1 Ra^-1 julia> mod(1hr+3minute+5s, 24s) 17//1 s
Note
Quantities in °C
or °F
always unit-convert under an affine transformation that takes their relative scales into account. To avoid ambiguities that can lead to incorrect results, the units °C
and °F
cannot be used in Unitful to represent temperature differences. Fortunately, 1°C - 0°C == 1K
and 1°F - 0°F == 1Ra
, so the absolute temperature scales Kelvin (K
) and Rankine (Ra
) can be used easily to represent temperature differences.
See test/runtests.jl
for more usage examples.