Math Utilities

Clamp

Constrains numeric values within specified minimum and maximum bounds via GET /v1/math/clamp. Essential for input validation, UI range controls, and data normalization. If value < min, returns min; if value > max, returns max; otherwise returns the original value unchanged.

API Endpoint

GET /v1/math/clamp

Code Examples

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

response = requests.get('https://tinyfn.io/v1/math/clamp',
    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 math tools:

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

Learn more about MCP setup →

FAQ

How does number clamping work with edge cases?

Returns the original value if it's within bounds, min if below minimum, max if above maximum. Handles negative numbers, decimals, and zero correctly.

Can I clamp floating point numbers to decimal ranges?

Yes, works with any numeric type including floats. Clamping 3.7 between 1.0 and 3.5 returns 3.5.

What happens if min is greater than max in the clamp range?

Behavior depends on implementation but typically returns one of the bounds. Always ensure min ≤ max for predictable results.

How do MCP-enabled editors use clamp for form validation?

AI agents can clamp user inputs in real-time, ensuring values stay within valid ranges for sliders, numeric inputs, or configuration parameters.

Is clamping the same as Math.min and Math.max combined?

Essentially yes - clamp(value, min, max) equals Math.min(Math.max(value, min), max), but as a single deterministic operation.

Try Clamp Now

Get your free API key and start using Clamp in seconds.

Get Free API Key