Algorithms and Subroutines for Solving Differential Equations
Routine Name: maceps
Author: Philip Nelson
Language: C++
maceps
returns the machine epsilon and precision of any primitive type. A make file is included with a driver program that compares maceps
to std::numeric_limits<T>::epsilon()
.
$ make
$ ./maceps.out
This will compile and run the driver program.
maceps<T>( )
requires a template argument T with the type of machine epsilon you want ( float, double, long double, etc… ). Otherwise, maceps
takes no input.
maceps returns an eps
struct with members int prec
which holds the precision and T maceps
which holds the machine epsilon for the specified type.
template <typename T>
eps<T> maceps()
{
T e = 1;
T one = 1;
T half = 0.5;
int prec = 1;
while (one + e * half > one)
{
e *= half;
++prec;
}
return eps(prec, e);
}
int main()
{
auto doubleeps = maceps<double>();
std::cout << "double\n";
std::cout << "precision:\t" << doubleeps.prec << std::endl;
std::cout << "maceps:\t\t" << doubleeps.maceps << std::endl;
std::cout << "std::numeric:\t" << std::numeric_limits<double>::epsilon() << std::endl << std::endl;
}
double
precision: 53
maceps: 2.22045e-16
std::numeric: 2.22045e-16
Last Modification date: 11 January 2018