home / skills / bdambrosio / cognitive_workbench / calculate

calculate skill

/src/tools/calculate

This skill evaluates mathematical expressions numerically using SymPy, returning precise floating-point results for reliable LLM arithmetic.

npx playbooks add skill bdambrosio/cognitive_workbench --skill calculate

Review the files below or copy the command above to add this skill to your agents.

Files (2)
Skill.md
1.3 KB
---
name: calculate
type: python
description: "Numerically evaluate mathematical expressions using SymPy"
---

# calculate

Evaluate mathematical expressions numerically using SymPy. Returns floating-point results.

## Input

- `value`: Mathematical expression as string (required)
- `variables`: Dictionary of variable substitutions (optional, numbers only)
- `precision`: Decimal places for evaluation (optional, default: 10)

## Output

Success (`status: "success"`):
- `value`: String representation of numeric result

Failure (`status: "failed"`):
- `reason`: Error description

## Behavior

- **Operators**: `+`, `-`, `*`, `/`, `**`, `%`
- **Constants**: `pi`, `E`
- **Functions**: `sin`, `cos`, `tan`, `asin`, `acos`, `atan` (radians), `sqrt`, `log`, `ln`, `exp`, `abs`, `factorial`
- **Equations**: Expressions with `=` are solved (e.g., `x**2 - 4 = 0` → `-2.0, 2.0`)
- All trig functions expect radians

## Planning Notes

- Use for reliable arithmetic (LLM arithmetic is unreliable)
- To convert result to integer, use coercion tool afterward

## Examples

```json
{"type":"calculate","value":"2 + 3 * 4","out":"$result"}
{"type":"calculate","value":"2 * v * sin(theta * pi / 180) / g","variables":{"v":20,"theta":30,"g":9.8},"out":"$time"}
{"type":"calculate","value":"x**2 - 4 = 0","out":"$roots"}
```

Overview

This skill numerically evaluates mathematical expressions using SymPy and returns floating-point results. It supports arithmetic, common mathematical functions, constants, and basic equation solving. Use it when you need reliable numeric evaluation or to offload arithmetic from an LLM.

How this skill works

You provide an expression string, optional numeric variable substitutions, and an optional precision (decimal places). The skill parses the expression with SymPy, substitutes variables, evaluates to the requested precision, and returns a numeric result or solved roots for equations. If parsing or evaluation fails, it returns a clear error reason.

When to use it

  • Perform reliable arithmetic that an LLM might mishandle
  • Evaluate expressions with variables by supplying numeric substitutions
  • Compute trigonometric, logarithmic, exponential, or factorial results
  • Solve simple equations and return numeric roots
  • Obtain floating-point results at a specified decimal precision

Best practices

  • Supply numeric variable values as plain numbers in the variables dictionary
  • Specify precision when you need a particular number of decimal places (default is 10)
  • Remember trigonometric functions use radians; convert degrees before calling if needed
  • Treat equality expressions (with '=') as equations to solve for roots
  • For integer results, coerce the returned float to an integer in a downstream step

Example use cases

  • Basic arithmetic: "2 + 3 * 4" → 14.0
  • Physics formula evaluation with variables: "2 * v * sin(theta * pi / 180) / g" and variables {v:20, theta:30, g:9.8}
  • Solve polynomial: "x**2 - 4 = 0" → -2.0 and 2.0
  • Compute trig or log: "sin(pi/6) + log(10)"
  • Evaluate factorial and absolute values: "factorial(5) - abs(-3)"

FAQ

What input formats are supported?

Provide the expression as a string. Variables are a dictionary of numeric values. Precision is an integer for decimal places.

How are equations handled?

Expressions containing '=' are treated as equations; the skill solves for variable roots and returns numeric solutions.