Calculator

Perform mathematical calculations

Overview

Calculator tool for performing mathematical calculations including basic arithmetic, scientific functions, and expression evaluation.

The user asks a math question; the agent evaluates the expression with the calculator and returns the result.

Diagram

CalculatorTool Flow

👤 User

🤖 Agent

🔧 CalculatorTool

✅ Result

Installation

bash
pip install "praisonai[tools]"

No API key required!

Quick Start

  1. Simple Usage
    python
    from praisonai_tools import CalculatorTool
    
    # Initialize
    calc = CalculatorTool()
    
    # Calculate
    result = calc.calculate("2 + 2 * 3")
    print(result)  # 8
  2. With Configuration

    Use the same tool with an agent — see Usage with Agent below, or pass env vars and options from the sections above.

Usage with Agent

python
from praisonaiagents import Agent
from praisonai_tools import CalculatorTool

agent = Agent(
    name="MathHelper",
    instructions="You are a math assistant. Use the calculator for computations.",
    tools=[CalculatorTool()]
)

response = agent.chat("What is 15% of 250?")
print(response)

Available Methods

calculate(expression)

Evaluate a mathematical expression.

python
from praisonai_tools import CalculatorTool

calc = CalculatorTool()

# Basic arithmetic
calc.calculate("10 + 5")      # 15
calc.calculate("100 / 4")     # 25.0
calc.calculate("2 ** 10")     # 1024

# Percentages
calc.calculate("250 * 0.15")  # 37.5

# Complex expressions
calc.calculate("(10 + 5) * 2 - 3")  # 27

Scientific Functions

python
import math

calc = CalculatorTool()

# Trigonometry (radians)
calc.calculate("sin(3.14159 / 2)")  # ~1.0
calc.calculate("cos(0)")            # 1.0

# Logarithms
calc.calculate("log(100)")          # 2.0 (base 10)
calc.calculate("log(2.718)")        # ~1.0 (natural log)

# Square root
calc.calculate("sqrt(144)")         # 12.0

Function-Based Usage

python
from praisonai_tools import calculate

# Quick calculation without instantiating class
result = calculate("(100 + 50) * 1.1")
print(result)  # 165.0

CLI Usage

bash
# Use with praisonai
praisonai --tools CalculatorTool "Calculate compound interest on $1000 at 5% for 3 years"

Supported Operations

OperationSymbolExample
Addition+5 + 3
Subtraction-10 - 4
Multiplication*6 * 7
Division/20 / 4
Power**2 ** 8
Modulo%17 % 5
Parentheses()(2 + 3) * 4

Supported Functions

  • sin, cos, tan – Trigonometric
  • asin, acos, atan – Inverse trigonometric
  • sqrt – Square root
  • log, log10 – Logarithms
  • exp – Exponential
  • abs – Absolute value
  • round, floor, ceil – Rounding

Error Handling

python
from praisonai_tools import CalculatorTool

calc = CalculatorTool()
result = calc.calculate("10 / 0")

if "error" in str(result).lower():
    print("Calculation error")
else:
    print(f"Result: {result}")

Common Errors

ErrorCauseSolution
Division by zeroDividing by 0Check divisor
Invalid syntaxMalformed expressionCheck expression format
Unknown functionUnsupported functionUse supported functions

How It Works

Diagram
CalculatorToolAgentUserCalculatorToolAgentUserRequestCall toolQueryDataResultResponse

Best Practices

Prefer the calculator over the LLM for math

LLMs approximate arithmetic. Route numeric work through the calculator tool for exact answers.

Guard against division by zero

Wrap calculate in error handling so a bad expression returns a message instead of crashing the agent.

Use parentheses for clarity

Explicit parentheses make the agent’s intended order of operations unambiguous.


Related Tools