View Source Quantity (Quantity v1.0.0)

A data structure that encapsulates a decimal value with a unit.

Link to this section Summary

Functions

Extracts the base value from the quantity

Compares two quantities with the same unit numerically

Converts the quantity to have a new unit. The new unit must be a whole 10-exponent more or less than the original unit.

Encodes a decimal as string. Uses either :raw (E-notation) or :normal based on exponent, so that precision is not lost

Returns true if the two quantities are numerically equal

Extracts the exponent from the quantity

Test whether a Quantity is negative

Builds a new Quantity from a Decimal and a unit

Builds a new Quantity from a base value, exponent and unit

Same as parse/1, but raises if it could not parse

Parses a string representation of a quantity (perhaps generated with to_string/1)

Test whether a Quantity is positive

Reduces the value to the largest possible exponent without altering the numerical value

Converts a 1-unit quantity to a decimal. If the quantity does not represent a decimal (a unit other than 1) it fails.

Encodes the quantity as a string. The result is parsable with parse/1 If the exponent is positive, encode usinge the "raw" format to preserve precision

Return a quantity with a zero value and the same unit and precision as another Quantity

Tries to create a new Quantity. If it fails because of infinity or NaN decimal, will return an error tuple. This could be used instead of new/2 when creating a Quantity from user input or other thirdparty input.

Extracts the unit from the quantity

Tests if a quantity has zero value

Link to this section Types

@type base_unit() :: String.t() | 1
@type t() :: %Quantity{unit: unit(), value: Decimal.t()}
@type unit() :: base_unit() | {:div | :mult, unit(), unit()}

Link to this section Functions

See Quantity.Math.abs/1.

Link to this function

add!(quantity_1, quantity_2)

View Source

See Quantity.Math.add!/2.

Link to this function

add(quantity_1, quantity_2)

View Source

See Quantity.Math.add/2.

@spec base_value(t()) :: integer()

Extracts the base value from the quantity

@spec compare(t(), t()) :: :lt | :eq | :gt

Compares two quantities with the same unit numerically

iex> Quantity.compare(~Q[1.00 m], ~Q[2.00 m]) :lt iex> Quantity.compare(~Q[1.00 m], ~Q[1 m]) :eq iex> Quantity.compare(~Q[3.00 m], ~Q[2.9999999 m]) :gt

Link to this function

convert_unit(quantity, new_unit, exponent)

View Source
@spec convert_unit(t(), String.t(), integer()) :: t()

Converts the quantity to have a new unit. The new unit must be a whole 10-exponent more or less than the original unit.

The exponent given is the difference in exponents (new-exponent - old-exponent). For example when converting from kWh to MWh: 6 (MWh) - 3 (kWh) = 3

iex> ~Q[1234E3 Wh] |> Quantity.convert_unit("MWh", 6) ~Q[1.234 MWh]

iex> ~Q[25.2 m] |> Quantity.convert_unit("mm", -3) ~Q[252E2 mm]

Link to this function

decimal_to_string(decimal)

View Source
@spec decimal_to_string(Decimal.t()) :: String.t()

Encodes a decimal as string. Uses either :raw (E-notation) or :normal based on exponent, so that precision is not lost

iex> Quantity.decimal_to_string(~d[1.234]) "1.234"

iex> Quantity.decimal_to_string(~d[1E3]) "1E3"

See Quantity.Math.div/2.

@spec equals?(t(), t()) :: boolean()

Returns true if the two quantities are numerically equal

iex> Quantity.equals?(~Q[5 bananas], ~Q[5.0 bananas]) true

iex> Quantity.equals?(~Q[5 bananas], ~Q[5 apples]) false

@spec exponent(t()) :: integer()

Extracts the exponent from the quantity

See Quantity.Math.inverse/1.

Link to this function

mult(quantity, quantity_or_scalar)

View Source

See Quantity.Math.mult/2.

@spec negative?(t()) :: boolean()

Test whether a Quantity is negative

iex> ~Q[100.00 DKK] |> Quantity.negative?() false

iex> ~Q[0.00 DKK] |> Quantity.negative?() false

iex> ~Q[-1.93 DKK] |> Quantity.negative?() true

@spec new(Decimal.t(), unit()) :: t()

Builds a new Quantity from a Decimal and a unit

Link to this function

new(base_value, exponent, unit)

View Source
@spec new(integer(), integer(), unit()) :: t()

Builds a new Quantity from a base value, exponent and unit

@spec parse!(String.t()) :: t()

Same as parse/1, but raises if it could not parse

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

Parses a string representation of a quantity (perhaps generated with to_string/1)

iex> Quantity.parse("99.0 red_balloons")

iex> Quantity.parse("15 bananas/monkey") {:ok, Quantity.new(~d[15], {:div, "bananas", "monkey"})}

iex> Quantity.parse("15 m*m") {:ok, Quantity.new(~d[15], {:mult, "m", "m"})}

iex> Quantity.parse("bogus") :error

@spec positive?(t()) :: boolean()

Test whether a Quantity is positive

iex> ~Q[100.00 DKK] |> Quantity.positive?() true

iex> ~Q[0.00 DKK] |> Quantity.positive?() false

iex> ~Q[-1.93 DKK] |> Quantity.positive?() false

@spec reduce(t()) :: t()

Reduces the value to the largest possible exponent without altering the numerical value

iex> Quantity.reduce(~Q[1.200 m]) ~Q[1.2 m]

Link to this function

round(quantity, decimals)

View Source

See Quantity.Math.round/2.

Link to this function

sub!(quantity_1, quantity_2)

View Source

See Quantity.Math.sub!/2.

Link to this function

sub(quantity_1, quantity_2)

View Source

See Quantity.Math.sub/2.

See Quantity.Math.sum!/1.

Link to this function

sum!(quantities, exp, unit)

View Source

See Quantity.Math.sum!/3.

See Quantity.Math.sum/1.

Link to this function

sum(quantities, exp, unit)

View Source

See Quantity.Math.sum/3.

@spec to_decimal!(t()) :: Decimal.t()

Converts a 1-unit quantity to a decimal. If the quantity does not represent a decimal (a unit other than 1) it fails.

iex> Quantity.to_decimal!(~Q[42]) ~d[42]

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

Encodes the quantity as a string. The result is parsable with parse/1 If the exponent is positive, encode usinge the "raw" format to preserve precision

iex> Quantity.new(42, -1, "db") |> Quantity.to_string() "4.2 db" iex> Quantity.new(42, 1, "db") |> Quantity.to_string() "42E1 db" iex> Quantity.new(~d[3600], {:div, "seconds", "hour"}) |> Quantity.to_string() "3600 seconds/hour" iex> Quantity.new(~d[34], {:mult, "m", "m"}) |> Quantity.to_string() "34 m*m"

@spec to_zero(t()) :: t()

Return a quantity with a zero value and the same unit and precision as another Quantity

iex> ~Q[123.99 EUR] |> Quantity.to_zero() ~Q[0.00 EUR]

iex> ~Q[1 person] |> Quantity.to_zero() ~Q[0 person]

iex> ~Q[-123 seconds] |> Quantity.to_zero() ~Q[0 seconds]

@spec try_new(Decimal.t(), unit()) :: {:ok, t()} | {:error, reason :: String.t()}

Tries to create a new Quantity. If it fails because of infinity or NaN decimal, will return an error tuple. This could be used instead of new/2 when creating a Quantity from user input or other thirdparty input.

@spec unit(t()) :: unit()

Extracts the unit from the quantity

@spec zero?(t()) :: boolean()

Tests if a quantity has zero value

iex> Quantity.zero?(~Q[0.00 m^2]) true

iex> Quantity.zero?(~Q[0E7 m^2]) true

iex> Quantity.zero?(~Q[10 m^2]) false