"""Advanced calculator operations."""
import math
from typing import Any, Dict
from ...logger import get_logger
from ..base import tool
logger = get_logger(__name__)
@tool(
name="calculator_power",
description="Calculate base raised to the power of exponent (base^exponent)",
input_schema={
"type": "object",
"properties": {
"base": {"type": "number", "description": "Base number"},
"exponent": {"type": "number", "description": "Exponent (power)"},
},
"required": ["base", "exponent"],
},
)
async def power(base: float, exponent: float) -> Dict[str, Any]:
"""
Calculate base raised to exponent.
Args:
base: Base number
exponent: Exponent
Returns:
Result dictionary with calculation details
"""
logger.info(f"calculator_power called: base={base}, exponent={exponent}")
result = math.pow(base, exponent)
logger.debug(f"calculator_power result: {result}")
return {
"result": result,
"operation": "power",
"operands": {"base": base, "exponent": exponent},
}
@tool(
name="calculator_sqrt",
description="Calculate the square root of a number",
input_schema={
"type": "object",
"properties": {
"x": {
"type": "number",
"description": "Number to calculate square root of (must be non-negative)",
}
},
"required": ["x"],
},
)
async def sqrt(x: float) -> Dict[str, Any]:
"""
Calculate square root of x.
Args:
x: Number (must be >= 0)
Returns:
Result dictionary with calculation details
Raises:
ValueError: If x is negative
"""
logger.info(f"calculator_sqrt called: x={x}")
if x < 0:
logger.error(f"Square root of negative number attempted: {x}")
raise ValueError("Cannot calculate square root of negative number")
result = math.sqrt(x)
logger.debug(f"calculator_sqrt result: {result}")
return {"result": result, "operation": "sqrt", "operands": {"x": x}}
@tool(
name="calculator_log",
description="Calculate logarithm of x with specified base (default: natural log)",
input_schema={
"type": "object",
"properties": {
"x": {
"type": "number",
"description": "Number to calculate logarithm of (must be positive)",
},
"base": {
"type": "number",
"description": "Logarithm base (default: e for natural log)",
"default": math.e,
},
},
"required": ["x"],
},
)
async def log(x: float, base: float = math.e) -> Dict[str, Any]:
"""
Calculate logarithm of x with given base.
Args:
x: Number (must be > 0)
base: Logarithm base (default: e)
Returns:
Result dictionary with calculation details
Raises:
ValueError: If x <= 0 or base <= 0 or base == 1
"""
logger.info(f"calculator_log called: x={x}, base={base}")
if x <= 0:
logger.error(f"Logarithm of non-positive number attempted: {x}")
raise ValueError("Cannot calculate logarithm of non-positive number")
if base <= 0 or base == 1:
logger.error(f"Invalid logarithm base: {base}")
raise ValueError("Logarithm base must be positive and not equal to 1")
if base == math.e:
result = math.log(x)
log_type = "natural"
else:
result = math.log(x, base)
log_type = f"base-{base}"
logger.debug(f"calculator_log result: {result}")
return {
"result": result,
"operation": "log",
"operands": {"x": x, "base": base},
"log_type": log_type,
}
@tool(
name="calculator_sin",
description="Calculate sine of an angle",
input_schema={
"type": "object",
"properties": {
"x": {"type": "number", "description": "Angle value"},
"unit": {
"type": "string",
"description": "Unit of angle: 'radians' or 'degrees'",
"enum": ["radians", "degrees"],
"default": "radians",
},
},
"required": ["x"],
},
)
async def sin(x: float, unit: str = "radians") -> Dict[str, Any]:
"""
Calculate sine of angle x.
Args:
x: Angle value
unit: 'radians' or 'degrees'
Returns:
Result dictionary with calculation details
"""
logger.info(f"calculator_sin called: x={x}, unit={unit}")
if unit == "degrees":
x_rad = math.radians(x)
else:
x_rad = x
result = math.sin(x_rad)
logger.debug(f"calculator_sin result: {result}")
return {"result": result, "operation": "sin", "operands": {"x": x, "unit": unit}}
@tool(
name="calculator_cos",
description="Calculate cosine of an angle",
input_schema={
"type": "object",
"properties": {
"x": {"type": "number", "description": "Angle value"},
"unit": {
"type": "string",
"description": "Unit of angle: 'radians' or 'degrees'",
"enum": ["radians", "degrees"],
"default": "radians",
},
},
"required": ["x"],
},
)
async def cos(x: float, unit: str = "radians") -> Dict[str, Any]:
"""
Calculate cosine of angle x.
Args:
x: Angle value
unit: 'radians' or 'degrees'
Returns:
Result dictionary with calculation details
"""
logger.info(f"calculator_cos called: x={x}, unit={unit}")
if unit == "degrees":
x_rad = math.radians(x)
else:
x_rad = x
result = math.cos(x_rad)
logger.debug(f"calculator_cos result: {result}")
return {"result": result, "operation": "cos", "operands": {"x": x, "unit": unit}}
@tool(
name="calculator_tan",
description="Calculate tangent of an angle",
input_schema={
"type": "object",
"properties": {
"x": {"type": "number", "description": "Angle value"},
"unit": {
"type": "string",
"description": "Unit of angle: 'radians' or 'degrees'",
"enum": ["radians", "degrees"],
"default": "radians",
},
},
"required": ["x"],
},
)
async def tan(x: float, unit: str = "radians") -> Dict[str, Any]:
"""
Calculate tangent of angle x.
Args:
x: Angle value
unit: 'radians' or 'degrees'
Returns:
Result dictionary with calculation details
"""
logger.info(f"calculator_tan called: x={x}, unit={unit}")
if unit == "degrees":
x_rad = math.radians(x)
else:
x_rad = x
result = math.tan(x_rad)
logger.debug(f"calculator_tan result: {result}")
return {"result": result, "operation": "tan", "operands": {"x": x, "unit": unit}}