AI coding assistants like Claude Code, Cursor, and Windsurf are powerful tools, but they have a weakness: they make math mistakes. When your assistant calculates array indices, estimates file sizes, or computes time differences, it's guessing - not calculating. This guide shows you how to fix this with TinyFn MCP tools.
The Problem: Math Errors in Coding Assistants
You're pair programming with your AI assistant, and it's going great. Then you ask it to help with something that involves numbers, and things go sideways.
You: "If I have an array of 1000 items and I'm processing
them in batches of 37, how many batches will I need?"
AI: "You'll need 27 batches."
Reality: 1000 / 37 = 27.027... so you need 28 batches
(the AI forgot about the partial batch)
This isn't a rare edge case. Coding assistants regularly make errors on:
- Division and remainders
- Percentage calculations
- Time and date math
- Memory and file size conversions
- Array indexing and pagination
- Counting iterations and loop bounds
Real Examples of Calculation Errors
Here are real scenarios where coding assistants commonly fail:
Pagination Logic
You: "I have 95 items and want 10 per page.
How many pages total?"
Wrong AI Answer: "9 pages" (95 / 10 = 9.5, truncated)
Correct Answer: 10 pages (ceil(95 / 10) = 10)
This bug causes the last 5 items to be unreachable!
Time Zone Conversions
You: "If it's 3 PM PST, what time is it in London?"
Wrong AI Answer: "11 PM GMT" (8 hour difference)
Correct Answer: Could be 11 PM GMT or 12 AM GMT
depending on DST status
The AI doesn't check current DST rules!
Memory Size Calculations
You: "How much memory for 1 million 64-bit integers?"
Wrong AI Answer: "64 MB" (1M * 64 bits)
Correct Answer: 8 MB (1M * 8 bytes)
The AI confused bits and bytes!
Percentage Calculations
You: "If response time improved from 250ms to 180ms,
what's the percentage improvement?"
Wrong AI Answer: "28% improvement" (rough estimate)
Correct Answer: 28% improvement... wait, the AI got lucky.
But: "72% of original" is also correct phrasing.
The AI often gives wrong answers or wrong framing.
Why Coding Assistants Make Math Mistakes
Understanding why these errors occur helps explain why tools are the solution.
LLMs Predict, They Don't Compute
When you ask Claude Code or Cursor to calculate something, the underlying LLM doesn't perform arithmetic. Instead, it predicts what number is most likely to appear next based on its training data.
For simple, common calculations (like 2 + 2), this works because the LLM has seen the correct answer many times. For complex or unusual calculations, the prediction often fails.
Tokenization Hides Numbers
LLMs process text as tokens, not individual digits. The number "1000000" might be tokenized as ["100", "0000"] or ["1000", "000"]. This makes arithmetic operations unreliable.
Context Window Limitations
When calculations span multiple steps, the LLM might lose track of intermediate results. This is especially problematic for iterative calculations or long chains of operations.
The Solution: TinyFn MCP Tools
TinyFn MCP provides deterministic tools that perform actual calculations. When your coding assistant uses TinyFn, it delegates math to code that computes correct results every time.
How It Works
You: "How many pages for 95 items at 10 per page?"
AI Process:
1. Recognize this is a pagination question
2. Predict the answer based on training patterns
3. Output: "9 pages" (WRONG)
You: "How many pages for 95 items at 10 per page?"
AI Process:
1. Recognize this is a pagination question
2. Call math/ceil tool with input: 95 / 10 = 9.5
3. Tool returns: 10
4. Output: "10 pages" (CORRECT)
Available Math Tools
TinyFn provides comprehensive math tools for coding tasks:
math/add,math/subtract,math/multiply,math/dividemath/modulo- Remainder calculationsmath/ceil,math/floor,math/roundmath/percentage- Percentage calculationsmath/is-prime,math/factorialmath/gcd,math/lcmconvert/bytes-to-human- Memory size formattingdatetime/diff- Time difference calculationstimezone/convert- Timezone conversions with DST
Setup for Claude Code
Claude Code (the CLI tool) supports MCP servers through its configuration file.
Step 1: Get Your API Key
Sign up at tinyfn.io for a free API key.
Step 2: Configure MCP
Add TinyFn to your Claude Code MCP configuration:
{
"mcpServers": {
"tinyfn": {
"url": "https://api.tinyfn.io/mcp/all/",
"headers": {
"X-API-Key": "your-api-key"
}
}
}
}
Step 3: Restart Claude Code
Restart Claude Code to load the new configuration.
Step 4: Test
claude> If I have 1000 items in batches of 37, how many batches?
Claude will use TinyFn to calculate: ceil(1000/37) = 28 batches
Setup for Cursor
Cursor supports MCP servers through its settings interface.
Step 1: Open Cursor Settings
Go to File > Preferences > Settings (or Cmd/Ctrl + ,)
Step 2: Navigate to MCP Configuration
Search for "MCP" in settings and locate the MCP servers configuration.
Step 3: Add TinyFn
{
"mcpServers": {
"tinyfn": {
"url": "https://api.tinyfn.io/mcp/all/",
"headers": {
"X-API-Key": "your-api-key"
}
}
}
}
Step 4: Restart Cursor
Restart Cursor to apply the configuration.
Setup for Windsurf
Windsurf (by Codeium) also supports MCP tools through its configuration system.
Step 1: Access Windsurf Configuration
Open Windsurf settings and navigate to the AI/MCP section.
Step 2: Add TinyFn Server
{
"mcpServers": {
"tinyfn": {
"url": "https://api.tinyfn.io/mcp/all/",
"headers": {
"X-API-Key": "your-api-key"
}
}
}
}
Step 3: Restart Windsurf
Restart to load the new MCP server.
Practical Examples
Here's how TinyFn helps with common coding calculations:
Pagination Implementation
You: "Help me implement pagination. I have 847 items
and want 25 per page."
AI with TinyFn:
[Calls math/ceil with 847/25]
Tool returns: 34
"You'll need 34 pages total. Here's the implementation:
const totalItems = 847;
const itemsPerPage = 25;
const totalPages = 34; // Calculated precisely
function getPage(pageNumber) {
const start = (pageNumber - 1) * itemsPerPage;
const end = Math.min(start + itemsPerPage, totalItems);
return items.slice(start, end);
}"
Memory Budget Calculation
You: "I'm caching 50,000 user objects, each about 2.3 KB.
How much memory do I need?"
AI with TinyFn:
[Calls math/multiply with 50000 * 2.3]
Tool returns: 115000 KB
[Calls convert/bytes-to-human with 115000000]
Tool returns: "109.67 MB"
"You'll need approximately 110 MB of memory for the cache.
Make sure your server has at least 256 MB free to account
for overhead and growth."
Rate Limiting Calculations
You: "My API allows 1000 requests per hour. If I need to
process 15,000 items, how long will it take?"
AI with TinyFn:
[Calls math/divide with 15000 / 1000]
Tool returns: 15
"At 1000 requests per hour, processing 15,000 items will
take 15 hours. Consider:
1. Batch operations if the API supports them
2. Running overnight to avoid blocking other work
3. Implementing exponential backoff for reliability"
Date Calculations for Scheduling
You: "I need to schedule a task 30 business days from today.
What date is that?"
AI with TinyFn:
[Calls datetime/add-business-days with today + 30]
Tool returns: "2025-03-14"
"30 business days from today (January 31, 2025) is
March 14, 2025. This accounts for weekends but not
holidays. If you need to exclude holidays, you'll need
to provide a holiday list."
Best Practices
1. Always Verify Critical Calculations
Even with TinyFn, verify calculations that have significant consequences (financial, security, data integrity).
2. Use Specific Tool Categories
If you primarily need math tools, configure TinyFn with /mcp/math instead of /mcp/all. This helps the AI select the right tool faster.
{
"mcpServers": {
"tinyfn-math": {
"url": "https://api.tinyfn.io/mcp/math/",
"headers": { "X-API-Key": "your-api-key" }
}
}
}
3. Prompt for Tool Usage
If the AI isn't using TinyFn tools, explicitly ask: "Calculate this using TinyFn" or "Use a tool to verify this."
4. Combine with Code Execution
For complex calculations, you can also ask the assistant to write code that performs the calculation and run it. TinyFn is best for quick, inline calculations.
5. Monitor Your Usage
Check your TinyFn dashboard to see how many requests you're using. The free tier (100/month) is good for testing; upgrade to Starter ($5/month for 10,000) for regular use.
Stop the Math Mistakes
Add TinyFn to your coding assistant and get accurate calculations every time.
Get Free API Key