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 outMath.sqrt(value)- Returns the square root of a number; integer in with an exact integer root, integer outMath.abs(value)- Returns the absolute valueMath.floor(value)- Rounds down to the nearest integerMath.ceil(value)- Rounds up to the nearest integerMath.round(value)- Rounds to the nearest integerMath.min(a, b)- Returns the smaller of two numbersMath.max(a, b)- Returns the larger of two numbersMath.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
@type function_result() :: {:ok, Predicator.Types.value()} | {:error, binary()}
Functions
@spec call_abs([Predicator.Types.value()], Predicator.Context.t()) :: function_result()
Returns the absolute value of a number.
@spec call_ceil([Predicator.Types.value()], Predicator.Context.t()) :: function_result()
Rounds a number up to the nearest integer.
@spec call_floor([Predicator.Types.value()], Predicator.Context.t()) :: function_result()
Rounds a number down to the nearest integer.
@spec call_max([Predicator.Types.value()], Predicator.Context.t()) :: function_result()
Returns the larger of two numbers.
@spec call_min([Predicator.Types.value()], Predicator.Context.t()) :: function_result()
Returns the smaller of two numbers.
@spec call_pow([Predicator.Types.value()], Predicator.Context.t()) :: function_result()
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).
@spec call_random([Predicator.Types.value()], Predicator.Context.t()) :: function_result()
Returns a random float between 0 and 1.
@spec call_round([Predicator.Types.value()], Predicator.Context.t()) :: function_result()
Rounds a number to the nearest integer.
@spec call_sqrt([Predicator.Types.value()], Predicator.Context.t()) :: function_result()
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.
@spec functions() :: %{ required(Predicator.FunctionProvider.name()) => Predicator.FunctionProvider.entry() }
Returns all math functions as a Predicator.FunctionProvider map.