🎯 Median Calculator
📐 工作原理与说明
Median (page-code conventions): (1) individual samples are separated by commas, whitespace or semicolons; after sorting, odd n takes position (n+1)/2 and even n averages the two middle values. (2) Frequency mode enters one "value, frequency" pair per line; equal values are merged, then the half position is found by cumulative frequency. If total frequency is even and the half position falls exactly between two distinct values, their average is used, matching the expanded-sample convention. (3) Weighted mode enters one "value, weight" pair per line with decimal weights allowed; the half position uses cumulative weight, averaging adjacent values when it falls exactly on a boundary. All-zero frequencies/weights, negative weights, lines without exactly two columns or non-numeric entries return a line-specific error. There is no fixed grading; the output shows the cumulative table, half position and convention note.
The median is the middle value after sorting: for odd n take position (n+1)/2, for even n average the two middle values. Frequency/weighted modes locate the half position by cumulative frequency or cumulative weight, averaging adjacent values when the half position falls exactly on a boundary. Unlike the mean, it is not pulled by extreme values.
Results keep at most 3 decimals; nonzero values below 0.001 keep 6 significant digits, and absolute values below 1e-6 use scientific notation. For even samples each middle value is halved before summing to avoid overflow.
Sort a list of numbers and return the median, resistant to outliers, for skewed data such as scores, salaries and house prices.
将数据排序后取中间值(偶数个取中间两个的平均)
📚 深度解析:中位数计算器
💡 常见使用场景
- 排序取中间,抗极端值。
- 收入、房价等偏态分布数据中看典型水平。
计算
[1,3,5,7,9] 中位数=5;[1,2,3,4] 中位数=(2+3)/2=2.5。
📖 定义与算法
中位数是升序排列后位于中间的数:n 为奇数时取第 (n+1)/2 个(下标 (n−1)/2),n 为偶数时取第 n/2 与第 n/2+1 个的算术平均。页面先把输入按逗号/空格/换行/分号切分并过滤非数字,再排序、取位、给出完整步骤。显示按 1e-10 四舍五入,绝对值小于 1e-12 的浮点残差按 0 显示。
🧮 算例(按本页代码口径实跑)
3, 1, 4, 1, 5, 9, 2, 6, 5(n=9):升序 1, 1, 2, 3, 4, 5, 5, 6, 9,中位数 = 第 5 个 = 4。 1, 2, 3, 4(n=4):取第 2、3 个,(2 + 3) / 2 = 5 / 2 = 2.5。 5, 3, 9, 1(n=4):升序 1, 3, 5, 9,(3 + 5) / 2 = 8 / 2 = 4——虽然原始顺序混乱,排序后结果正确。 空输入或全是非数字:页面提示「请输入数据」,不输出 0。
🧭 为什么用中位数而不是均值
对 1, 2, 3, 4, 100 这组数据,均值 = 110/5 = 22,中位数 = 3。一个极端值把均值拉高 7 倍多,中位数只挪到中间位置。收入、房价、响应时间这类长尾分布因此更适合看中位数;但中位数不参与求和,不能拿来分摊总量。
⚠ 常见误用
不排序直接取中间:输入顺序不代表大小顺序,必须先升序。 偶数个忘记平均:n=4 时中位数是第 2、3 个的均值,不是任意一个。 把中位数当均值:中位数不满足「各项与中心的偏差和为 0」,也不能用于预算平摊。
❓ 常见问题(FAQ)
- 偶数个怎么办?
- 取中间两数平均。
- 中位数与均值差很大说明什么?
- 说明分布偏斜或存在离群值。差值方向告诉你长尾在哪一侧:均值明显大于中位数通常是右偏(少数极大值),反之是左偏。
- 重复值会影响结果吗?
- 不会。位置只由 n 决定,重复值只是让相邻位置上的数相同;例如 1, 1, 2, 3, 4 的中位数仍是第 3 个 = 2。
Formula Reference
| Item | Formula / Note | Source / Standard |
|---|---|---|
| Median | Mean of the two middle values for even n; robust to skew | Standard statistical definition |
⚠️ 使用说明与注意事项
- 本工具纯前端运行,数据不会上传到服务器
- 建议在主流浏览器(Chrome/Safari/Firefox/Edge)中使用
- 计算结果仅供参考,请以实际应用场景为准