View Source Quantity.Math (Quantity v1.0.0)

Functions for doing math with Quantities

Link to this section Summary

Functions

Return the absolute value of the Quantity

Add two Quantities, but raise an ArgumentError on error

Add two Quantities, keeping the unit

Divide a Quantity by a scalar or another Quantity

Inverse a Quantity, similar to 1/quantity

Multiply a quantity by a scalar or another quantity

Round a Quantity to match a precision using the :half_up strategy

Subtract two Quantities, but raise ArgumentError on error

Subtract two Quantities, keeping the unit

Sum a list of Quantities with identical units, raises ArgumentError on error

Sum a list of Quantities with identical units. Includes a fallback value. The exp and unit will be used to create a Quantity with value 0 if the list is empty. Raises ArgumentError on error.

Sum a list of Quantities with identical units. Errors when addition fails or when the list is empty.

Sum a list of Quantities with identical units. Includes a fallback value. The exp and unit will be used to create a Quantity with value 0 if the list is empty.

Link to this section Functions

@spec abs(Quantity.t()) :: Quantity.t()

Return the absolute value of the Quantity

iex> Quantity.abs(~Q[-100.0 L]) ~Q[100.0 L]

iex> Quantity.abs(~Q[100.0 L]) ~Q[100.0 L]

@spec add!(Quantity.t(), Quantity.t()) :: Quantity.t()

Add two Quantities, but raise an ArgumentError on error

iex> add!(~Q[50.94 kWh], ~Q[49.40 kWh]) ~Q[100.34 kWh]

@spec add(Quantity.t(), Quantity.t()) :: {:ok, Quantity.t()} | :error

Add two Quantities, keeping the unit

iex> add(~Q[1.34 MWh], ~Q[3.49 MWh])

iex> add(~Q[1.234567 days], ~Q[3.5 days])

iex> add(~Q[10 goats], ~Q[40 sheep]) :error

@spec div(Quantity.t(), Quantity.t() | Decimal.t() | integer()) :: Quantity.t()

Divide a Quantity by a scalar or another Quantity

iex> Quantity.div(~Q[15 $], ~Q[10 banana]) ~Q[1.5 $/banana]

iex> Quantity.div(~Q[15 $], ~d[7.5]) ~Q[2 $]

iex> Quantity.div(~Q[15 $], 10) ~Q[1.5 $]

iex> Quantity.div(~Q[15 $], ~Q[10 $]) ~Q[1.5]

@spec inverse(Quantity.t()) :: Quantity.t()

Inverse a Quantity, similar to 1/quantity

iex> Quantity.inverse(~Q[10 DKK/m³]) ~Q[0.1 m³/DKK]

@spec mult(Quantity.t(), Quantity.t() | Decimal.t() | integer()) :: Quantity.t()

Multiply a quantity by a scalar or another quantity

iex> Quantity.mult(~Q[15 $], ~d[4.5]) ~Q[67.5 $]

iex> Quantity.mult(~Q[15 $], 4) ~Q[60 $]

iex> Quantity.mult(~Q[15 $], ~Q[4 banana]) ~Q[60 $*banana]

iex> Quantity.mult(~Q[15 $/banana], ~Q[4 banana]) ~Q[60 $]

Link to this function

round(quantity, decimal_count)

View Source

Round a Quantity to match a precision using the :half_up strategy

iex> Quantity.round(~Q[1.49 DKK], 1) ~Q[1.5 DKK]

iex> Quantity.round(~Q[0.5 DKK], 2) ~Q[0.50 DKK]

@spec sub!(Quantity.t(), Quantity.t()) :: Quantity.t()

Subtract two Quantities, but raise ArgumentError on error

iex> sub!(~Q[99 problems], ~Q[2 problems]) ~Q[97 problems]

@spec sub(Quantity.t(), Quantity.t()) :: {:ok, Quantity.t()} | :error

Subtract two Quantities, keeping the unit

iex> sub(~Q[99 bottles of beer], ~Q[2 bottles of beer])

iex> sub(~Q[2 bananas], ~Q[1 apple]) :error

@spec sum!([Quantity.t()]) :: Quantity.t()

Sum a list of Quantities with identical units, raises ArgumentError on error

iex> sum!([~Q[123 DKK], ~Q[10 DKK], ~Q[39 DKK]]) ~Q[172 DKK]

Link to this function

sum!(quantities, exp, unit)

View Source
@spec sum!([Quantity.t()], integer(), String.t()) :: Quantity.t()

Sum a list of Quantities with identical units. Includes a fallback value. The exp and unit will be used to create a Quantity with value 0 if the list is empty. Raises ArgumentError on error.

iex> sum!([~Q[123 apples], ~Q[10 apples]], 0, "apples") ~Q[133 apples]

iex> sum!([], -2, "DKK") ~Q[0.00 DKK]

iex> sum!([~Q[1 apples], ~Q[2 apples]], -2, "pears") ~Q[3 apples]

@spec sum([Quantity.t()]) :: {:ok, Quantity.t()} | :error

Sum a list of Quantities with identical units. Errors when addition fails or when the list is empty.

iex> sum([~Q[11.11 DKK], ~Q[22.22 DKK], ~Q[33.33 DKK]])

iex> sum([~Q[1 EUR], ~Q[2 DKK]]) :error

iex> sum([]) :error

Link to this function

sum(quantities, exp, unit)

View Source
@spec sum([Quantity.t()], integer(), String.t()) :: {:ok, Quantity.t()} | :error

Sum a list of Quantities with identical units. Includes a fallback value. The exp and unit will be used to create a Quantity with value 0 if the list is empty.

iex> sum([~Q[0.11 DKK], ~Q[0.22 DKK], ~Q[0.33 DKK]], -2, "DKK")

iex> sum([], 0, "DKK")

iex> sum([], -2, "DKK")

iex> sum([~Q[1 EUR], ~Q[2 EUR]], -1, "DKK")

iex> sum([~Q[1 EUR], ~Q[2 DKK]], -2, "EUR") :error