median
Calculate the middle value from a set of numbers. Provide an array of numbers to get the central value.
Instructions
Median of numbers.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| numbers | Yes | ||
| timeout | No | Custom timeout in ms (100-60000, default: 3000) |
Implementation Reference
- cruncher.js:1516-1531 (handler)The function that executes the median tool logic: sorts numbers and returns the middle value (or average of two middle values for even-length arrays).
/** * Calculates the median of an array of numbers. * @param {Object} args - The arguments object. * @param {number[]} args.numbers - Array of numbers. * @returns {number} The median value. * @throws {Error} If the array is empty. */ median: ({ numbers }) => { if (numbers.length === 0) throw new Error("Cannot calculate the median of an empty list."); const sorted = [...numbers].sort((a, b) => a - b); const mid = Math.floor(sorted.length / 2); return sorted.length % 2 === 0 ? (sorted[mid - 1] + sorted[mid]) / 2 : sorted[mid]; }, - cruncher.js:585-603 (schema)Input schema for the median tool: requires an array of numbers, with optional custom timeout parameter.
{ name: "median", annotations: { title: "Median", readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false, }, description: "Median of numbers.", inputSchema: { type: "object", properties: { numbers: { type: "array", items: { type: "number" } }, timeout: { type: "number", minimum: 100, maximum: 60000, description: "Custom timeout in ms (100-60000, default: 3000)" }, }, required: ["numbers"], }, }, - cruncher.js:69-79 (registration)Registration of 'median' in the standard tool tier, which is the default tool set.
standard: [ "evaluate_expression", "add", "subtract", "multiply", "divide", "sqrt", "power", "absolute", "modulo", "factorial", "logarithm", "natural_log", "get_constant", "sine", "cosine", "tangent", "asin", "acos", "atan", "set_angle_mode", "get_angle_mode", "sum", "avg", "min", "max", "count", "variance", "std_dev", "percentage_of", "percentage_change", "percentage_reverse", "median", "range", "convert_unit", - cruncher.js:127-128 (helper)The median tool is listed in TIMEOUT_TOOLS, meaning it supports a custom timeout parameter and runs in a worker thread.
const TIMEOUT_TOOLS = new Set(["factorial", "median", "percentile"]);