Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
GeographicLib::Accumulator< T > Class Template Reference

An accumulator for sums. More...

#include <Accumulator.hpp>

Public Member Functions

 Accumulator (T y=T(0))
 
bool operator!= (T y) const
 
T operator() () const
 
T operator() (T y) const
 
Accumulatoroperator*= (int n)
 
Accumulatoroperator*= (T y)
 
Accumulatoroperator+= (T y)
 
Accumulatoroperator-= (T y)
 
bool operator< (T y) const
 
bool operator<= (T y) const
 
Accumulatoroperator= (T y)
 
bool operator== (T y) const
 
bool operator> (T y) const
 
bool operator>= (T y) const
 

Private Member Functions

void Add (T y)
 
T Sum (T y) const
 

Static Private Member Functions

static T fastsum (T u, T v, T &t)
 

Private Attributes

T _s
 
T _t
 

Detailed Description

template<typename T = Math::real>
class GeographicLib::Accumulator< T >

An accumulator for sums.

This allows many numbers of floating point type T to be added together with twice the normal precision. Thus if T is double, the effective precision of the sum is 106 bits or about 32 decimal places.

The implementation follows J. R. Shewchuk, Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates, Discrete & Computational Geometry 18(3) 305–363 (1997).

Approximate timings (summing a vector<double>)

In the documentation of the member functions, sum stands for the value currently held in the accumulator.

Example of use:

// Example of using the GeographicLib::Accumulator class
#include <iostream>
#include <exception>
using namespace std;
using namespace GeographicLib;
int main() {
try {
// Compare using Accumulator and ordinary summation for a sum of large and
// small terms.
double sum = 0;
Accumulator<> acc = 0;
sum += 1e20; sum += 1; sum += 2; sum += 100; sum += 5000; sum += -1e20;
acc += 1e20; acc += 1; acc += 2; acc += 100; acc += 5000; acc += -1e20;
cout << sum << " " << acc() << "\n";
}
catch (const exception& e) {
cerr << "Caught exception: " << e.what() << "\n";
return 1;
}
}

Definition at line 40 of file Accumulator.hpp.

Constructor & Destructor Documentation

◆ Accumulator()

template<typename T = Math::real>
GeographicLib::Accumulator< T >::Accumulator ( T  y = T(0))
inline

Construct from a T. This is not declared explicit, so that you can write Accumulator<double> a = 5;.

Parameters
[in]yset sum = y.

Definition at line 102 of file Accumulator.hpp.

Member Function Documentation

◆ Add()

template<typename T = Math::real>
void GeographicLib::Accumulator< T >::Add ( T  y)
inlineprivate

Definition at line 52 of file Accumulator.hpp.

◆ fastsum()

template<typename T = Math::real>
static T GeographicLib::Accumulator< T >::fastsum ( T  u,
T  v,
T t 
)
inlinestaticprivate

Definition at line 46 of file Accumulator.hpp.

◆ operator!=()

template<typename T = Math::real>
bool GeographicLib::Accumulator< T >::operator!= ( T  y) const
inline

Test inequality of an Accumulator with a number.

Definition at line 165 of file Accumulator.hpp.

◆ operator()() [1/2]

template<typename T = Math::real>
T GeographicLib::Accumulator< T >::operator() ( ) const
inline

Return the value held in the accumulator.

Returns
sum.

Definition at line 117 of file Accumulator.hpp.

◆ operator()() [2/2]

template<typename T = Math::real>
T GeographicLib::Accumulator< T >::operator() ( T  y) const
inline

Return the result of adding a number to sum (but don't change sum).

Parameters
[in]ythe number to be added to the sum.
Returns
sum + y.

Definition at line 125 of file Accumulator.hpp.

◆ operator*=() [1/2]

template<typename T = Math::real>
Accumulator& GeographicLib::Accumulator< T >::operator*= ( int  n)
inline

Multiply accumulator by an integer. To avoid loss of accuracy, use only integers such that n × T is exactly representable as a T (i.e., ± powers of two). Use n = −1 to negate sum.

Parameters
[in]nset sum *= n.

Definition at line 145 of file Accumulator.hpp.

◆ operator*=() [2/2]

template<typename T = Math::real>
Accumulator& GeographicLib::Accumulator< T >::operator*= ( T  y)
inline

Multiply accumulator by a number. The fma (fused multiply and add) instruction is used (if available) in order to maintain accuracy.

Parameters
[in]yset sum *= y.

Definition at line 152 of file Accumulator.hpp.

◆ operator+=()

template<typename T = Math::real>
Accumulator& GeographicLib::Accumulator< T >::operator+= ( T  y)
inline

Add a number to the accumulator.

Parameters
[in]yset sum += y.

Definition at line 131 of file Accumulator.hpp.

◆ operator-=()

template<typename T = Math::real>
Accumulator& GeographicLib::Accumulator< T >::operator-= ( T  y)
inline

Subtract a number from the accumulator.

Parameters
[in]yset sum -= y.

Definition at line 137 of file Accumulator.hpp.

◆ operator<()

template<typename T = Math::real>
bool GeographicLib::Accumulator< T >::operator< ( T  y) const
inline

Less operator on an Accumulator and a number.

Definition at line 169 of file Accumulator.hpp.

◆ operator<=()

template<typename T = Math::real>
bool GeographicLib::Accumulator< T >::operator<= ( T  y) const
inline

Less or equal operator on an Accumulator and a number.

Definition at line 173 of file Accumulator.hpp.

◆ operator=()

template<typename T = Math::real>
Accumulator& GeographicLib::Accumulator< T >::operator= ( T  y)
inline

Set the accumulator to a number.

Parameters
[in]yset sum = y.

Definition at line 111 of file Accumulator.hpp.

◆ operator==()

template<typename T = Math::real>
bool GeographicLib::Accumulator< T >::operator== ( T  y) const
inline

Test equality of an Accumulator with a number.

Definition at line 161 of file Accumulator.hpp.

◆ operator>()

template<typename T = Math::real>
bool GeographicLib::Accumulator< T >::operator> ( T  y) const
inline

Greater operator on an Accumulator and a number.

Definition at line 177 of file Accumulator.hpp.

◆ operator>=()

template<typename T = Math::real>
bool GeographicLib::Accumulator< T >::operator>= ( T  y) const
inline

Greater or equal operator on an Accumulator and a number.

Definition at line 181 of file Accumulator.hpp.

◆ Sum()

template<typename T = Math::real>
T GeographicLib::Accumulator< T >::Sum ( T  y) const
inlineprivate

Definition at line 90 of file Accumulator.hpp.

Member Data Documentation

◆ _s

template<typename T = Math::real>
T GeographicLib::Accumulator< T >::_s
private

Definition at line 43 of file Accumulator.hpp.

◆ _t

template<typename T = Math::real>
T GeographicLib::Accumulator< T >::_t
private

Definition at line 43 of file Accumulator.hpp.


The documentation for this class was generated from the following file:
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
GeographicLib
Namespace for GeographicLib.
Definition: JacobiConformal.hpp:15
main
int main(int argc, char **argv)
Definition: cmake/example_cmake_find_gtsam/main.cpp:63
Accumulator.hpp
Header for GeographicLib::Accumulator class.
GeographicLib::Accumulator
An accumulator for sums.
Definition: Accumulator.hpp:40
std
Definition: BFloat16.h:88


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:23:28