Number Utilities

Nearest Power

Finds the closest power of any base to a target number. Access via MCP in Cursor or Windsurf, or REST at `/v1/number/nearest-power`. Given target 100 and base 2, returns 2^7=128 (closest to 100). Essential for algorithms needing power-of-two sizing, exponential approximations, or logarithmic scaling in AI applications.

API Endpoint

GET /v1/number/nearest-power

Code Examples

curl "https://tinyfn.io/v1/number/nearest-power" \
  -H "X-API-Key: YOUR_API_KEY"
const response = await fetch('https://tinyfn.io/v1/number/nearest-power', {
  headers: { 'X-API-Key': 'YOUR_API_KEY' }
});
const data = await response.json();
console.log(data);
import requests

response = requests.get('https://tinyfn.io/v1/number/nearest-power',
    headers={'X-API-Key': 'YOUR_API_KEY'})
data = response.json()
print(data)

Use via MCP

Add to your AI agent

Connect your AI agent (Claude, Cursor, Windsurf, etc.) to TinyFn's number tools:

{
  "mcpServers": {
    "tinyfn-number": {
      "url": "https://tinyfn.io/mcp/number",
      "headers": {
        "X-API-Key": "YOUR_API_KEY"
      }
    }
  }
}

Learn more about MCP setup →

FAQ

How do I find the nearest power of 2 to a number?

Call with your target number and base=2. For target 100, it returns 2^7=128 since 128 is closer to 100 than 2^6=64.

Can I use bases other than 2?

Yes, any positive base works. Base 10 finds nearest powers of 10, base 3 finds powers of 3, etc.

What happens with numbers that are exact powers?

If your target is already a perfect power (like 64 for base 2), it returns that exact power with zero distance.

How can MCP tools use this for buffer sizing?

AI agents in Cline or Claude Code can call this to find optimal power-of-2 buffer sizes, ensuring efficient memory allocation in generated code.

What's returned for fractional or negative numbers?

Works with any real target number. For negatives, it finds the nearest power by absolute distance, handling edge cases deterministically.

Try Nearest Power Now

Get your free API key and start using Nearest Power in seconds.

Get Free API Key