avg
Calculates the average (mean) of a list of numbers. Provide a set of numeric values to get their arithmetic mean.
Instructions
Average (mean) of numbers.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| numbers | Yes |
Implementation Reference
- cruncher.js:573-590 (schema)Input schema definition for the 'avg' tool. Takes an array of numbers and computes the average (mean).
{ name: "avg", annotations: { title: "Average", readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false, }, description: "Average (mean) of numbers.", inputSchema: { type: "object", properties: { numbers: { type: "array", items: { type: "number" } }, }, required: ["numbers"], }, }, - cruncher.js:1529-1536 (handler)Handler function for the 'avg' tool. Computes the arithmetic mean of an array of numbers using safeMath.add and safeMath.divide to avoid floating-point errors. Throws if the array is empty.
avg: ({ numbers }) => { if (numbers.length === 0) throw new Error("Cannot calculate the average of an empty list."); return safeMath.divide( numbers.reduce((acc, val) => safeMath.add(acc, val), 0), numbers.length, ); }, - cruncher.js:82-82 (registration)Tool name 'avg' registered in the 'standard' tier of the TOOL_TIERS configuration. This controls which tool set is exposed based on the CRUNCHER_TOOL_SET environment variable.
"sum", "avg", "min", "max", "count", "variance", "std_dev", - cruncher.js:1244-1294 (helper)The safeMath helper object, used by the avg handler to safely add and divide numbers with integer-scaling to avoid floating-point precision errors.
const safeMath = { add: (a, b) => { const d1 = countDecimals(a); const d2 = countDecimals(b); const maxDecimals = Math.max(d1, d2); const multiplier = Math.pow(10, maxDecimals); return ( (Math.round(a * multiplier) + Math.round(b * multiplier)) / multiplier ); }, subtract: (a, b) => { const d1 = countDecimals(a); const d2 = countDecimals(b); const maxDecimals = Math.max(d1, d2); const multiplier = Math.pow(10, maxDecimals); return ( (Math.round(a * multiplier) - Math.round(b * multiplier)) / multiplier ); }, multiply: (a, b) => { const d1 = countDecimals(a); const d2 = countDecimals(b); const multiplier1 = Math.pow(10, d1); const multiplier2 = Math.pow(10, d2); return ( (Math.round(a * multiplier1) * Math.round(b * multiplier2)) / (multiplier1 * multiplier2) ); }, divide: (a, b) => { if (b === 0) throw new Error("Division by zero is not allowed."); const d1 = countDecimals(a); const d2 = countDecimals(b); const maxDecimals = Math.max(d1, d2); const multiplier = Math.pow(10, maxDecimals); return Math.round(a * multiplier) / Math.round(b * multiplier); }, modulo: (a, b) => { if (b === 0) throw new Error("Modulo by zero is not allowed."); const d1 = countDecimals(a); const d2 = countDecimals(b); const maxDecimals = Math.max(d1, d2); const multiplier = Math.pow(10, maxDecimals); return ( (Math.round(a * multiplier) % Math.round(b * multiplier)) / multiplier ); }, };