Elex.Units.Formula (Elex v0.3.0)

View Source

Parses unit formulas such as m * m, kg * m | s^2, and m | s^2.

Formula language is not the expression language. Multiply with *, middot (· U+00B7 or U+22C5), or whitespace (kg m is kg * m; ms is the symbol ms). At most one | (left numerator, right denominator). / and parentheses are invalid. Integer ^ binds tighter than multiplication. The exponent must be a non-zero integer literal without a leading zero, not a unit. A formula that cancels to nothing (m | m, m^0) is invalid. Trailing digits in a symbol are part of the name (s2 is not s^2). Inverse is s^-1 or 1 | s (not | s). / is invalid (m | s, not m / s). Braces are only a quantity suffix (1 {m | s}), not a formula string.

Used when registering derived-category units and when converting an evaluate result to a compound unit: target. Callers typically write formula strings and do not invoke this module directly.

Summary

Types

monomial()

@type monomial() :: %{optional(String.t()) => integer()}

Functions

parse(source)

@spec parse(String.t()) :: {:ok, monomial()} | {:error, String.t()}