Skip to main content

Overview

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

Installation

pip install "praisonai[tools]"
No API key required!

Quick Start

from praisonai_tools import CalculatorTool

# Initialize
calc = CalculatorTool()

# Calculate
result = calc.calculate("2 + 2 * 3")
print(result)  # 8

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")  # 27

Scientific 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.0

Function-Based Usage

from praisonai_tools import calculate

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

CLI Usage

# 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

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