mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-28 13:43:45 +03:00
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.
This commit is contained in:
parent
0cede94c39
commit
25dd0a4d2d
Notes:
sideshowbarker
2024-07-17 05:23:40 +09:00
Author: https://github.com/Popaulol 🔰 Commit: https://github.com/SerenityOS/serenity/commit/25dd0a4d2d Pull-request: https://github.com/SerenityOS/serenity/pull/16870 Reviewed-by: https://github.com/AtkinsSJ ✅ Reviewed-by: https://github.com/drunderscore ✅
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user