numbers v2.0.3 Numbers

Allows you to perform math on any kind of data structure that follows the Numeric behaviour.

This includes plain Integer and Floats, but also many custom numeric types specified in packages (such as Ratio, Decimal, Tensor, ComplexNum).

Summary

Functions

Computes the absolute value of num

Adds two Numeric a and b together

Attempts to coerce a built-in datatype num to a struct of type numericType by calling numericType.new(num)

Divides the Numeric a by b

Computes the unary minus of num, also known as its negation

Multiplies the Numeric a with the Numeric b

Power function: computes base^exponent, where base is Numeric, and exponent has to be an integer

Subtracts the Numeric b from the Numeric a

Tries to convert num to a built-in floating-point number

Functions

abs(num)

Computes the absolute value of num.

add(a, b)

Adds two Numeric a and b together.

coerce(numericType, num)

Attempts to coerce a built-in datatype num to a struct of type numericType by calling numericType.new(num).

This function will raise an CannotCoerceError if:

  • There is no numericType.new/1 function available.
  • The function is available but returns an ArgumentError for the passed built-in data type.
div(a, b)

Divides the Numeric a by b.

Note that this is a supposed to be a full (non-truncated) division; no rounding or truncation is supposed to happen, even when calculating with integers.

minus(num)

Computes the unary minus of num, also known as its negation.

mult(a, b)

Multiplies the Numeric a with the Numeric b

pow(base, exponent)

Power function: computes base^exponent, where base is Numeric, and exponent has to be an integer.

This means that it is impossible to calculate roots by using this function.

If base supports direct computation of pow, that is used. Otherwise, the Exponentiation by Squaring algorithm is used.

sub(a, b)

Subtracts the Numeric b from the Numeric a.

to_float(num)

Tries to convert num to a built-in floating-point number.

Note that precision might be lost during this conversion.

Not all numeric data types support this conversion! A CannotConvertToFloatError is raised if this conversion is unsupported by the passed data type.