Skip to content
moxon home

Evaluate

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

gethttps://api.moxon.dev/v5/evaluate

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.

Try itget/v5/evaluate

Parameters

  • exprstringrequired

    The mathematical expression to evaluate (max 500 characters)

  • precisionnumberoptional

    Number 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

  • exprstring

    The original expression

  • resultnumber

    The evaluated result

Code Examples

Error Handling

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

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