Numerical Methods at work

Disclaimer:
Permission to use, copy, and distribute this software, and It’s documentation for any non-commercial purpose is hereby granted without fee, provided: THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL Henrik Vestermark, BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Interval Arithmetic C++ template class

Here is a simple interval arithmetic template class that supports the c++ built-in base type of float and double. There is only one header file to be included to make it happen and that is the header file: intervaldouble.h

Building an interval template class that can handle all interval arithmetic for IEEE754 floating point is just down to simple math. The paper you can download describes the practical implementation aspect of building an interval template class and discuss the tricks and math that are behind the implementation. The implementation spans from the basic operators like +,-,*,/ to elementary functions like sqrt(), log(), pow(), exp() and trigonometric functions of sin(), cos(), tan(), asin(), acos() and atan(). Of course, it also supports interval constants like π, ln2, and ln10. The interval arithmetic supports the basic C++ types of float & double.

Click here to download the template class header file. intervaldouble.h
Click here to download the paper "Interval Arithmetic. A Practical implementation. Download

Image of interval Arithmetic boundaries

Intervaldouble.h is version 3.01 from February 2024

Examples of using the interval arithmetic template class:

This example show how to evaluate a polynomial with complex coefficients a, at a complex point z and output the result as a complex interval that is the bound of the evaluation error. The evaluation is done using Horner algorithm

#include "intervaldouble.h"
#include <complex>

complex<interval<double> > horner(const register int n, const complex<double> a[], complex<double> z)
{
 complex<interval<double> > fval;
 complex<interval<double> > zi=z;
 fval = a[0];
 for (int i = 1; i <= n; i++) fval = fval * zi + complex<interval<double> > (a[i]);
  return fval;
}

int test_horner()
{
 complex<double> a[6], z;
 complex<interval<double> > res;

 a[0] = 1.0;
 a[1] = -1.5;
  a[2] = +2.5;
 a[3] = -3.5;
 a[4] = +4.5;
 a[5] = -5.5;
 z = 1.364018313559659;
  res = horner(5, a, z);
}