GeographicLib  1.21
Public Member Functions
GeographicLib::Accumulator Class Reference

An accumulator for sums. More...

#include <GeographicLib/Accumulator.hpp>

List of all members.

Public Member Functions

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

Detailed Description

An accumulator for sums.

This allow 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 core idea is the error free transformation of a sum, D. E. Knuth, TAOCP, Vol 2, 4.2.2, Theorem B.

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
// $Id: cd6505cf47fbae653b439ba63ea927050cbf0a45 $

#include <iostream>
#include <exception>
#include <GeographicLib/Accumulator.hpp>

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<double> 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;
  }
  return 0;
}

Constructor & Destructor Documentation

GeographicLib::Accumulator::Accumulator ( y = T(0)) throw () [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 116 of file Accumulator.hpp.


Member Function Documentation

Accumulator& GeographicLib::Accumulator::operator= ( y) throw () [inline]

Set the accumulator to a number.

Parameters:
[in]yset sum = y.

Definition at line 125 of file Accumulator.hpp.

T GeographicLib::Accumulator::operator() ( ) const throw () [inline]

Return the value held in the accumulator.

Returns:
sum.

Definition at line 131 of file Accumulator.hpp.

T GeographicLib::Accumulator::operator() ( y) const throw () [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 138 of file Accumulator.hpp.

Accumulator& GeographicLib::Accumulator::operator+= ( y) throw () [inline]

Add a number to the accumulator.

Parameters:
[in]yset sum += y.

Definition at line 144 of file Accumulator.hpp.

Accumulator& GeographicLib::Accumulator::operator-= ( y) throw () [inline]

Subtract a number from the accumulator.

Parameters:
[in]yset sum -= y.

Definition at line 150 of file Accumulator.hpp.

Accumulator& GeographicLib::Accumulator::operator*= ( int  n) throw () [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 158 of file Accumulator.hpp.

bool GeographicLib::Accumulator::operator== ( y) const throw () [inline]

Test equality of an Accumulator with a number.

Definition at line 162 of file Accumulator.hpp.

bool GeographicLib::Accumulator::operator!= ( y) const throw () [inline]

Test inequality of an Accumulator with a number.

Definition at line 166 of file Accumulator.hpp.

bool GeographicLib::Accumulator::operator< ( y) const throw () [inline]

Less operator on an Accumulator and a number.

Definition at line 170 of file Accumulator.hpp.

bool GeographicLib::Accumulator::operator<= ( y) const throw () [inline]

Less or equal operator on an Accumulator and a number.

Definition at line 174 of file Accumulator.hpp.

bool GeographicLib::Accumulator::operator> ( y) const throw () [inline]

Greater operator on an Accumulator and a number.

Definition at line 178 of file Accumulator.hpp.

bool GeographicLib::Accumulator::operator>= ( y) const throw () [inline]

Greater or equal operator on an Accumulator and a number.

Definition at line 182 of file Accumulator.hpp.


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