Analytical Expression Syntax@
Reference
Text from on this page was taken from the Function Parser Library for C++ help page. Please visit the help page for more or in-depth details.
Numeric literals
A numeric literal is a fixed numerical value in the input function string (either a floating point value or an integer value, depending on the parser type).
An integer literal can consist solely of numerical digits (possibly with a preceding unary minus). For example, "12345
".
If the literal is preceded by the characters "0x
", it will be interpreted as a hexadecimal literal, where digits can also include the letters from 'A
' to 'F
' (in either uppercase or lowercase). For example, "0x89ABC
" (which corresponds to the value 563900).
A floating point literal (only supported by the floating point type parsers) may additionally include a decimal point followed by the decimal part of the value, such as for example "12.34
", optionally followed by a decimal exponent.
A decimal exponent consists of an 'E
' or 'e
', followed by an optional plus or minus sign, followed by decimal digits, and indicates multiplication by a power of 10. For example, "1.2e5
" (which is equivalent to the value 120000).
If a floating point literal is preceded by the characters "0x
" it will be interpreted in hexadecimal. A hexadecimal floating point literal consists of a hexadecimal value, with an optional decimal point, followed optionally by a binary exponent in base 10 (in other words, the exponent is not in hexadecimal).
A binary exponent has the same format as a decimal exponent, except that 'P
' or 'p
' is used. A binary exponent indicates multiplication by a power of 2. For example, "0xA.Bp10
" (which is equivalent to the value 10944).
With the complex versions of the library, the imaginary part of a numeric literal is written as a regular numeric literal with an 'i
' appended, for example "5i
". Note that when also specifying the real part of a complex literal, parentheses should be used to avoid precedence problems. (For example, "(2+5i) * x
" is not the same thing as "2+5i * x
". The latter would be equivalent to "2 + (5i * x)
".)
Identifier names
An identifier is the name of a function (internal or user-defined), variable, constant or unit. New identifiers can be specified with the functions described in the earlier subsections in this document.
The name of an identifier can use any alphanumeric characters, the underscore character and any UTF8-encoded unicode character, excluding those denoting whitespace. The first character of the name cannot be a numeric digit, though.
All functions, variables, constants and units must use unique names. It's not possible to add two different identifiers with the same name.
The function string syntax
The function string understood by the class is very similar (but not completely identical in all aspects) to mathematical expressions in the C/C++ languages. Arithmetic float expressions can be created from float literals, variables or functions using the following operators in this order of precedence:
() | expressions in parentheses first |
A unit | a unit multiplier (if one has been added) |
A^B | exponentiation (A raised to the power B) |
-A | unary minus |
!A | unary logical not (result is 1 if int(A) is 0, else 0) |
A*B A/B A%B | multiplication, division and modulo |
A+B A-B | addition and subtraction |
A=B A<B A<=B | comparison between A and B (result is either 0 or 1) |
A&B | result is 1 if int(A) and int(B) differ from 0, else 0.Note: Regardless of the values, both operands are always evaluated. However, if the expression is optimized, it may be changed such that only one of the operands is evaluated, according to standard shortcut logical operation semantics. |
A|B | result is 1 if int(A) or int(B) differ from 0, else 0.Note: Regardless of the values, both operands are always evaluated. However, if the expression is optimized, it may be changed such that only one of the operands is evaluated, according to standard shortcut logical operation semantics. |
Since the unary minus has higher precedence than any other operator, for example the following expression is valid: x*-y
The comparison operators use an epsilon value, so expressions which may differ in very least-significant digits should work correctly. For example, "0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1 = 1"
should always return 1, and the same comparison done with ">
" or "<
" should always return 0. (The epsilon value can be configured in the fpconfig.hh
file.) Without epsilon this comparison probably returns the wrong value.
The class supports these functions:
abs(A) | Absolute value (magnitude) of A. With real numbers, if A is negative, returns -A otherwise returns A. With complex numbers, equivalent to hypot(real(x),imag(x)) . |
acos(A) | Arc-cosine of A. Returns the angle, measured in radians, whose cosine is A. |
acosh(A) | Same as acos() but for hyperbolic cosine. |
arg(A) | Phase angle of complex number A. Equivalent to atan2(imag(x),real(x)) . |
asin(A) | Arc-sine of A. Returns the angle, measured in radians, whose sine is A. |
asinh(A) | Same as asin() but for hyperbolic sine. |
atan(A) | Arc-tangent of (A). Returns the angle, measured in radians, whose tangent is A. |
atan2(A,B) | Principal arc-tangent of A/B, using the signs of the two arguments to determine the quadrant of the result. Returns the solution to the two expressions hypot(A,B)*sin(x)=A , hypot(A,B)*cos(x)=B . The return value is in range -pi to pi, inclusive. |
atanh(A) | Same as atan() but for hyperbolic tangent. |
cbrt(A) | Cube root of A. Returns a solution to expression pow(x,3)=A. |
conj(A) | Complex conjugate of A. Equivalent to real(x) - 1i*imag(x) or polar(abs(x),-arg(x)) . |
ceil(A) | Ceiling of A. Returns the smallest integer not smaller than A. Rounds up to the next higher integer. E.g. -2.9, -2.5 and -2.1 are rounded to -2.0, and 2.9, 2.5 and 2.1 are rounded to 3.0. |
cos(A) | Cosine of A. Returns the cosine of the angle A, where A is measured in radians. |
cosh(A) | Same as cos() but for hyperbolic cosine. |
cot(A) | Cotangent of A. Equivalent to 1/tan(A) . |
csc(A) | Cosecant of A. Equivalent to 1/sin(A) . |
eval(...) | This a recursive call to the function to be evaluated. The number of parameters must be the same as the number of parameters taken by the function. Must be called inside if() to avoid infinite recursion. |
exp(A) | Exponential of A. Returns the value of e raised to the power A where e is the base of the natural logarithm, i.e. the non-repeating value approximately equal to 2.71828182846. |
exp2(A) | Base 2 exponential of A. Equivalent to pow(2,A) . |
floor(A) | Floor of A. Returns the largest integer not greater than A. Rounds down to the next lower integer. E.g. -2.9, -2.5 and -2.1 are rounded to -3.0, and 2.9, 2.5 and 2.1 are rounded to 2.0. |
hypot(A,B) | Euclidean distance function. Equivalent to sqrt(A^2+B^2) . |
if(A,B,C) | If int(A) differs from 0, the return value of this function is B, else C. Only the parameter which needs to be evaluated is evaluated, the other parameter is skipped; this makes it safe to use eval() in them. |
imag(A) | Return the imaginary part of complex number A. Equivalent to abs(A)*sin(arg(A)) . |
int(A) | Rounds A to the closest integer. Equidistant values are rounded away from zero. E.g. -2.9 and -2.5 are rounded to -3.0; -2.1 is rounded to -2.0, and 2.9 and 2.5 are rounded to 3.0; 2.1 is rounded to 2.0. |
log(A) | Natural (base e ) logarithm of A. Returns the solution to expression exp(x)=A. |
log2(A) | Base 2 logarithm of A. Equivalent to log(A)/log(2) . |
log10(A) | Base 10 logarithm of A. |
max(A,B) | If A>B, the result is A, else B. |
min(A,B) | If A<B, the result is A, else B. |
polar(A,B) | Returns a complex number from magnitude A, phase angle B (in radians). Equivalent to real(A)*(cos(real(B))+1i*sin(real(B))) . |
pow(A,B) | Exponentiation (A raised to the power B). |
real(A) | Return the real part of complex number A. Equivalent to abs(A)*cos(arg(A)) . |
sec(A) | Secant of A. Equivalent to 1/cos(A) . |
sin(A) | Sine of A. Returns the sine of the angle A, where A is measured in radians. |
sinh(A) | Same as sin() but for hyperbolic sine. |
sqrt(A) | Square root of A. Returns a solution to expression pow(x,2)=A. |
tan(A) | Tangent of A. Returns the tangent of the angle A, where A is measured in radians. |
tanh(A) | Same as tan() but for hyperbolic tangent. |
trunc(A) | Truncated value of A. Returns an integer corresponding to the value of A without its fractional part. E.g. -2.9, -2.5 and -2.1 are rounded to -2.0, and 2.9, 2.5 and 2.1 are rounded to 2.0. |