Math (math v0.7.0) View Source

Mathematical 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 y value of a given t point on a bezier_curve

Computes the y value of a given t point on a bezier_curve

Computes the cosine of x.

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

Converts degrees to radians

e()

The mathematical constant (e).

Calculates integers gcd, s, and t for as + bt = gcd(a, b)

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.

Computes the y value of a given t point on a linear_interpolation between 2 points

Computes the point of a given t on a linear_interpolation between 2 points

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.

Computes the modular multiplicatibe inverse of a under modulo m

Computes the modular multiplicatibe inverse of a under modulo m

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

Specs

number() <~> number() :: boolean()

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

Specs

acos(x()) :: float()

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

Specs

acosh(x()) :: float()

Computes the inverse hyperbolic cosine of x.

Specs

asin(x()) :: float()

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

Specs

asinh(x()) :: float()

Computes the inverse hyperbolic sine of x.

Specs

atan(x()) :: float()

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

Specs

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.

Specs

atanh(x()) :: float()

Computes the inverse hyperbolic tangent of x.

Link to this function

bezier_curve(t, control_points)

View Source

Specs

bezier_curve(t :: number(), control_points :: maybe_improper_list()) :: tuple()

Computes the y value of a given t point on a bezier_curve

If t is not in range [0,1] returns the interpolated point outside control points bounding box

## Examples

iex> Math.bezier_curve(0.5, [{0,0}, {1,1}])
{0.5,0.5}
Link to this function

bezier_curve!(t, control_points)

View Source

Specs

bezier_curve!(t :: number(), control_points :: maybe_improper_list()) :: tuple()

Computes the y value of a given t point on a bezier_curve

Similar to bezier_curve/2, but raises an error if not on range [0,1].

## Examples

iex> Math.bezier_curve!(0.5, [{0,0}, {1,1}])
{0.5,0.5}

iex> Math.bezier_curve!(1.5, [{0,0}, {1,1}])
** (ArgumentError) t is not beetween 0 and 1

Specs

cos(x()) :: float()

Computes the cosine of x.

x is interpreted as a value in radians.

Specs

cosh(x()) :: float()

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

Specs

deg2rad(x()) :: float()

Converts degrees to radians

Examples

iex>Math.deg2rad(180)
3.141592653589793

Specs

e() :: float()

The mathematical constant (e).

Specs

egcd(integer(), integer()) :: non_neg_integer()

Calculates integers gcd, s, and t for as + bt = gcd(a, b)

Also see Math.gcd/2.

Returns a tuple: {gcd, s, t}

Examples

iex> Math.egcd(2, 4)
{2, 1, 0}
iex> Math.egcd(2, 3)
{1, -1, 1}
iex> Math.egcd(12, 8)
{4, 1, -1}
iex> Math.egcd(54, 24)
{6, 1, -2}
iex> Math.egcd(-54, 24)
{6, 1, -2}

Specs

exp(x()) :: float()

Calculates ℯ to the xth power.

Specs

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> Math.factorial(1)
1
iex> Math.factorial(5)
120
iex> Math.factorial(20)
2432902008176640000

Specs

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 Math.lcm/2

Examples

iex> Math.gcd(2, 4)
2
iex> Math.gcd(2, 3)
1
iex> Math.gcd(12, 8)
4
iex> Math.gcd(54, 24)
6
iex> Math.gcd(-54, 24)
6

Specs

isqrt(integer()) :: integer()

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

Does not accept negative numbers as input.

Examples

iex> Math.isqrt(100)
10
iex> Math.isqrt(16)
4
iex> Math.isqrt(65536)
256
iex> Math.isqrt(10)
3

Specs

k_combinations(non_neg_integer(), non_neg_integer()) :: non_neg_integer()

Calculates the k-combinations of n.

Examples

iex> Math.k_combinations(10, 2)
45
iex> Math.k_combinations(5, 5)
1
iex> Math.k_combinations(3, 4)
0

Specs

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> Math.k_permutations(10, 2)
90
iex> Math.k_permutations(5, 5)
120
iex> Math.k_permutations(3, 4)
0

Specs

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 Math.gcd/2

Examples

iex> Math.lcm(4, 6)
12
iex> Math.lcm(3, 7)
21
iex> Math.lcm(21, 6)
42
Link to this function

linear_interpolation(t, p0, p1)

View Source

Specs

linear_interpolation(t :: number(), p0 :: tuple(), p1 :: tuple()) :: tuple()

Computes the y value of a given t point on a linear_interpolation between 2 points

If t is not in range [0,1] returns the interpolated point outside control points bounding box

Examples

iex> Math.linear_interpolation(0.5, {0,0}, {1,1})
{0.5, 0.5}
Link to this function

linear_interpolation!(t, p0, p1)

View Source

Specs

linear_interpolation!(t :: number(), p0 :: tuple(), p1 :: tuple()) :: tuple()

Computes the point of a given t on a linear_interpolation between 2 points

Similar to linear_interpolation/3, but raises an error if not on range [0,1].

Examples

iex> Math.linear_interpolation!(0.5, {0,0}, {1,1})
{0.5,0.5}

iex> Math.linear_interpolation!(1.5, {0,0}, {1,1})
** (ArgumentError) t is not beetween 0 and 1

Specs

log(x()) :: float()

Calculates the natural logarithm (base ) of x.

See also Math.e/0.

Specs

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 Math.log/1, Math.log2/1 and Math.log10/1.

Examples

iex> Math.log(5, 5)
1.0
iex> Math.log(20, 2) <~> Math.log2(20)
true
iex> Math.log(20, 10) <~> Math.log10(20)
true
iex> Math.log(2, 4)
0.5
iex> Math.log(10, 4)
1.6609640474436813

Specs

log10(x()) :: float()

Calculates the common logarithm (base 10) of x.

See also Math.log/2.

Specs

log2(x()) :: float()

Calculates the binary logarithm (base 2) of x.

See also Math.log/2.

Specs

mod_inv(a :: integer(), m :: integer()) ::
  {:ok, integer()} | {:error, :not_coprime}

Computes the modular multiplicatibe inverse of a under modulo m

In other words, given integers a and m calculate a value b for ab = 1 (mod m)

Returns an {:ok, b} tuple or {:error, :not_coprime} when b cannot be calculated for the inputs.

Examples

iex> Math.mod_inv 3, 11
{:ok, 4}
iex> Math.mod_inv 10, 17
{:ok, 12}
iex> Math.mod_inv 123, 455
{:ok, 37}
iex> Math.mod_inv 123, 456
{:error, :not_coprime}

Specs

mod_inv!(a :: integer(), m :: integer()) :: integer()

Computes the modular multiplicatibe inverse of a under modulo m

Similar to mod_in/2, but returns only the value or raises an error.

Examples

iex> Math.mod_inv! 3, 11
4
iex> Math.mod_inv! 10, 17
12
iex> Math.mod_inv! 123, 455
37
iex> Math.mod_inv! 123, 456
** (ArithmeticError) Inputs are not coprime!

Specs

nth_root(x(), number()) :: float()

Calculates the non-negative nth-root of x.

Examples

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

Specs

pi() :: float()

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)

Specs

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> Math.pow(2, 4)
16
iex> Math.pow(2.0, 4)
16.0
iex> Math.pow(2, 4.0)
16.0
iex> Math.pow(5, 100)
7888609052210118054117285652827862296732064351090230047702789306640625
iex> Math.pow(5.0, 100)
7.888609052210118e69
iex> Math.pow(2, (1 / 2))
1.4142135623730951

Specs

rad2deg(x()) :: float()

Converts radians to degrees

Examples

iex>Math.rad2deg(Math.pi)
180.0

Specs

sin(x()) :: float()

Computes the sine of x.

x is interpreted as a value in radians.

Specs

sinh(x()) :: float()

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

Specs

sqrt(x()) :: float()

Calculates the non-negative square root of x.

Specs

tan(x()) :: float()

Computes the tangent of x (expressed in radians).

Specs

tanh(x()) :: float()

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

Specs

tau() :: float()

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)