avg
Calculate the average (mean) of a list of numbers. Provide an array of numbers to get their mean value.
Instructions
Average (mean) of numbers.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| numbers | Yes |
Implementation Reference
- cruncher.js:1507-1514 (handler)The avg handler function. Calculates the mean by summing numbers using safeMath.add and dividing by count using safeMath.divide. Throws on empty array.
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:567-584 (schema)The inputSchema definition for the 'avg' tool. Defines the 'numbers' parameter as an array of numbers (required).
{ 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:76-76 (registration)The 'avg' tool is listed in the 'standard' tool tier, meaning it's exposed in standard and full modes.
"sum", "avg", "min", "max", "count", "variance", "std_dev", - cruncher.js:1238-1288 (helper)The safeMath.add and safeMath.divide helpers used by the avg handler to perform safe floating-point arithmetic via integer-scaling.
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 ); }, };