mathx v0.1.0 Mathx

Mathxematical functions and constants.

Link to this section Summary

Functions

Equality-test for whether x and y are nearly equal

Computes the arc cosine of x. (expressed in radians)

Computes the inverse hyperbolic cosine of x

Computes the arc sine of x. (expressed in radians)

Computes the inverse hyperbolic sine of x

Computes the arc tangent of x. (expressed in radians)

Computes the arc tangent given y and x. (expressed in radians)

Computes the inverse hyperbolic tangent of x

Computes the cosine of x

Computes the hyperbolic cosine of x (expressed in radians)

Converts degrees to radians

e()

The mathematical constant (e)

Calculates ℯ to the xth power

Calculates the factorial of n: 1 2 3 n

Calculates the Greatest Common divisor of two numbers

Calculates the non-negative integer square root of x (rounded towards zero)

Calculates the k-combinations of n

Calculates the k-permutations of n

Calculates the Least Common Multiple of two numbers

Calculates the natural logarithm (base ) of x

Calculates the base-b logarithm of x

Calculates the common logarithm (base 10) of x

Calculates the binary logarithm (base 2) of x

Calculates the non-negative nth-root of x

The mathematical constant π (pi)

Arithmetic exponentiation. Calculates x to the n -th power

Converts radians to degrees

Computes the sine of x

Computes the hyperbolic sine of x (expressed in radians)

Calculates the non-negative square root of x

Computes the tangent of x (expressed in radians)

Computes the hyperbolic tangent of x (expressed in radians)

The mathematical constant τ (tau)

Link to this section Types

Link to this section Functions

Equality-test for whether x and y are nearly equal.

This is useful when working with floating-point numbers, as these introduce small rounding errors.

Examples

iex> 2.3 - 0.3 == 2.0
false
iex> 2.3 - 0.3 <~> 2.0
true
Link to this function acos(x)
acos(x()) :: float()

Computes the arc cosine of x. (expressed in radians)

Link to this function acosh(x)
acosh(x()) :: float()

Computes the inverse hyperbolic cosine of x.

Link to this function asin(x)
asin(x()) :: float()

Computes the arc sine of x. (expressed in radians)

Link to this function asinh(x)
asinh(x()) :: float()

Computes the inverse hyperbolic sine of x.

Link to this function atan(x)
atan(x()) :: float()

Computes the arc tangent of x. (expressed in radians)

Link to this function atan2(y, x)
atan2(y(), x()) :: float()

Computes the arc tangent given y and x. (expressed in radians)

This variant returns the inverse tangent in the correct quadrant, as the signs of both x and y are known.

Link to this function atanh(x)
atanh(x()) :: float()

Computes the inverse hyperbolic tangent of x.

Link to this function cos(x)
cos(x()) :: float()

Computes the cosine of x.

x is interpreted as a value in radians.

Link to this function cosh(x)
cosh(x()) :: float()

Computes the hyperbolic cosine of x (expressed in radians).

Link to this function deg2rad(x)
deg2rad(x()) :: float()

Converts degrees to radians

Examples

iex>Mathx.deg2rad(180)
3.141592653589793

The mathematical constant (e).

Link to this function exp(x)
exp(x()) :: float()

Calculates ℯ to the xth power.

Link to this function factorial(n)
factorial(non_neg_integer()) :: pos_integer()

Calculates the factorial of n: 1 2 3 n

To make this function faster, values of n up to 1000 are precomputed at compile time.

Examples

iex> Mathx.factorial(1)
1
iex> Mathx.factorial(5)
120
iex> Mathx.factorial(20)
2432902008176640000

Calculates the Greatest Common divisor of two numbers.

This is the largest positive integer that divides both a and b without leaving a remainder.

Also see Mathx.lcm/2

Examples

iex> Mathx.gcd(2, 4)
2
iex> Mathx.gcd(2, 3)
1
iex> Mathx.gcd(12, 8)
4
iex> Mathx.gcd(54, 24)
6
iex> Mathx.gcd(-54, 24)
6
Link to this function isqrt(x)
isqrt(integer()) :: integer()

Calculates the non-negative integer square root of x (rounded towards zero)

Does not accept negative numbers as input.

Examples

iex> Mathx.isqrt(100)
10
iex> Mathx.isqrt(16)
4
iex> Mathx.isqrt(65536)
256
iex> Mathx.isqrt(10)
3
Link to this function k_combinations(n, k)
k_combinations(non_neg_integer(), non_neg_integer()) :: non_neg_integer()

Calculates the k-combinations of n.

Examples

iex> Mathx.k_combinations(10, 2)
45
iex> Mathx.k_combinations(5, 5)
1
iex> Mathx.k_combinations(3, 4)
0
Link to this function k_permutations(n, k)
k_permutations(non_neg_integer(), non_neg_integer()) :: non_neg_integer()

Calculates the k-permutations of n.

This is the number of distinct ways to create groups of size k from n distinct elements.

Notice that n is the first parameter, for easier piping.

Examples

iex> Mathx.k_permutations(10, 2)
90
iex> Mathx.k_permutations(5, 5)
120
iex> Mathx.k_permutations(3, 4)
0

Calculates the Least Common Multiple of two numbers.

This is the smallest positive integer that can be divided by both a by b without leaving a remainder.

Also see Mathx.gcd/2

Examples

iex> Mathx.lcm(4, 6)
12
iex> Mathx.lcm(3, 7)
21
iex> Mathx.lcm(21, 6)
42
Link to this function log(x)
log(x()) :: float()

Calculates the natural logarithm (base ) of x.

See also Mathx.e/0.

Link to this function log(x, x)
log(x(), number()) :: float()

Calculates the base-b logarithm of x

Note that variants for the most common logarithms exist that are faster and more precise.

See also Mathx.log/1, Mathx.log2/1 and Mathx.log10/1.

Examples

iex> Mathx.log(5, 5)
1.0
iex> Mathx.log(20, 2) <~> Mathx.log2(20)
true
iex> Mathx.log(20, 10) <~> Mathx.log10(20)
true
iex> Mathx.log(2, 4)
0.5
iex> Mathx.log(10, 4)
1.6609640474436813
Link to this function log10(x)
log10(x()) :: float()

Calculates the common logarithm (base 10) of x.

See also Mathx.log/2.

Link to this function log2(x)
log2(x()) :: float()

Calculates the binary logarithm (base 2) of x.

See also Mathx.log/2.

Link to this function nth_root(x, n)
nth_root(x(), number()) :: float()

Calculates the non-negative nth-root of x.

Examples

iex> Mathx.nth_root(27, 3)
3.0
iex> Mathx.nth_root(65536, 8)
4.0

The mathematical constant π (pi).

The ratio of a circle’s circumference to its diameter. The returned number is a floating-point approximation (as π is irrational)

Link to this function pow(x, n)
pow(number(), number()) :: number()

Arithmetic exponentiation. Calculates x to the n -th power.

When both x and n are integers and n is positive, returns an integer. When n is a negative integer, returns a float. When working with integers, the Exponentiation by Squaring algorithm is used, to allow for a fast and precise result.

When one of the numbers is a float, returns a float by using erlang’s :math.pow/2 function.

It is possible to calculate roots by choosing n between 0.0 and 1.0 (To calculate the p -th-root, pass 1/p to the function)

Examples

iex> Mathx.pow(2, 4)
16
iex> Mathx.pow(2.0, 4)
16.0
iex> Mathx.pow(2, 4.0)
16.0
iex> Mathx.pow(5, 100)
7888609052210118054117285652827862296732064351090230047702789306640625
iex> Mathx.pow(5.0, 100)
7.888609052210118e69
iex> Mathx.pow(2, (1 / 2))
1.4142135623730951
Link to this function rad2deg(x)
rad2deg(x()) :: float()

Converts radians to degrees

Examples

iex>Mathx.rad2deg(Mathx.pi)
180.0
Link to this function sin(x)
sin(x()) :: float()

Computes the sine of x.

x is interpreted as a value in radians.

Link to this function sinh(x)
sinh(x()) :: float()

Computes the hyperbolic sine of x (expressed in radians).

Link to this function sqrt(x)
sqrt(x()) :: float()

Calculates the non-negative square root of x.

Link to this function tan(x)
tan(x()) :: float()

Computes the tangent of x (expressed in radians).

Link to this function tanh(x)
tanh(x()) :: float()

Computes the hyperbolic tangent of x (expressed in radians).

The mathematical constant τ (tau).

The ratio of a circle’s circumference to its radius. Defined as 2 * π, but preferred by some mathematicians. The returned number is a floating-point approximation (as τ is irrational)