Skip to content

Evaluate ​

The /evaluate endpoint safely evaluates a mathematical expression and returns its result. It uses a Pratt parser — no eval() is ever called.

How it works?

Expressions are tokenized then parsed recursively using binding powers (Pratt parsing). The result is computed directly from the AST — no eval(), no Function(). Two constants are available: pi (≈ 3.14159…) and e (≈ 2.71828…). Recursion depth is capped at 100 and expression length at 500 characters.

Parameters ​

ParameterRequiredDescription
exprYesThe mathematical expression to evaluate (max 500 characters)
precisionNoNumber of decimal places in the result (0–15)

Supported Operators ​

OperatorDescription
+Addition
-Subtraction / unary negation
*Multiplication
/Division
%Modulo
^Exponentiation

Unary - binds looser than ^, matching standard math convention: -2^2 evaluates to -4 (i.e. -(2^2)), not 4.

Available Functions ​

sin, cos, tan, asin, acos, atan, sqrt, abs, floor, ceil, round, log, log2, log10

Response Fields ​

FieldTypeDescription
exprstringThe original expression
resultnumberThe evaluated result

Code Examples ​

curl -X GET \
  "https://api.sylvain.sh/v6/evaluate?expr=2*%283%2B4%29%5E2&precision=2"

Try It ​

Error Handling ​

If parameters are missing or invalid, the API will return an error:

Error MessageDescription
Please provide a math expression (?expr={expression})The expr parameter is missing
Expression is too long (max 500 characters)The expression exceeds 500 characters
Maximum expression depth exceededNesting depth exceeds 100 levels
Division by zeroThe expression attempts to divide by zero
Unknown identifier: {name}An unknown constant or function name was used
Invalid number: {num}A number literal has more than one decimal point (e.g. 1.2.3)
precision must be between 0 and 15The precision value is out of range