median
Calculate the median of a list of numbers for accurate statistical analysis. Input an array of numerical values to derive the central value in the dataset.
Instructions
Calculates the median of a list of numbers
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| numbers | Yes | Array of numbers to find the median of |
Implementation Reference
- src/index.ts:150-159 (handler)Handler function that executes the median tool logic by calling Statistics.median(numbers) and returning the result as text content.}, async ({ numbers }) => { const value = Statistics.median(numbers) return { content: [{ type: "text", text: `${value}` }] } })
- src/index.ts:149-149 (schema)Input schema using Zod: requires an array of at least one number.numbers: z.array(z.number()).min(1).describe("Array of numbers to find the median of")
- src/index.ts:148-148 (registration)Registration of the 'median' tool on the mathServer with description, schema, and handler.mathServer.tool("median", "Calculates the median of a list of numbers", {
- src/Classes/Statistics.ts:21-38 (helper)Core implementation of median calculation: sorts the array, handles odd/even length to find middle value(s).static median(numbers: number[]) { //Sort numbers numbers.sort() //Find the median index const medianIndex = numbers.length / 2 let medianValue: number; if (numbers.length % 2 !== 0) { //If number is odd medianValue = numbers[Math.floor(medianIndex)] } else { //If number is even medianValue = (numbers[medianIndex] + numbers[medianIndex - 1]) / 2 } return medianValue }