sqrt
Compute the square root of a non-negative number. Returns an error for negative input.
Instructions
Square root. Errors on negative input.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| value | Yes |
Implementation Reference
- cruncher.js:1392-1398 (handler)The handler function for the 'sqrt' tool. Takes a 'value' parameter, validates it is non-negative, and returns Math.sqrt(value).
sqrt: ({ value }) => { if (value < 0) throw new Error( "Cannot calculate the square root of a negative number.", ); return Math.sqrt(value); }, - cruncher.js:324-339 (schema)The schema definition for the 'sqrt' tool. Defines its name, annotations, description, and inputSchema requiring a single 'value' parameter of type 'number'.
name: "sqrt", annotations: { title: "Square Root", readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false, }, description: "Square root. Errors on negative input.", inputSchema: { type: "object", properties: { value: { type: "number" } }, required: ["value"], }, }, - cruncher.js:1050-1051 (registration)The 'sqrt' tool is registered via the toolsAll array (line 323-339), filtered by tier (TOOL_TIERS.standard includes 'sqrt', line 78), and made available via TOOL_LOOKUP_MAP.
/** Active tool list — filtered by CRUNCHER_TOOL_SET env var. */ const TOOLS = filterToolsByTier(toolsAll, TOOL_SET); - cruncher.js:136-149 (registration)'sqrt' is registered as a main-thread tool (no worker overhead), meaning it executes synchronously on the main thread without spawning a worker.
const MAIN_THREAD_TOOLS = new Set([ // Angle management "set_angle_mode", "get_angle_mode", // Trigonometry (instant Math calls) "sine", "cosine", "tangent", "asin", "acos", "atan", // Cache management "cache_clear", "cache_info", // Simple stats (zero-cost) "count", "min", "max", "variance", "std_dev", // Percentage "percentage_of", "percentage_change", "percentage_reverse", // Math one-liners "power", "sqrt", "logarithm", "natural_log", "absolute", // Constant lookup - cruncher.js:217-229 (helper)Pre-compiled regex patterns used in evaluate_expression to support 'sqrt()' function calls by converting them to 'Math.sqrt()'.
const RE_FUNC_SQRT = /\bsqrt\s*\(/g; const RE_FUNC_LOG = /\blog10\s*\(/g; // log10() → Math.log10() const RE_FUNC_LN = /\bln\s*\(/g; // ln() → Math.log() const RE_FUNC_LOG_BASE = /\blog\s*\(([^,)]+)\s*,\s*([^)]+)\)/g; // log(x,base) // Constants pattern: longest names first to avoid partial matches (e_charge before e, // euler_mascheroni before e, sqrt2 before pi, tau before tau). Built dynamically. const RE_CONSTANTS = (() => { const names = Object.keys(CONSTANTS).sort((a, b) => b.length - a.length); const escaped = names.map(n => n.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')); return new RegExp(`\\b(${escaped.join('|')})\\b`, 'g'); })(); const RE_DISALLOWED_CHARS = /[^0-9+\-*/().% \t*,Mathabspowrndflceigumsxqtogv1]/; const RE_VALID_MATH_CALLS = /Math\.(pow|abs|round|floor|ceil|min|max|sin|cos|tan|asin|acos|atan|sqrt|log10|log)\(/g;