Algorithms and Subroutines for Solving Differential Equations
Routine Names: absoluteError and relativeError
Author: Philip Nelson
Language: C++
absoluteError
calculates the absolute error given an approximation and a real value.
relativeError
calculates the relative error given an approximation and a real value.
A driver program and make file are provided.
$ make
$ ./error.out
This will compile and run the driver program.
absoluteError<T>(T approx, T value)
requires:
T approx
- the approximated valueT value
- the real valueabsolute error \(= \epsilon := \Big \lvert v - v_{\text{approx}} \Big \rvert \)
Note: approx
and value
must be the same type.
absoluteError
returns a T
with the absolute error.
template <typename T>
inline T absoluteError(const T approx, const T value)
{
return std::abs(value - approx);
}
int main()
{
double approx = 3.2;
double value = 3.14159;
std::cout << "Approximate: " << approx
<< "\nReal Value: " << value << std::endl
<< std::endl;
std::cout << "Absolute: " << absoluteError(approx, value) << std::endl;
}
Approximate: 3.2
Real Value: 3.14159
Absolute: 0.05841
relativeError<T>(T approx, T value)
requires:
T approx
- the approximated valueT value
the real valueelative error \(= \eta := \frac{\epsilon}{\lvert v \rvert} \)
Note: approx
and value
must be the same type.
relativeError
returns a T
with the relative error.
template <typename T>
inline T relativeError(const T approx, const T value)
{
return std::abs(absoluteError(value, approx) / value);
}
int main()
{
double approx = 3.2;
double value = 3.14159;
std::cout << "Approximate: " << approx
<< "\nReal Value: " << value << std::endl
<< std::endl;
std::cout << "Relative: " << relativeError(approx, value) << std::endl;
}
Approximate: 3.2
Real Value: 3.14159
Relative: 0.0185925
Last Modification date: 11 January 2018