Math Utilities

Log2

Calculates the base-2 logarithm of any positive number with mathematical precision. Access via MCP in AI coding assistants like Cursor or Windsurf, or call GET /v1/math/log2 directly. Essential for bit manipulation, algorithm complexity analysis, and binary calculations. Returns exact floating-point results where spreadsheet functions might introduce rounding errors.

API Endpoint

GET /v1/math/log2

Code Examples

curl "https://tinyfn.io/v1/math/log2" \
  -H "X-API-Key: YOUR_API_KEY"
const response = await fetch('https://tinyfn.io/v1/math/log2', {
  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/log2',
    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 do I calculate log base 2 in my AI coding assistant?

Use the log2 MCP tool available in Cursor, Windsurf, Claude Code, and other supported editors. It provides mathematically precise base-2 logarithm calculations for any positive input.

What happens if I pass zero or negative numbers to log2?

The function returns an error for zero and negative inputs since log₂(x) is only defined for positive real numbers. Always validate your input is greater than zero.

Why use this instead of built-in language log functions?

TinyFn's log2 ensures consistent results across different programming environments and eliminates floating-point precision variations that can occur with language-specific implementations.

What's the difference between log2 and natural logarithm?

Log2 uses base 2 (log₂(8) = 3), while natural log uses base e (ln(8) ≈ 2.08). Log2 is essential for computer science applications like binary tree depth and bit operations.

Can I use log2 for calculating algorithm time complexity?

Yes, log2 is perfect for analyzing divide-and-conquer algorithms. For example, log₂(1000000) ≈ 19.93 tells you a binary search needs about 20 steps maximum.

Try Log2 Now

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

Get Free API Key