AK: Improve numerical accuracy with Kahan summation algorithm

This commit is contained in:
Faraz Fallahi 2024-07-09 19:40:00 -07:00
parent f76f84d687
commit a0277b6320

View file

@ -26,8 +26,13 @@ public:
explicit Statistics(ContainerType&& existing_container)
: m_values(forward<ContainerType>(existing_container))
{
for (auto const& value : m_values)
m_sum += value;
T c = 0.0; // correction term
for (auto const& value : m_values) {
T const y = value - c;
T const t = m_sum + y;
c = (t - m_sum) - y;
m_sum = t;
}
}
void add(T const& value)