Simbody  3.5

isNumericallyEqual(x,y) compares two scalar types using a tolerance (default or explicitly specified) and returns true if they are close enough. More...

Functions

bool SimTK::isNumericallyEqual (const float &a, const float &b, double tol=RTraits< float >::getDefaultTolerance())
 Compare two floats for approximate equality. More...
 
bool SimTK::isNumericallyEqual (const double &a, const double &b, double tol=RTraits< double >::getDefaultTolerance())
 Compare two doubles for approximate equality. More...
 
bool SimTK::isNumericallyEqual (const long double &a, const long double &b, double tol=RTraits< long double >::getDefaultTolerance())
 Compare two long doubles for approximate equality. More...
 
bool SimTK::isNumericallyEqual (const float &a, const double &b, double tol=RTraits< float >::getDefaultTolerance())
 Compare a float and a double for approximate equality at float precision. More...
 
bool SimTK::isNumericallyEqual (const double &a, const float &b, double tol=RTraits< float >::getDefaultTolerance())
 Compare a float and a double for approximate equality at float precision. More...
 
bool SimTK::isNumericallyEqual (const float &a, const long double &b, double tol=RTraits< float >::getDefaultTolerance())
 Compare a float and a long double for approximate equality at float precision. More...
 
bool SimTK::isNumericallyEqual (const long double &a, const float &b, double tol=RTraits< float >::getDefaultTolerance())
 Compare a float and a long double for approximate equality at float precision. More...
 
bool SimTK::isNumericallyEqual (const double &a, const long double &b, double tol=RTraits< double >::getDefaultTolerance())
 Compare a double and a long double for approximate equality at double precision. More...
 
bool SimTK::isNumericallyEqual (const long double &a, const double &b, double tol=RTraits< double >::getDefaultTolerance())
 Compare a double and a long double for approximate equality at double precision. More...
 
bool SimTK::isNumericallyEqual (const float &a, int b, double tol=RTraits< float >::getDefaultTolerance())
 Test a float for approximate equality to an integer. More...
 
bool SimTK::isNumericallyEqual (int a, const float &b, double tol=RTraits< float >::getDefaultTolerance())
 Test a float for approximate equality to an integer. More...
 
bool SimTK::isNumericallyEqual (const double &a, int b, double tol=RTraits< double >::getDefaultTolerance())
 Test a double for approximate equality to an integer. More...
 
bool SimTK::isNumericallyEqual (int a, const double &b, double tol=RTraits< double >::getDefaultTolerance())
 Test a double for approximate equality to an integer. More...
 
bool SimTK::isNumericallyEqual (const long double &a, int b, double tol=RTraits< long double >::getDefaultTolerance())
 Test a long double for approximate equality to an integer. More...
 
bool SimTK::isNumericallyEqual (int a, const long double &b, double tol=RTraits< long double >::getDefaultTolerance())
 Test a long double for approximate equality to an integer. More...
 
template<class P , class Q >
bool SimTK::isNumericallyEqual (const std::complex< P > &a, const std::complex< Q > &b, double tol=RTraits< typename Narrowest< P, Q >::Precision >::getDefaultTolerance())
 Compare two complex numbers for approximate equality, using the numerical accuracy expectation of the narrower of the two precisions in the case of mixed precision. More...
 
template<class P , class Q >
bool SimTK::isNumericallyEqual (const conjugate< P > &a, const conjugate< Q > &b, double tol=RTraits< typename Narrowest< P, Q >::Precision >::getDefaultTolerance())
 Compare two conjugate numbers for approximate equality, using the numerical accuracy expectation of the narrower of the two precisions in the case of mixed precision. More...
 
template<class P , class Q >
bool SimTK::isNumericallyEqual (const std::complex< P > &a, const conjugate< Q > &b, double tol=RTraits< typename Narrowest< P, Q >::Precision >::getDefaultTolerance())
 Compare a complex and a conjugate number for approximate equality, using the numerical accuracy expectation of the narrower of the two precisions in the case of mixed precision. More...
 
template<class P , class Q >
bool SimTK::isNumericallyEqual (const conjugate< P > &a, const std::complex< Q > &b, double tol=RTraits< typename Narrowest< P, Q >::Precision >::getDefaultTolerance())
 Compare a complex and a conjugate number for approximate equality, using the numerical accuracy expectation of the narrower of the two precisions in the case of mixed precision. More...
 
template<class P >
bool SimTK::isNumericallyEqual (const std::complex< P > &a, const float &b, double tol=RTraits< float >::getDefaultTolerance())
 Test whether a complex number is approximately equal to a particular real float. More...
 
template<class P >
bool SimTK::isNumericallyEqual (const float &a, const std::complex< P > &b, double tol=RTraits< float >::getDefaultTolerance())
 Test whether a complex number is approximately equal to a particular real float. More...
 
template<class P >
bool SimTK::isNumericallyEqual (const std::complex< P > &a, const double &b, double tol=RTraits< typename Narrowest< P, double >::Precision >::getDefaultTolerance())
 Test whether a complex number is approximately equal to a particular real double. More...
 
template<class P >
bool SimTK::isNumericallyEqual (const double &a, const std::complex< P > &b, double tol=RTraits< typename Narrowest< P, double >::Precision >::getDefaultTolerance())
 Test whether a complex number is approximately equal to a particular real double. More...
 
template<class P >
bool SimTK::isNumericallyEqual (const std::complex< P > &a, const long double &b, double tol=RTraits< P >::getDefaultTolerance())
 Test whether a complex number is approximately equal to a particular real long double. More...
 
template<class P >
bool SimTK::isNumericallyEqual (const long double &a, const std::complex< P > &b, double tol=RTraits< P >::getDefaultTolerance())
 Test whether a complex number is approximately equal to a particular real long double. More...
 
template<class P >
bool SimTK::isNumericallyEqual (const std::complex< P > &a, int b, double tol=RTraits< P >::getDefaultTolerance())
 Test whether a complex number is approximately equal to a particular integer. More...
 
template<class P >
bool SimTK::isNumericallyEqual (int a, const std::complex< P > &b, double tol=RTraits< P >::getDefaultTolerance())
 Test whether a complex number is approximately equal to a particular integer. More...
 
template<class P >
bool SimTK::isNumericallyEqual (const conjugate< P > &a, const float &b, double tol=RTraits< float >::getDefaultTolerance())
 Test whether a conjugate number is approximately equal to a particular real float. More...
 
template<class P >
bool SimTK::isNumericallyEqual (const float &a, const conjugate< P > &b, double tol=RTraits< float >::getDefaultTolerance())
 Test whether a conjugate number is approximately equal to a particular real float. More...
 
template<class P >
bool SimTK::isNumericallyEqual (const conjugate< P > &a, const double &b, double tol=RTraits< typename Narrowest< P, double >::Precision >::getDefaultTolerance())
 Test whether a conjugate number is approximately equal to a particular real double. More...
 
template<class P >
bool SimTK::isNumericallyEqual (const double &a, const conjugate< P > &b, double tol=RTraits< typename Narrowest< P, double >::Precision >::getDefaultTolerance())
 Test whether a conjugate number is approximately equal to a particular real double. More...
 
template<class P >
bool SimTK::isNumericallyEqual (const conjugate< P > &a, const long double &b, double tol=RTraits< P >::getDefaultTolerance())
 Test whether a conjugate number is approximately equal to a particular real long double. More...
 
template<class P >
bool SimTK::isNumericallyEqual (const long double &a, const conjugate< P > &b, double tol=RTraits< P >::getDefaultTolerance())
 Test whether a conjugate number is approximately equal to a particular real long double. More...
 
template<class P >
bool SimTK::isNumericallyEqual (const conjugate< P > &a, int b, double tol=RTraits< P >::getDefaultTolerance())
 Test whether a conjugate number is approximately equal to a particular integer. More...
 
template<class P >
bool SimTK::isNumericallyEqual (int a, const conjugate< P > &b, double tol=RTraits< P >::getDefaultTolerance())
 Test whether a conjugate number is approximately equal to a particular integer. More...
 

Detailed Description

isNumericallyEqual(x,y) compares two scalar types using a tolerance (default or explicitly specified) and returns true if they are close enough.

The default tolerance used is the NTraits<P>::getSignificant() value (about 1e-14 in double precision, 1e-6 in float) for the narrower of the types being compared but you can override that. The tolerance is both a relative and absolute tolerance; for two numbers a and b and tolerance tol we compute the following condition:

     scale = max(|a|,|b|,1)
     isNumericallyEqual = |a-b| <= scale*tol

For complex or conjugate numbers we insist that both the real and imaginary parts independently satisfy the above condition.

Mixed precision
We support mixed argument types here in which case the default tolerance used is the one appropriate to the lower-precision argument. When one argument is an integer, the default tolerance used is that of the floating point argument. Comparisons may be performed at a higher precision than the tolerance to avoid incorrect truncation; for example an int cannot generally be contained in a float so an int/float comparison is done as double/double, but to float tolerance.
Treatment of NaN
When both arguments are NaN they are considered equal here, which is different than the behavior of the IEEE-sanctioned "==" comparison for which NaN==NaN returns false. If only one argument is NaN we return false. When comparing complex or conjugate numbers the real and imaginary parts are tested separately, so (NaN,0) and (0,NaN) don't test equal despite the fact that isNaN() would return true for both of them. We don't distinguish among *types* of NaNs, though, so NaN and -NaN (if there is such a thing) will test numerically equal.

Function Documentation

bool SimTK::isNumericallyEqual ( const float &  a,
const float &  b,
double  tol = RTraits<float>::getDefaultTolerance() 
)
inline

Compare two floats for approximate equality.

bool SimTK::isNumericallyEqual ( const double &  a,
const double &  b,
double  tol = RTraits<double>::getDefaultTolerance() 
)
inline

Compare two doubles for approximate equality.

bool SimTK::isNumericallyEqual ( const long double &  a,
const long double &  b,
double  tol = RTraits<long double>::getDefaultTolerance() 
)
inline

Compare two long doubles for approximate equality.

bool SimTK::isNumericallyEqual ( const float &  a,
const double &  b,
double  tol = RTraits<float>::getDefaultTolerance() 
)
inline

Compare a float and a double for approximate equality at float precision.

bool SimTK::isNumericallyEqual ( const double &  a,
const float &  b,
double  tol = RTraits<float>::getDefaultTolerance() 
)
inline

Compare a float and a double for approximate equality at float precision.

bool SimTK::isNumericallyEqual ( const float &  a,
const long double &  b,
double  tol = RTraits<float>::getDefaultTolerance() 
)
inline

Compare a float and a long double for approximate equality at float precision.

bool SimTK::isNumericallyEqual ( const long double &  a,
const float &  b,
double  tol = RTraits<float>::getDefaultTolerance() 
)
inline

Compare a float and a long double for approximate equality at float precision.

bool SimTK::isNumericallyEqual ( const double &  a,
const long double &  b,
double  tol = RTraits<double>::getDefaultTolerance() 
)
inline

Compare a double and a long double for approximate equality at double precision.

bool SimTK::isNumericallyEqual ( const long double &  a,
const double &  b,
double  tol = RTraits<double>::getDefaultTolerance() 
)
inline

Compare a double and a long double for approximate equality at double precision.

bool SimTK::isNumericallyEqual ( const float &  a,
int  b,
double  tol = RTraits<float>::getDefaultTolerance() 
)
inline

Test a float for approximate equality to an integer.

bool SimTK::isNumericallyEqual ( int  a,
const float &  b,
double  tol = RTraits<float>::getDefaultTolerance() 
)
inline

Test a float for approximate equality to an integer.

bool SimTK::isNumericallyEqual ( const double &  a,
int  b,
double  tol = RTraits<double>::getDefaultTolerance() 
)
inline

Test a double for approximate equality to an integer.

bool SimTK::isNumericallyEqual ( int  a,
const double &  b,
double  tol = RTraits<double>::getDefaultTolerance() 
)
inline

Test a double for approximate equality to an integer.

bool SimTK::isNumericallyEqual ( const long double &  a,
int  b,
double  tol = RTraits<long double>::getDefaultTolerance() 
)
inline

Test a long double for approximate equality to an integer.

bool SimTK::isNumericallyEqual ( int  a,
const long double &  b,
double  tol = RTraits<long double>::getDefaultTolerance() 
)
inline

Test a long double for approximate equality to an integer.

template<class P , class Q >
bool SimTK::isNumericallyEqual ( const std::complex< P > &  a,
const std::complex< Q > &  b,
double  tol = RTraits<typename Narrowest<P,Q>::Precision>::getDefaultTolerance() 
)
inline

Compare two complex numbers for approximate equality, using the numerical accuracy expectation of the narrower of the two precisions in the case of mixed precision.

template<class P , class Q >
bool SimTK::isNumericallyEqual ( const conjugate< P > &  a,
const conjugate< Q > &  b,
double  tol = RTraits<typename Narrowest<P,Q>::Precision>::getDefaultTolerance() 
)
inline

Compare two conjugate numbers for approximate equality, using the numerical accuracy expectation of the narrower of the two precisions in the case of mixed precision.

template<class P , class Q >
bool SimTK::isNumericallyEqual ( const std::complex< P > &  a,
const conjugate< Q > &  b,
double  tol = RTraits<typename Narrowest<P,Q>::Precision>::getDefaultTolerance() 
)
inline

Compare a complex and a conjugate number for approximate equality, using the numerical accuracy expectation of the narrower of the two precisions in the case of mixed precision.

template<class P , class Q >
bool SimTK::isNumericallyEqual ( const conjugate< P > &  a,
const std::complex< Q > &  b,
double  tol = RTraits<typename Narrowest<P,Q>::Precision>::getDefaultTolerance() 
)
inline

Compare a complex and a conjugate number for approximate equality, using the numerical accuracy expectation of the narrower of the two precisions in the case of mixed precision.

template<class P >
bool SimTK::isNumericallyEqual ( const std::complex< P > &  a,
const float &  b,
double  tol = RTraits<float>::getDefaultTolerance() 
)
inline

Test whether a complex number is approximately equal to a particular real float.

template<class P >
bool SimTK::isNumericallyEqual ( const float &  a,
const std::complex< P > &  b,
double  tol = RTraits<float>::getDefaultTolerance() 
)
inline

Test whether a complex number is approximately equal to a particular real float.

template<class P >
bool SimTK::isNumericallyEqual ( const std::complex< P > &  a,
const double &  b,
double  tol = RTraits<typename Narrowest<P,double>::Precision>::getDefaultTolerance() 
)
inline

Test whether a complex number is approximately equal to a particular real double.

template<class P >
bool SimTK::isNumericallyEqual ( const double &  a,
const std::complex< P > &  b,
double  tol = RTraits<typename Narrowest<P,double>::Precision>::getDefaultTolerance() 
)
inline

Test whether a complex number is approximately equal to a particular real double.

template<class P >
bool SimTK::isNumericallyEqual ( const std::complex< P > &  a,
const long double &  b,
double  tol = RTraits<P>::getDefaultTolerance() 
)
inline

Test whether a complex number is approximately equal to a particular real long double.

template<class P >
bool SimTK::isNumericallyEqual ( const long double &  a,
const std::complex< P > &  b,
double  tol = RTraits<P>::getDefaultTolerance() 
)
inline

Test whether a complex number is approximately equal to a particular real long double.

template<class P >
bool SimTK::isNumericallyEqual ( const std::complex< P > &  a,
int  b,
double  tol = RTraits<P>::getDefaultTolerance() 
)
inline

Test whether a complex number is approximately equal to a particular integer.

template<class P >
bool SimTK::isNumericallyEqual ( int  a,
const std::complex< P > &  b,
double  tol = RTraits<P>::getDefaultTolerance() 
)
inline

Test whether a complex number is approximately equal to a particular integer.

template<class P >
bool SimTK::isNumericallyEqual ( const conjugate< P > &  a,
const float &  b,
double  tol = RTraits<float>::getDefaultTolerance() 
)
inline

Test whether a conjugate number is approximately equal to a particular real float.

template<class P >
bool SimTK::isNumericallyEqual ( const float &  a,
const conjugate< P > &  b,
double  tol = RTraits<float>::getDefaultTolerance() 
)
inline

Test whether a conjugate number is approximately equal to a particular real float.

template<class P >
bool SimTK::isNumericallyEqual ( const conjugate< P > &  a,
const double &  b,
double  tol = RTraits<typename Narrowest<P,double>::Precision>::getDefaultTolerance() 
)
inline

Test whether a conjugate number is approximately equal to a particular real double.

template<class P >
bool SimTK::isNumericallyEqual ( const double &  a,
const conjugate< P > &  b,
double  tol = RTraits<typename Narrowest<P,double>::Precision>::getDefaultTolerance() 
)
inline

Test whether a conjugate number is approximately equal to a particular real double.

template<class P >
bool SimTK::isNumericallyEqual ( const conjugate< P > &  a,
const long double &  b,
double  tol = RTraits<P>::getDefaultTolerance() 
)
inline

Test whether a conjugate number is approximately equal to a particular real long double.

template<class P >
bool SimTK::isNumericallyEqual ( const long double &  a,
const conjugate< P > &  b,
double  tol = RTraits<P>::getDefaultTolerance() 
)
inline

Test whether a conjugate number is approximately equal to a particular real long double.

template<class P >
bool SimTK::isNumericallyEqual ( const conjugate< P > &  a,
int  b,
double  tol = RTraits<P>::getDefaultTolerance() 
)
inline

Test whether a conjugate number is approximately equal to a particular integer.

template<class P >
bool SimTK::isNumericallyEqual ( int  a,
const conjugate< P > &  b,
double  tol = RTraits<P>::getDefaultTolerance() 
)
inline

Test whether a conjugate number is approximately equal to a particular integer.