{
  "schemaVersion": 1,
  "slug": "median-calculator",
  "category": "math",
  "kind": "implementation",
  "independentValidation": false,
  "implementationHash": "6b0d0fd08aef75ebecf6c9f0fb7a6de8ae23a7a5487ecf6640089c3d53b28480",
  "provenance": "Generated from the calculation contract used by the published page. Publication is not external validation.",
  "sourceFiles": [
    "data/calculators/median-calculator.json",
    "src/engine.js"
  ],
  "runtimeSource": "/sources/engine.txt",
  "runtimeHash": "5c888d8898afbdee36267bb9977829e1e2ac568f6677c8ee87469c139864645c",
  "scope": "The compute function records implemented equations, constants, lookup tables and custom rules. Inputs record defaults and allowed values; outputs record precision and units. Defaults are examples, not recommendations. General background references are not evidence for this specific algorithm.",
  "publishedFormula": {
    "formula": "Q(p) = x[⌊k⌋] + (x[⌈k⌉] − x[⌊k⌋])(k − ⌊k⌋), k = (n − 1)p; median = Q(0.5)",
    "vars": [
      [
        "Q(p)",
        "quantile at probability p"
      ],
      [
        "median",
        "Q(0.5)"
      ],
      [
        "x",
        "sorted values"
      ],
      [
        "n",
        "value count"
      ],
      [
        "p",
        "probability 0–1"
      ],
      [
        "k",
        "position index, 0-based"
      ]
    ],
    "note": "Uses linear interpolation at positions (n−1)p for Q1, median and Q3. Empty fields, non-finite numbers and lists above 10,000 values are rejected."
  },
  "externalEvidence": null,
  "contract": {
    "compute": "const parseList=s=>{const t=String(s).trim(),num='[+-]?(?:\\\\d+(?:\\\\.\\\\d*)?|\\\\.\\\\d+)(?:[eE][+-]?\\\\d+)?',sep='(?:\\\\s*[,;]\\\\s*|\\\\s+)';if(!t||t.length>20000||!new RegExp('^\\\\s*'+num+'(?:'+sep+num+')*\\\\s*$').test(t))return null;const a=t.match(new RegExp(num,'g')).map(Number);return a.length<=10000&&a.every(Number.isFinite)?a:null;};const xs=parseList(values);if(!xs)return{median:null,q1:null,q3:null,iqr:null,range:null,__error:'inputError'};xs.sort((a,b)=>a-b);const q=p=>{const idx=(xs.length-1)*p,lo=Math.floor(idx),hi=Math.ceil(idx);return xs[lo]+(xs[hi]-xs[lo])*(idx-lo);},median=q(.5),q1=q(.25),q3=q(.75),iqr=q3-q1,range=xs[xs.length-1]-xs[0];if(![median,q1,q3,iqr,range].every(Number.isFinite))return{median:null,q1:null,q3:null,iqr:null,range:null,__error:'inputError'};return{median,q1,q3,iqr,range};",
    "inputs": [
      {
        "name": "values",
        "type": "text",
        "default": "12, 7, 3, 19, 25, 8, 15",
        "step": 1
      }
    ],
    "outputs": [
      {
        "name": "median",
        "precision": 4,
        "unit": "",
        "trimTrailingZeros": true
      },
      {
        "name": "q1",
        "precision": 4,
        "unit": "",
        "trimTrailingZeros": true
      },
      {
        "name": "q3",
        "precision": 4,
        "unit": "",
        "trimTrailingZeros": true
      },
      {
        "name": "iqr",
        "precision": 4,
        "unit": "",
        "trimTrailingZeros": true
      },
      {
        "name": "range",
        "precision": 4,
        "unit": "",
        "trimTrailingZeros": true
      }
    ],
    "interpret": null
  }
}
