Quantity.Math (Quantity v0.6.1) View Source
Functions for doing math with Quantities
Link to this section Summary
Functions
Add two Quantities, keeping the unit
Add two Quantities, but raise an ArgumentError on error
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, keeping the unit
Subtract two Quantities, but raise 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.
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.
Link to this section Functions
Specs
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
Specs
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]
Specs
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]
Specs
inverse(Quantity.t()) :: Quantity.t()
Inverse a Quantity, similar to 1/quantity
iex> Quantity.inverse(~Q[10 DKK/m³]) ~Q[0.1 m³/DKK]
Specs
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 $]
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]
Specs
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
Specs
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]
Specs
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
Specs
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 DKK]], -2, "EUR") :error
Specs
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]
Specs
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]