Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Calculator Tool

The CalculatorTool evaluates mathematical expressions using the meval crate. It supports arithmetic, power, trigonometric, and logarithmic functions — perfect for agents that need to perform calculations without hallucinating.

Setup

[dependencies]
synaptic = { version = "0.4", features = ["tools"] }

No API key required.

Usage

use synaptic::tools::CalculatorTool;
use synaptic::core::Tool;
use serde_json::json;

let tool = CalculatorTool;

let result = tool.call(json!({"expression": "2 + 3 * 4"})).await?;
// {"expression": "2 + 3 * 4", "result": 14.0}

let result = tool.call(json!({"expression": "sqrt(144) + log(10)"})).await?;

Supported Operations

OperationExample
Arithmetic2 + 3 * 4 - 1
Power2 ^ 10
Square rootsqrt(16)
Absolute valueabs(-42)
Trigonometrysin(3.14159), cos(0)
Logarithmlog(100) (base e), log2(8)

Notes

  • The calculator uses the meval crate for expression parsing.
  • Division by zero and other undefined operations return an error.
  • All results are returned as 64-bit floating-point values.