calculate
Evaluates mathematical expressions and returns the computed result. Supports basic operators, functions, and constants for safe calculations.
Instructions
Evaluates a mathematical expression and returns the result.
Supports basic operators (+, -, *, /, **, %, ^), mathematical functions (sin, cos, tan, exp, log, log10, sqrt), and constants (pi, e). Uses an AST whitelist for safe execution.
Args: expression: The mathematical expression to evaluate as a string. Examples: "2 + 2", "sin(pi/4)", "sqrt(16) * 2", "log(100, 10)"
Returns: On success: {"result": } On error: {"error": }
Examples: >>> calculate("2 * 3 + 4") {'result': 10} >>> calculate("sin(pi/2)") {'result': 1.0} >>> calculate("sqrt(16)") {'result': 4.0} >>> calculate("(2 + 3)^2") {'result': 25}
Notes: - Use 'x' as the variable (e.g., x2, not x^2) - Multiplication must be explicitly indicated with * (e.g., 2*x, not 2x) - Powers can be represented with ** or ^ (e.g., x2 or x^2)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| expression | Yes |