View Source Measurements.Unit (Measurements v0.1.2)

Documentation for Measurements.Unit.

A unit is represented by an atom. Ref: https://en.wikipedia.org/wiki/International_System_of_Units

There exist multiple submodules defining the various units:

Internally, a unit relies on Scale and Dimension to determine:

  • which conversion is allowed or not.
  • which unit is better suited to a value.

But a user does not need to know about it, it will be managed automatically, to minimize loss of precision, and keep the Measurement value in the integer range as much as possible.

examples

Examples

iex> Measurements.Unit.time(:second)
{:ok, :second}

iex> Measurements.Unit.min(:second, :nanosecond)
{:ok, :nanosecond}

iex> {:ok, converter} = Measurements.Unit.convert(:second, :millisecond)
iex> converter.(42)
42_000

Link to this section Summary

Types

t()

Unit Type

Functions

Conversion algorithm from a unit to another.

The dimension of the unit

Normalizes a custom length unit to a known one

finds out, for two units of the same dimension, which unit is more (in scale) than the other. This means the returned unit will be the least precise

finds out, for two units of the same dimension, which unit is less (in scale) than the other. This means the returned unit will be the most precise

Returns the module where this unit is defined.

Normalizes a known unit, of any dimension

Normalizes a custom time unit to a known one.

Link to this section Types

@type t() :: atom()

Unit Type

@type value() :: integer()

Link to this section Functions

Link to this function

convert(from_unit, to_unit)

View Source
@spec convert(t(), t()) :: {:ok, (value() -> value())} | {:error, String.t()}

Conversion algorithm from a unit to another.

Will find out which dimension the unnit belongs to, and if a conversion is possible.

@spec dimension(atom()) :: {:ok, Dimension.t()} | {:error, term()}

The dimension of the unit

Link to this function

length(unit, power_ten_scale \\ 0)

View Source
@spec length(atom(), integer()) :: {:ok, t()} | {:error, (value() -> value()), t()}

Normalizes a custom length unit to a known one

@spec max(t(), t()) :: t()

finds out, for two units of the same dimension, which unit is more (in scale) than the other. This means the returned unit will be the least precise

@spec min(t(), t()) :: t()

finds out, for two units of the same dimension, which unit is less (in scale) than the other. This means the returned unit will be the most precise

@spec module(atom()) :: atom()

Returns the module where this unit is defined.

Indicates which implementation to call for normalization, conversion, etc.

@spec new(atom()) :: {:ok, t()} | {:error, (value() -> value()), t()}

Normalizes a known unit, of any dimension

@spec scale(atom()) :: {:ok, Measurements.Scale.t()} | {:error, term()}
Link to this function

time(unit, power_ten_scale \\ 0)

View Source
@spec time(atom(), integer()) :: {:ok, t()} | {:error, (value() -> value()), t()}

Normalizes a custom time unit to a known one.

@spec to_string(atom()) :: String.t()