LibreOffice Module sc (master) 1
Public Member Functions | Private Attributes | List of all members
KahanSum Class Reference

This class provides LO with Kahan summation algorithm About this algorithm: https://en.wikipedia.org/wiki/Kahan_summation_algorithm For general purpose software we assume first order error is enough. More...

#include <kahan.hxx>

Public Member Functions

constexpr KahanSum ()=default
 
constexpr KahanSum (double x_0)
 
constexpr KahanSum (double x_0, double err_0)
 
constexpr KahanSum (const KahanSum &fSum)=default
 
void add (double x_i)
 Adds a value to the sum using Kahan summation. More...
 
void add (const KahanSum &fSum)
 Adds a value to the sum using Kahan summation. More...
 
void subtract (const KahanSum &fSum)
 Substracts a value to the sum using Kahan summation. More...
 
constexpr KahanSum operator- () const
 
constexpr KahanSumoperator= (double fSum)
 
constexpr KahanSumoperator= (const KahanSum &fSum)=default
 
void operator+= (const KahanSum &fSum)
 
void operator+= (double fSum)
 
void operator-= (const KahanSum &fSum)
 
void operator-= (double fSum)
 
KahanSum operator+ (double fSum) const
 
KahanSum operator+ (const KahanSum &fSum) const
 
KahanSum operator- (double fSum) const
 
KahanSum operator- (const KahanSum &fSum) const
 
constexpr void operator*= (double fTimes)
 In some parts of the code of interpr_.cxx this may be used for product instead of sum. More...
 
constexpr void operator/= (double fDivides)
 
KahanSum operator* (const KahanSum &fTimes) const
 
KahanSum operator* (double fTimes) const
 
KahanSum operator/ (const KahanSum &fDivides) const
 
KahanSum operator/ (double fDivides) const
 
bool operator< (const KahanSum &fSum) const
 
bool operator< (double fSum) const
 
bool operator> (const KahanSum &fSum) const
 
bool operator> (double fSum) const
 
bool operator<= (const KahanSum &fSum) const
 
bool operator<= (double fSum) const
 
bool operator>= (const KahanSum &fSum) const
 
bool operator>= (double fSum) const
 
bool operator== (const KahanSum &fSum) const
 
bool operator!= (const KahanSum &fSum) const
 
double get () const
 Returns the final sum. More...
 

Private Attributes

double m_fSum = 0
 
double m_fError = 0
 
double m_fMem = 0
 

Detailed Description

This class provides LO with Kahan summation algorithm About this algorithm: https://en.wikipedia.org/wiki/Kahan_summation_algorithm For general purpose software we assume first order error is enough.

Additionally queue and remember the last recent non-zero value and add it similar to approxAdd() when obtaining the final result to further eliminate accuracy errors. (e.g. for the dreaded 0.1 + 0.2 - 0.3 != 0.0)

Definition at line 25 of file kahan.hxx.

Constructor & Destructor Documentation

◆ KahanSum() [1/4]

constexpr KahanSum::KahanSum ( )
constexprdefault

◆ KahanSum() [2/4]

constexpr KahanSum::KahanSum ( double  x_0)
inlineconstexpr

Definition at line 30 of file kahan.hxx.

◆ KahanSum() [3/4]

constexpr KahanSum::KahanSum ( double  x_0,
double  err_0 
)
inlineconstexpr

Definition at line 35 of file kahan.hxx.

◆ KahanSum() [4/4]

constexpr KahanSum::KahanSum ( const KahanSum fSum)
constexprdefault

Member Function Documentation

◆ add() [1/2]

void KahanSum::add ( const KahanSum fSum)
inline

Adds a value to the sum using Kahan summation.

Parameters
fSum

Definition at line 72 of file kahan.hxx.

References add(), m_fError, m_fMem, and m_fSum.

◆ add() [2/2]

void KahanSum::add ( double  x_i)
inline

Adds a value to the sum using Kahan summation.

Parameters
x_i

Definition at line 48 of file kahan.hxx.

References m_fError, m_fMem, m_fSum, and t.

Referenced by add(), get(), operator+(), operator+=(), operator-(), operator-=(), and subtract().

◆ get()

double KahanSum::get ( ) const
inline

◆ operator!=()

bool KahanSum::operator!= ( const KahanSum fSum) const
inline

Definition at line 204 of file kahan.hxx.

References get().

◆ operator*() [1/2]

KahanSum KahanSum::operator* ( const KahanSum fTimes) const
inline

Definition at line 178 of file kahan.hxx.

References sc::fTimes, and get().

◆ operator*() [2/2]

KahanSum KahanSum::operator* ( double  fTimes) const
inline

Definition at line 180 of file kahan.hxx.

References sc::fTimes, and get().

◆ operator*=()

constexpr void KahanSum::operator*= ( double  fTimes)
inlineconstexpr

In some parts of the code of interpr_.cxx this may be used for product instead of sum.

This operator shall be used for that task.

Definition at line 150 of file kahan.hxx.

References sc::fTimes, get(), m_fError, m_fMem, and m_fSum.

◆ operator+() [1/2]

KahanSum KahanSum::operator+ ( const KahanSum fSum) const
inline

Definition at line 125 of file kahan.hxx.

◆ operator+() [2/2]

KahanSum KahanSum::operator+ ( double  fSum) const
inline

Definition at line 118 of file kahan.hxx.

References add().

◆ operator+=() [1/2]

void KahanSum::operator+= ( const KahanSum fSum)
inline

Definition at line 110 of file kahan.hxx.

References add().

◆ operator+=() [2/2]

void KahanSum::operator+= ( double  fSum)
inline

Definition at line 112 of file kahan.hxx.

References add().

◆ operator-() [1/3]

constexpr KahanSum KahanSum::operator- ( ) const
inlineconstexpr

Definition at line 91 of file kahan.hxx.

References m_fError, m_fMem, and m_fSum.

◆ operator-() [2/3]

KahanSum KahanSum::operator- ( const KahanSum fSum) const
inline

Definition at line 139 of file kahan.hxx.

◆ operator-() [3/3]

KahanSum KahanSum::operator- ( double  fSum) const
inline

Definition at line 132 of file kahan.hxx.

References add().

◆ operator-=() [1/2]

void KahanSum::operator-= ( const KahanSum fSum)
inline

Definition at line 114 of file kahan.hxx.

References subtract().

◆ operator-=() [2/2]

void KahanSum::operator-= ( double  fSum)
inline

Definition at line 116 of file kahan.hxx.

References add().

◆ operator/() [1/2]

KahanSum KahanSum::operator/ ( const KahanSum fDivides) const
inline

Definition at line 182 of file kahan.hxx.

References get().

◆ operator/() [2/2]

KahanSum KahanSum::operator/ ( double  fDivides) const
inline

Definition at line 184 of file kahan.hxx.

References get().

◆ operator/=()

constexpr void KahanSum::operator/= ( double  fDivides)
inlineconstexpr

Definition at line 164 of file kahan.hxx.

References get(), m_fError, m_fMem, and m_fSum.

◆ operator<() [1/2]

bool KahanSum::operator< ( const KahanSum fSum) const
inline

Definition at line 186 of file kahan.hxx.

References get().

◆ operator<() [2/2]

bool KahanSum::operator< ( double  fSum) const
inline

Definition at line 188 of file kahan.hxx.

References get().

◆ operator<=() [1/2]

bool KahanSum::operator<= ( const KahanSum fSum) const
inline

Definition at line 194 of file kahan.hxx.

References get().

◆ operator<=() [2/2]

bool KahanSum::operator<= ( double  fSum) const
inline

Definition at line 196 of file kahan.hxx.

References get().

◆ operator=() [1/2]

constexpr KahanSum & KahanSum::operator= ( const KahanSum fSum)
constexprdefault

◆ operator=() [2/2]

constexpr KahanSum & KahanSum::operator= ( double  fSum)
inlineconstexpr

Definition at line 100 of file kahan.hxx.

References m_fError, m_fMem, and m_fSum.

◆ operator==()

bool KahanSum::operator== ( const KahanSum fSum) const
inline

Definition at line 202 of file kahan.hxx.

References get().

◆ operator>() [1/2]

bool KahanSum::operator> ( const KahanSum fSum) const
inline

Definition at line 190 of file kahan.hxx.

References get().

◆ operator>() [2/2]

bool KahanSum::operator> ( double  fSum) const
inline

Definition at line 192 of file kahan.hxx.

References get().

◆ operator>=() [1/2]

bool KahanSum::operator>= ( const KahanSum fSum) const
inline

Definition at line 198 of file kahan.hxx.

References get().

◆ operator>=() [2/2]

bool KahanSum::operator>= ( double  fSum) const
inline

Definition at line 200 of file kahan.hxx.

References get().

◆ subtract()

void KahanSum::subtract ( const KahanSum fSum)
inline

Substracts a value to the sum using Kahan summation.

Parameters
fSum

Definition at line 83 of file kahan.hxx.

References add(), m_fError, m_fMem, and m_fSum.

Referenced by operator-=().

Member Data Documentation

◆ m_fError

double KahanSum::m_fError = 0
private

Definition at line 236 of file kahan.hxx.

Referenced by add(), get(), operator*=(), operator-(), operator/=(), operator=(), and subtract().

◆ m_fMem

double KahanSum::m_fMem = 0
private

Definition at line 237 of file kahan.hxx.

Referenced by add(), get(), operator*=(), operator-(), operator/=(), operator=(), and subtract().

◆ m_fSum

double KahanSum::m_fSum = 0
private

Definition at line 235 of file kahan.hxx.

Referenced by add(), get(), operator*=(), operator-(), operator/=(), operator=(), and subtract().


The documentation for this class was generated from the following file: