Skip to main content
Glama
Math & Statistics Calculator API for AI Agents

Math & Statistics Calculator API for AI Agents

by chunyunglo
README.md
# Math & Statistics Calculator API for AI Agents

Stop letting your LLM guess numbers. This Actor evaluates math expressions safely and computes basic statistics for a list of numbers, deterministically, in one call. It works as a normal Apify Actor (API, schedules, integrations) and also as an MCP server so AI agents can call it as a tool.

## What it does

- **Expression evaluation**: arithmetic (`+ - * / // % **`), parentheses, constants (`pi`, `e`, `tau`) and functions (`sqrt`, `sin`, `cos`, `tan`, `asin`, `acos`, `atan`, `log`, `log10`, `log2`, `exp`, `floor`, `ceil`, `abs`, `round`, `min`, `max`, `sum`, `factorial`, `gcd`, `radians`, `degrees`, `mean`, `median`, `stdev`, `variance`). Expressions are parsed into a syntax tree and only a whitelist of numeric operations is executed. Python `eval` is never used.
- **Statistics**: count, sum, mean, median, mode, min, max, range, sample variance and standard deviation, population variance and standard deviation.
- Floating point noise is rounded away (`0.1 + 0.2` gives `0.3`), configurable with `decimals`.

## Input example

```json
{
  "expressions": ["(12.5 * 4) + sqrt(144)"],
  "numbers": [2, 4, 4, 4, 5, 5, 7, 9],
  "statistics": ["mean", "median", "mode", "pstdev"],
  "decimals": 10
}
```

Provide `expressions`, `numbers`, or both. `statistics` is optional (default: all).

## Output example

```json
[
  {"type": "expression", "expression": "(12.5 * 4) + sqrt(144)", "result": 62},
  {"type": "statistics", "mean": 5, "median": 4.5, "mode": [4], "pstdev": 2}
]
```

`mode` is always a list because a data set can have several modes. `variance` and `stdev` are `null` when fewer than two numbers are given.

## Pricing

US$0.002 per run (event: *Calculation run*), regardless of how many expressions or numbers are in the run.

## Limits

- Up to 1000 expressions per run, each up to 1000 characters.
- Up to 100,000 numbers per run.
- Integer bases and exponents in `**` are capped at 10,000; `factorial` accepts 0-1000; results larger than 1e300 are rejected.
- Use `**` for powers. `^` is rejected to avoid silent XOR mistakes.
- Only numbers are supported (no strings, variables or complex numbers).
- Invalid input (unknown function, division by zero, non-numeric result) stops the run with a clear error message.
- Results are IEEE double precision, not arbitrary-precision decimals (except integer arithmetic, which is exact).

## Credits

This Actor is built on the Python standard library (`ast`, `math` and `statistics` modules), distributed under the Python Software Foundation License. It does not wrap a third-party open-source project.


---
**Hosted version:** [Apify Store](https://apify.com/chun_yung_lo/math-statistics-calculator-api) · **Docs:** [https://chunyunglo.github.io/afw-tools/math-statistics-calculator-api/](https://chunyunglo.github.io/afw-tools/math-statistics-calculator-api/)