Introduce Numeric Traits, with fuzzy compares, random numbers, etc.

This commit is contained in:
Benoit Jacob
2007-10-07 12:44:42 +00:00
parent c768a44909
commit ae2072406c
4 changed files with 210 additions and 1 deletions

View File

@@ -47,4 +47,35 @@ class EigenTest : public QObject
void testMatrixManip();
};
template<typename T> T TestEpsilon();
template<> int TestEpsilon<int>() { return 0; }
template<> float TestEpsilon<float>() { return 1e-2f; }
template<> double TestEpsilon<double>() { return 1e-4; }
template<typename T> T TestEpsilon<std::complex<T> >() { return TestEpsilon<T>(); }
template<typename T> bool TestNegligible(const T& a, const T& b)
{
return(EiAbs(a) <= EiAbs(b) * TestEpsilon<T>());
}
template<typename T> bool TestApprox(const T& a, const T& b)
{
if(EiTraits<T>::IsFloat)
return(EiAbs(a - b) <= std::min(EiAbs(a), EiAbs(b)) * TestEpsilon<T>());
else
return(a == b);
}
template<typename T> bool TestLessThanOrApprox(const T& a, const T& b)
{
if(EiTraits<T>::IsFloat)
return(a < b || EiApprox(a, b));
else
return(a <= b);
}
#define QVERIFY_NEGLIGIBLE(a, b) QVERIFY(TestNegligible(a, b))
#define QVERIFY_APPROX(a, b) QVERIFY(TestApprox(a, b))
#define QVERIFY_LESS_THAN_OR_APPROX(a, b) QVERIFY(TestLessThanOrApprox(a, b))
#endif // EI_TEST_MAIN_H