From 25dd0a4d2d668f1927b525ab12a4928b1f262461 Mon Sep 17 00:00:00 2001 From: Staubfinger Date: Fri, 6 Jan 2023 21:38:21 +0100 Subject: [PATCH] AK: Make `Statistics::median` return a mean for an even number of values The previous implementation of Statistics::median() was slightly incorrect with an even number of elements since in those cases it needs to be the arithmetic mean of the two elements that share the middle position. --- AK/Statistics.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/AK/Statistics.h b/AK/Statistics.h index bd42f9514a5..9caf69ad77b 100644 --- a/AK/Statistics.h +++ b/AK/Statistics.h @@ -55,6 +55,11 @@ public: T const median() { quick_sort(m_values); + // If the number of values is even, the median is the arithmetic mean of the two middle values + if (size() % 2 == 0) { + auto index = size() / 2; + return (m_values.at(index) + m_values.at(index + 1)) / 2; + } return m_values.at(size() / 2); }