-
Notifications
You must be signed in to change notification settings - Fork 1
Function
A function is identified by its unique name. Every function may have any number of non-negative inputs and a numeric output. The inputs and outputs may be either a float
type, an int
type or a number
type (which is int|float
).
A function may be registered for a given parser statically (before expressions are parsed). This can be done by invoking FunctionRegistry::register()
.
$registry = $parser->getFunctionRegistry();
$registry->register("intp1", static fn(int $x) : int => $x + 1);
$registry->register("num2", static fn(float $x) : float => $x * 2);
$registry->register("dist2d", static fn(float $x1, float $y1, float $x2, float $y2) : float => sqrt($x1 * $y1 + $x2 * $y2));
$parser->parse("int(2)")->evaluate(); // 3
$parser->parse("num2(num2(2))")->evaluate(); // 8
$parser->parse("dist2d(2, 3, 4, 5)")->evaluate(); // 5.0990195135927845
Functions accept any number of numeric inputs (including a varying number of inputs (variadic)), and return a numeric output. Below are some examples of valid function signatures:
fn() : int
fn() : float
fn() : int|float
fn(int $x, int|float $y) : int
fn(int ...$values) : float
fn(int $x, int $y, float ...$values) : float
A function may be registered as deterministic so that it is expected to return the same result for the same set of inputs (the meaning of deterministic here is the same as it's meaning in the context of SQL). To register a deterministic function, the deterministic
parameter must be set to true
at the time of registration.
$registry->register("sum", static fn(int|float $x, int|float $y) : int|float => $x + $y, true /* register this function as deterministic */);
Function calls to deterministic functions having deterministic inputs are computed during the parsing stage rather than the evaluation stage. Some of the example instances of this optimization are listed below:
- The expression
max(sum(2, 3), sum(4, 5))
is pre-computed as9
. The functionsmax()
andsum()
are invoked only during the parsing stage and no function call occurs during evaluation. - The expression
max(sum(x, 3), sum(4, 5))
is pre-computed asmax(sum(x, 3), 9)
. The function callsum(4, 5)
is resolved during the parsing stage, while the other function calls are resolved during evaluation. - No optimization transformation occurs on the expression
max(sum(x, y), sum(z, w))
.
A function call is made by mentioning the function name followed by an open and a close curve bracket containing the function arguments. Function arguments must be separated by a comma, such as fn()
, fn(x)
, fn(x, y)
, etc. If no argument is supplied following a comma, the function's default value for the parameter is used.
For example, for the given function fn(int $x = 1, int $y = 2) : int => $x + $y
:
-
fn()
resolves to3
. -
fn(2)
resolves to4
. -
fn(2, 3)
resolves to5
. -
fn(2, )
resolves to4
. -
fn(,)
resolves to3
. -
fn(, 1)
resolves to2
.
For the given function fn(int $x = 1, int $y) : int => $x + $y
:
-
fn()
throws aParseException
as the parameter$y
does not have a default value. -
fn(1)
throws aParseException
as the parameter$y
does not have a default value. -
fn(1,)
throws aParseException
as the parameter$y
does not have a default value. -
fn(, 1)
resolves to2
.
Below is a list of functions provided by arithmexp
out-of-the-box. These functions are directly ported from PHP's list of math functions and behave the same way.
-
abs(number num) : number
— Absolute value -
acos(float num) : float
— Arc cosine -
acosh(float num) : float
— Inverse hyperbolic cosine -
asin(float num) : float
— Arc sine -
asinh(float num) : float
— Inverse hyperbolic sine -
atan2(float y, float x) : float
— Arc tangent of two variables -
atan(float num) : float
— Arc tangent -
atanh(float num) : float
— Inverse hyperbolic tangent -
ceil(number num) : float
— Round fractions up -
cos(float num) : float
— Cosine -
cosh(float num) : float
— Hyperbolic cosine -
deg2rad(float num) : float
— Converts the number in degrees to the radian equivalent -
exp(float num) : float
— Calculates the exponent of e -
expm1(float num) : float
— Returns exp(number) - 1, computed in a way that is accurate even when the value of number is close to zero -
fdiv(float num1, float num2) : float
— Divides two numbers, according to IEEE 754 -
floor(number num) : float
— Round fractions down -
fmod(float num1, float num2) : float
— Returns the floating point remainder (modulo) of the division of the arguments -
getrandmax() : int
— Show largest possible random value -
hypot(float x, float y) : float
— Calculate the length of the hypotenuse of a right-angle triangle -
intdiv(int num1, int num2) : int
— Integer division -
lcg_value() : float
— Combined linear congruential generator -
log10(float num) : float
— Base-10 logarithm -
log1p(float num) : float
— Returns log(1 + number), computed in a way that is accurate even when the value of number is close to zero -
log(float num, float base = M_E) : float
— Natural logarithm -
max(number ...values) : number
— Find highest value -
min(number ...values) : number
— Find lowest value -
mt_getrandmax() : int
— Show largest possible random value -
mt_rand(int min, int max) : int
— Generate a random value via the Mersenne Twister Random Number Generator -
pi() : float
— Get value of pi -
pow(number num, number exponent) : number
— Exponential expression -
rad2deg(float num) : float
— Converts the radian number to the equivalent number in degrees -
rand(int min, int max) : int
— Generate a random integer -
round(number num, int precision = 0, int mode = PHP_ROUND_HALF_UP) : float
— Rounds a float -
sin(float num) : float
— Sine -
sinh(float num) : float
— Hyperbolic sine -
sqrt(float num) : float
— Square root -
tan(float num) : float
— Tangent -
tanh(float num) : float
— Hyperbolic tangent
Try out arithmexp
on the demo site!
1.1. Installation
2.1. Constant
2.2. Function
2.3. Macro
2.4. Operator
2.5. Variable
2.6. Expression Optimization
2.7. Implementation Internals