Skip to content
Snippets Groups Projects
Commit 1b8ded47 authored by Tom Fischer's avatar Tom Fischer Committed by Dmitri Naumov
Browse files

[MaL] Implement KahanSum algorithm

parent 7552550e
No related branches found
No related tags found
No related merge requests found
/**
* \file
* \copyright
* Copyright (c) 2012-2024, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*/
#pragma once
#include <iomanip>
#include <ostream>
namespace MathLib
{
class KahanSum
{
public:
explicit constexpr KahanSum(double const value = 0) : value_(value) {}
constexpr KahanSum& operator+=(double const increment)
{
double const y = increment - correction_;
double const t = value_ + y;
correction_ = (t - value_) - y;
value_ = t;
return *this;
}
constexpr double value() const { return value_; }
constexpr double operator()() const { return value_; }
friend inline std::ostream& operator<<(std::ostream& os, KahanSum const& x)
{
auto const precision = os.precision();
return os << std::setprecision(
std::numeric_limits<double>::max_digits10)
<< x.value() << " (± " << x.correction_ << ')'
<< std::setprecision(precision);
}
private:
double value_;
double correction_ = 0.;
};
} // namespace MathLib
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment