GeographicLib: GeographicLib::Accumulator< T > Class Template Reference (original) (raw)
An accumulator for sums. More...
#include <[GeographicLib/Accumulator.hpp](Accumulator%5F8hpp%5Fsource.html)>
Public Member Functions | |
---|---|
Accumulator (T y=T(0)) | |
Accumulator & | operator= (T y) |
T | operator() () const |
T | operator() (T y) const |
Accumulator & | operator+= (T y) |
Accumulator & | operator-= (T y) |
Accumulator & | operator*= (int n) |
Accumulator & | operator*= (T y) |
Accumulator & | remainder (T y) |
bool | operator== (T y) const |
bool | operator!= (T y) const |
bool | operator< (T y) const |
bool | operator<= (T y) const |
bool | operator> (T y) const |
bool | operator>= (T y) const |
template
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: 2ns
- Accumulator: 23ns
In the documentation of the member functions, sum stands for the value currently held in the accumulator.
Example of use:
#include
#include
using namespace std;
try {
double sum = 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;
}
}
Header for GeographicLib::Accumulator class.
int main(int argc, const char *const argv[])
Namespace for GeographicLib.
Definition at line 40 of file Accumulator.hpp.
template
Construct from a T. This is not declared explicit, so that you can write Accumulator<double> a = 5;
.
Parameters
Definition at line 102 of file Accumulator.hpp.
◆ operator=()
template
Set the accumulator to a number.
Parameters
Definition at line 111 of file Accumulator.hpp.
◆ operator()() [1/2]
template
Return the value held in the accumulator.
Returns
sum.
Definition at line 117 of file Accumulator.hpp.
◆ operator()() [2/2]
template
Return the result of adding a number to sum (but don't change sum).
Parameters
[in] | y | the number to be added to the sum. |
---|
Returns
sum + y.
Definition at line 125 of file Accumulator.hpp.
◆ operator+=()
template
Add a number to the accumulator.
Parameters
Definition at line 131 of file Accumulator.hpp.
◆ operator-=()
template
Subtract a number from the accumulator.
Parameters
Definition at line 137 of file Accumulator.hpp.
◆ operator*=() [1/2]
template
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
Definition at line 145 of file Accumulator.hpp.
◆ operator*=() [2/2]
template
Multiply accumulator by a number. The fma (fused multiply and add) instruction is used (if available) in order to maintain accuracy.
Parameters
Definition at line 152 of file Accumulator.hpp.
◆ remainder()
template
Reduce accumulator to the range [-y/2, y/2].
Parameters
Definition at line 164 of file Accumulator.hpp.
◆ operator==()
template
◆ operator!=()
template
◆ operator<()
template
◆ operator<=()
template
◆ operator>()
template
◆ operator>=()
template
The documentation for this class was generated from the following file: