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.
- This package requires Julia 1.0. Older versions will not be supported.
] add 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:
inchis used instead of
inconflicts with Julia syntax
minuteis used instead of
minis a commonly used function
hris used instead of
his revered as the Planck constant
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
- Explicitly import from the
Unitfulpackage to bring specific symbols into the calling namespace.
using Unitful.DefaultSymbolswill bring the following symbols into the calling namespace:
𝐋,𝐌,𝐓,𝐈,𝚯,𝐉,𝐍for length, mass, time, current, temperature, luminosity, and amount, respectively.
- Base and derived SI units, with SI prefixes (except for
cd, which conflicts with
If you have been using the SIUnits.jl package, this is not unlike typing
using SIUnits.ShortUnits with that package.
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 s
One useful interactive function is being able to convert to preferred (in this case SI) units.
julia> upreferred(F/m) A^2 s^4 kg^-1 m^-3
°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
°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.
test/runtests.jl for more usage examples.