arcade-mcp/toolkits/math/arcade_math/tools/statistics.py
Mateo Torres f04087b389
Math tools expanded (#293)
Migrated all interfaces to get and return strings.

Added tests and evals for all functions (except the random generation)
Math functions are now organized into different math categories

---------

Co-authored-by: Nate Barbettini <nate@arcade-ai.com>
2025-03-14 09:47:04 -03:00

31 lines
993 B
Python

from decimal import Decimal
from statistics import median as stats_median
from typing import Annotated
from arcade.sdk import tool
@tool
def avg(
numbers: Annotated[list[str], "The list of numbers as strings"],
) -> Annotated[str, "The average (mean) of the numbers in the list as a string"]:
"""
Calculate the average (mean) of a list of numbers.
Returns "0.0" if the list is empty.
"""
# Use Decimal for arbitrary precision
d_numbers = [Decimal(n) for n in numbers]
return str(sum(d_numbers) / len(d_numbers)) if d_numbers else "0.0"
@tool
def median(
numbers: Annotated[list[str], "A list of numbers as strings"],
) -> Annotated[str, "The median value of the numbers in the list as a string"]:
"""
Calculate the median of a list of numbers.
Returns "0.0" if the list is empty.
"""
# Use Decimal for arbitrary precision
d_numbers = [Decimal(n) for n in numbers]
return str(stats_median(d_numbers)) if d_numbers else "0.0"