UCUM, the Unified Code for Units of Measure, for Elixir: read,
check, convert and name unit codes such as kg/m2, mm[Hg], 10*3/uL and
{request}/s, as the standard defines them.
iex> Ucum.convert!(1500, "ms", "s")
1.5
iex> Ucum.conversion("Cel", "[degF]")
{:ok, {{9, 5}, {32, 1}}}
iex> Ucum.convert(7, "[pH]", "mol/l")
{:ok, 1.0e-7}
iex> Ucum.parse("sec")
{:error, {:unknown_atom, "sec"}}
iex> Ucum.name("km/h")
{:ok, "(kilometer) / (hour)"}
iex> Ucum.case_sensitive("MM[HG]")
{:ok, "mm[Hg]"}Installation
def deps do
[{:ucum, "~> 0.7"}]
endOn Hex, documented on HexDocs, with its source on GitHub. It needs Elixir 1.18 or later.
What it does
- Reads all of UCUM's grammar (spec/01): products
., quotients/(left to right), a leading/, parentheses, signed integer exponents, integer factors, the10*/10^atoms and{annotations}. A string that is not a code is refused with a named reason, never guessed at. - Reads all of UCUM's table: the essence table itself (
ucum-essence.xml, version 2.2), which the library is compiled from. All 24 prefixes and all 312 units, each definition worked down to the seven base units exactly. - Answers what UCUM defines for a code (spec/02): its
dimension/1, its exactfactor/1(a rational, π kept as itself), itskind/1, its displaysymbol/2, itsname/1and itscanonical/1form in base units. - Converts:
conversion/2answers the exact scale and offset between two codes,convert/2applies it to a value andconvert/3converts between codes. The temperatures convert exactly; the other special units (dB,Np,[pH],dB[SPL],[p'diop], …) through UCUM's functions. An arbitrary unit ([IU],[arb'U]) converts to itself and nothing else. A conversion by a scale and an offset is the nearest float to the exact answer, and an answer past a float's range is refused, never raised. - Multiplies and divides quantities:
multiply/2anddivide/2, into base units. - Reads the case-insensitive codes:
case_sensitive/1answers the case-sensitive code for one (PALisPa,HRish). Every other function takes the case-sensitive form. - Shows its table:
atom/1,prefix/1,atoms/0,prefixes/0.
Every function answers {:ok, value} or {:error, reason}
(spec/03); its ! twin answers the value or raises Ucum.Error.
The library has no runtime dependency, starts no process and reads no file.
Conformance
The library is measured by the UCUM functional tests (spec/04),
the UcumFunctionalTests.xml of Ucum-java. It passes
572 of 573 cases: every validation, conversion, multiplication and division case, and
8 of the 9 display-name cases. The ninth names the empty string, which the same file and
UCUM's grammar call no code; spec/04 §3 argues it. mix conformance prints the count.
Specification
spec/ is the full specification: what the library reads, what it answers, how it fails, and how its conformance is measured.
Attribution
UCUM is maintained by the Regenstrief Institute, and its essence table is included here unmodified. This library is an independent implementation, not affiliated with or endorsed by Regenstrief.