.NET wrapper for GeographicLib::Accumulator.
This class allows .NET applications to access GeographicLib::Accumulator<double>.
This allow many numbers of floating point type double to be added together with twice the normal precision. 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).
C# Example:
using System;
namespace example_Accumulator
{
class Program
{
static void Main(string[] args)
{
try {
double sum = 0;
acc.Assign( 0.0 );
sum += 1e20; sum += 1; sum += 2; sum += 100; sum += 5000; sum += -1e20;
acc.Sum( 1e20 ); acc.Sum( 1 ); acc.Sum( 2 ); acc.Sum( 100 ); acc.Sum( 5000 ); acc.Sum( -1e20 );
Console.WriteLine(String.Format("{0} {1}", sum, acc.Result()));
}
catch (GeographicErr e) {
Console.WriteLine( String.Format( "Caught exception: {0}", e.Message ) );
}
}
}
}
Accumulator(void)
Constructor.
Managed C++ Example:
using namespace System;
int main(array<System::String ^> ^)
{
try {
double sum = 0;
acc->Assign( 0.0 );
sum += 1e20; sum += 1; sum += 2; sum += 100; sum += 5000; sum += -1e20;
acc->Sum( 1e20 ); acc->Sum( 1 ); acc->Sum( 2 ); acc->Sum( 100 ); acc->Sum( 5000 ); acc->Sum( -1e20 );
Console::WriteLine(String::Format("{0} {1}", sum, acc->Result()));
}
catch (GeographicErr^ e) {
Console::WriteLine( String::Format( "Caught exception: {0}", e->Message ) );
return -1;
}
return 0;
}
int main(int argc, const char *const argv[])
Visual Basic Example:
Imports NETGeographicLib
Module example_Accumulator
Public Sub Main()
Try
' Compare using Accumulator and ordinary summation for a sum of large and
' small terms.
Dim sum As Double = 0.0
Dim acc As Accumulator = New Accumulator()
acc.Assign(0.0)
sum += 1.0E+20 : sum += 1 : sum += 2 : sum += 100 : sum += 5000
sum += -1.0E+20 : acc.Sum(1.0E+20) : acc.Sum(1) : acc.Sum(2) : acc.Sum(100) : acc.Sum(5000) : acc.Sum(-1.0E+20)
Console.WriteLine(String.Format("{0} {1}", sum, acc.Result()))
Catch ex As GeographicErr
Console.WriteLine(String.Format("Caught exception: {0}", ex.Message))
End Try
End Sub
End Module
INTERFACE DIFFERENCES:
Since assignment operators (=,+=,-=,*=) are not supported in managed classes;
- the Assign() method replaces the = operator,
- the Sum() method replaces the += and -= operators, and
- the Multiply() method replaces the *= operator,
Use Result() instead of the () operator to obtain the summed value from the accumulator.
Definition at line 44 of file Accumulator.h.