# `Macaw.Units.Quantity`
[🔗](https://github.com/dcoai/macaw_units/blob/v0.1.0/lib/macaw/units/quantity.ex#L1)

A fixed list of quantities, each with its facts: a base unit, a kind, bounds and
additivity (spec/01 §6–§8).

A quantity is what a unit measures. A unit (a UCUM code, `Macaw.Units.Unit`) and a
quantity are **facts about the data, not knobs**: neither changes a value, they say what
the value already is, so a display can choose how to show it.

The list is fixed and grows by proposal: `duration`, `information`, `data_rate`,
`frequency`, `ratio`, `count`, `temperature`, `length`, `speed`, `power`, `energy`,
`pressure`, `area`, `volume`, `mass`, `pace`, `angle`, `currency`,
`electric_potential`, `electric_current`, `electric_charge`, `illuminance`,
`sound_level`, `mass_concentration` and `amount_concentration`. spec/01 §6 tables each
one's facts.

- **Kind** is `:linear`, `:affine` (a temperature), `:logarithmic` (a sound level) or
  `:cyclic` (an angle, whose mean is circular).
- **Bounds** are a display hint, never validation: a difference is legitimately
  negative, so a value outside them is never refused.
- **Additivity** depends on the aggregation: `additivity/2` refines the quantity's
  defaults across series and over time by the field's metric kind (`:gauge`,
  `:counter`, `:rate`).

Information is **decimal by default**: UCUM's `kBy` is 1,000 bytes, and a unit written
with a binary prefix (`MiBy`) or in `bit` is the data's own choice; no setting makes it.

How a quantity is shown (its display families, unit systems and label) is presentation,
macaw_format's, over these facts.

## Examples

    iex> Macaw.Units.Quantity.additivity!(:data_rate)
    %{across: true, over_time: false}

    iex> Macaw.Units.Quantity.of_unit!("kW.h")
    :energy

# `kind`

```elixir
@type kind() :: :linear | :affine | :logarithmic | :cyclic
```

How a quantity's values combine (spec/01 §7).

# `measure`

```elixir
@type measure() :: %{
  optional(:unit) =&gt; Macaw.Units.Unit.code() | nil,
  optional(:quantity) =&gt; t() | nil
}
```

A unit and a quantity, either or both, as `resolve/1` takes them.

# `metric_kind`

```elixir
@type metric_kind() :: :gauge | :counter | :rate
```

How a field's values were measured, which refines its quantity's additivity.

# `reason`

```elixir
@type reason() ::
  {:unit, Macaw.Units.Unit.code(), Macaw.Units.Unit.reason()}
  | {:quantity, t(), Macaw.Units.Unit.code()}
  | {:unknown_quantity, term()}
  | {:unknown_metric_kind, term()}
```

Why a quantity has no fact, or a unit and a quantity do not resolve.

# `t`

```elixir
@type t() ::
  :duration
  | :information
  | :data_rate
  | :frequency
  | :ratio
  | :count
  | :temperature
  | :length
  | :speed
  | :power
  | :energy
  | :pressure
  | :area
  | :volume
  | :mass
  | :pace
  | :angle
  | :currency
  | :electric_potential
  | :electric_current
  | :electric_charge
  | :illuminance
  | :sound_level
  | :mass_concentration
  | :amount_concentration
```

One of the fixed list of quantities.

# `additivity`

```elixir
@spec additivity(t() | nil, metric_kind() | nil) ::
  {:ok, %{across: boolean(), over_time: boolean()}} | {:error, reason()}
```

Whether a sum of the quantity's values means anything (spec/01 §8, D-155):
`%{across: boolean, over_time: boolean}` — summing several series at one instant, and
summing one series over time. The quantity's physical defaults are refined by the
field's metric kind: a `:gauge` and a `:rate` never add over time, and a `:counter`'s
increments add over time when the quantity adds across. No quantity (a plain number)
defaults to both.

## Examples

    iex> Macaw.Units.Quantity.additivity(:count, :gauge)
    {:ok, %{across: true, over_time: false}}

    iex> Macaw.Units.Quantity.additivity!(:temperature, :counter)
    %{across: false, over_time: false}

`{:error, {:unknown_quantity, quantity}}` for a quantity not in `all/0`, and
`{:error, {:unknown_metric_kind, kind}}` for a metric kind not `:gauge`, `:counter` or
`:rate`; `additivity!/2` answers the map or raises.

# `additivity!`

```elixir
@spec additivity!(t() | nil, metric_kind() | nil) :: %{
  across: boolean(),
  over_time: boolean()
}
```

As `additivity/2`, the map alone, raising `Macaw.Units.Error` naming the reason.

# `all`

```elixir
@spec all() :: [t(), ...]
```

The fixed list of quantities (spec/01 §6).

# `base_unit`

```elixir
@spec base_unit(t()) :: {:ok, Macaw.Units.Unit.code() | nil} | {:error, reason()}
```

A quantity's base unit, the unit its values are in when no unit is given; `nil` for a
`:currency`, whose code the data must say. `{:ok, unit}`, or `{:error,
{:unknown_quantity, quantity}}` for a quantity not in `all/0` (spec/00 §3);
`base_unit!/1` answers the unit or raises.

## Examples

    iex> Macaw.Units.Quantity.base_unit(:pressure)
    {:ok, "Pa"}

# `base_unit!`

```elixir
@spec base_unit!(t()) :: Macaw.Units.Unit.code() | nil
```

As `base_unit/1`, the unit alone, raising `Macaw.Units.Error` naming the quantity.

# `bounds`

```elixir
@spec bounds(t()) :: {:ok, {number() | nil, number() | nil}} | {:error, reason()}
```

Where a quantity's values physically lie, in its base unit, `{min | nil, max | nil}`
(spec/01 §7): a **display hint** an axis may anchor at, never validation — a value
outside them is never refused.

## Examples

    iex> Macaw.Units.Quantity.bounds(:duration)
    {:ok, {0, nil}}

`{:error, {:unknown_quantity, quantity}}` for a quantity not in `all/0`; `bounds!/1`
answers the bounds or raises.

# `bounds`

```elixir
@spec bounds(t(), Macaw.Units.Unit.code()) ::
  {:ok, {number() | nil, number() | nil}} | {:error, term()}
```

A quantity's bounds in a unit of it: the base's, converted, as `{:ok, bounds}`. A unit
without a factor (`Cel`) takes the base's bounds as they are. A unit that is no code, or
not of the quantity's dimension, is `Macaw.Units.Unit.convert/3`'s `{:error, reason}`
(spec/00 §3); `bounds!/2` answers the bounds or raises.

## Examples

    iex> Macaw.Units.Quantity.bounds(:ratio, "%")
    {:ok, {0.0, 100.0}}

    iex> Macaw.Units.Quantity.bounds(:ratio, "s")
    {:error, {:dimension, "1", "s"}}

# `bounds!`

```elixir
@spec bounds!(t()) :: {number() | nil, number() | nil}
```

As `bounds/1`, the bounds alone, raising `Macaw.Units.Error` naming the quantity.

# `bounds!`

```elixir
@spec bounds!(t(), Macaw.Units.Unit.code()) :: {number() | nil, number() | nil}
```

As `bounds/2`, the bounds alone, raising `Macaw.Units.Error` in `Macaw.Units.Unit.format_error/1`'s words.

# `kind`

```elixir
@spec kind(t()) :: {:ok, kind()} | {:error, reason()}
```

How a quantity's values combine (spec/01 §7): `:linear`, `:affine` (a temperature),
`:logarithmic` (a sound level) or `:cyclic` (an angle).

## Examples

    iex> Macaw.Units.Quantity.kind(:angle)
    {:ok, :cyclic}

    iex> Macaw.Units.Quantity.kind(:vibes)
    {:error, {:unknown_quantity, :vibes}}

`{:error, {:unknown_quantity, quantity}}` for a quantity not in `all/0`; `kind!/1`
answers the kind or raises.

# `kind!`

```elixir
@spec kind!(t()) :: kind()
```

As `kind/1`, the kind alone, raising `Macaw.Units.Error` naming the quantity.

# `of_unit`

```elixir
@spec of_unit(Macaw.Units.Unit.code()) :: {:ok, t() | nil} | {:error, term()}
```

The quantity a code's dimension implies, or `nil` when none of the list has it
(spec/01 §6). A dimensionless code holding `%` is a `:ratio`, any other a `:count`, and
a code of one currency alone (`{iso4217:USD}`) a `:currency`. A string that is not a code
is `Macaw.Units.Unit.parse/1`'s `{:error, reason}`; `of_unit!/1` answers the quantity or
raises.

## Examples

    iex> Macaw.Units.Quantity.of_unit("kW.h")
    {:ok, :energy}

    iex> Macaw.Units.Quantity.of_unit!("{request}")
    :count

# `of_unit!`

```elixir
@spec of_unit!(Macaw.Units.Unit.code()) :: t() | nil
```

As `of_unit/1`, the quantity alone, raising `Macaw.Units.Error` in `Macaw.Units.Unit.format_error/1`'s words.

# `resolve`

```elixir
@spec resolve(measure()) ::
  {:ok, %{unit: Macaw.Units.Unit.code() | nil, quantity: t() | nil}}
  | {:error, reason()}
```

A unit and a quantity completed and checked (spec/01 §6): a unit alone implies its
quantity, a quantity alone means its base unit, and a unit whose dimension is not its
quantity's is refused. A `:ratio` or a `:count` takes any dimensionless unit, and a
`:currency` a code of one currency alone. `{:error, {:unit, code, reason}}` for a code
that does not read, `{:error, {:unknown_quantity, term}}` for a quantity not in the
list, and `{:error, {:quantity, quantity, code}}` for the mismatch.

## Examples

    iex> Macaw.Units.Quantity.resolve(%{quantity: :duration})
    {:ok, %{unit: "s", quantity: :duration}}

    iex> Macaw.Units.Quantity.resolve(%{unit: "By", quantity: :duration})
    {:error, {:quantity, :duration, "By"}}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
