Predicator.Functions.MathFunctions (predicator v9.0.0)

Copy Markdown View Source

Mathematical functions for Predicator expressions.

Provides SCXML-compatible Math functions for numerical computations.

Available Functions

  • Math.pow(base, exponent) - Raises base to the power of exponent; integer in, non-negative integer exponent, integer out
  • Math.sqrt(value) - Returns the square root of a number; integer in with an exact integer root, integer out
  • Math.abs(value) - Returns the absolute value
  • Math.floor(value) - Rounds down to the nearest integer
  • Math.ceil(value) - Rounds up to the nearest integer
  • Math.round(value) - Rounds to the nearest integer
  • Math.min(a, b) - Returns the smaller of two numbers
  • Math.max(a, b) - Returns the larger of two numbers
  • Math.random() - Returns a random float between 0 and 1

Examples

iex> {:ok, result} = Predicator.evaluate("Math.pow(2, 3)",
...>   %{}, providers: [Predicator.Functions.MathFunctions])
iex> result
8

iex> {:ok, result} = Predicator.evaluate("Math.sqrt(16)",
...>   %{}, providers: [Predicator.Functions.MathFunctions])
iex> result
4

Summary

Functions

Returns the absolute value of a number.

Rounds a number up to the nearest integer.

Rounds a number down to the nearest integer.

Returns the larger of two numbers.

Returns the smaller of two numbers.

Raises base to the power of exponent.

Returns a random float between 0 and 1.

Rounds a number to the nearest integer.

Returns the square root of a number.

Returns all math functions as a Predicator.FunctionProvider map.

Types

function_result()

@type function_result() :: {:ok, Predicator.Types.value()} | {:error, binary()}

Functions

call_abs(list, context)

Returns the absolute value of a number.

call_ceil(list, context)

Rounds a number up to the nearest integer.

call_floor(list, context)

Rounds a number down to the nearest integer.

call_max(list, context)

Returns the larger of two numbers.

call_min(list, context)

Returns the smaller of two numbers.

call_pow(list, context)

Raises base to the power of exponent.

Returns an integer when both arguments are integers and the exponent is non-negative - computed exactly, with no float rounding at large magnitudes. Returns a float otherwise (float argument, or negative exponent).

call_random(list, context)

Returns a random float between 0 and 1.

call_round(list, context)

Rounds a number to the nearest integer.

call_sqrt(list, context)

Returns the square root of a number.

Returns an integer when the argument is a non-negative integer with an exact integer square root (Math.sqrt(16) is 4). Returns a float otherwise. A negative argument is an error, not NaN.

functions()

@spec functions() :: %{
  required(Predicator.FunctionProvider.name()) =>
    Predicator.FunctionProvider.entry()
}

Returns all math functions as a Predicator.FunctionProvider map.