As I told you in my last post, Fortran sucks! This is the C++ Version and its only half as long.
#include <iostream>
#include <limits>
#include <cmath>
using namespace std;
template<typename Func>
void test(Func func, string funcname, double a, double b, double x) {
double res = func(a,b);
cout << funcname << "(" << a << "," << b << ") = " << res << "\n";
if(isnan(x)) {
if(not isnan(res)) {
cout << "But should be " << x << "\n";
}
} else if(res != x) {
cout << "But should be " << x << "\n";
}
}
int main() {
double NaN = numeric_limits<double>::quiet_NaN();
test(min<double>, "min", -3.0, NaN, -3.0);
test(min<double>, "min", NAN, -3.0, -3.0);
test(min<double>, "min", NAN, NAN, NAN);
test(max<double>, "max", -3.0, NAN, -3.0);
test(max<double>, "max", NAN, -3.0, -3.0);
test(max<double>, "max", NAN, NAN, NAN);
}
The ugly procedure pointer is replaced with a simple template. But std::min() and std::max() are also template functions. So we must give the compiler a hint which one we want. We want std::min() for doubles. The code is fine, nothing more to say. Except it dosent work as expected ;)