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.
Installation
pip install "praisonai[tools]"No API key required!
Quick Start
- Simple Usage
python from praisonai_tools import CalculatorTool # Initialize calc = CalculatorTool() # Calculate result = calc.calculate("2 + 2 * 3") print(result) # 8 - 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
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.
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") # 27Scientific Functions
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.0Function-Based Usage
from praisonai_tools import calculate
# Quick calculation without instantiating class
result = calculate("(100 + 50) * 1.1")
print(result) # 165.0CLI Usage
# Use with praisonai
praisonai --tools CalculatorTool "Calculate compound interest on $1000 at 5% for 3 years"Supported Operations
| Operation | Symbol | Example |
|---|---|---|
| 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– Trigonometricasin,acos,atan– Inverse trigonometricsqrt– Square rootlog,log10– Logarithmsexp– Exponentialabs– Absolute valueround,floor,ceil– Rounding
Error Handling
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
| Error | Cause | Solution |
|---|---|---|
Division by zero | Dividing by 0 | Check divisor |
Invalid syntax | Malformed expression | Check expression format |
Unknown function | Unsupported function | Use supported functions |
How It Works
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.