cpp_near_equal

View page source

C++ Check That Values are Nearly Equal

Prototype

namespace cmpad {
   template <class Type> bool near_equal(
      const Type&                x                                           ,
      const Type&                y                                           ,
      const Type                 rel_error                                   ,
      const cmpad::vector<Type>& vec       = cmpad::vector<Type>(0)          ,
      std::ostream&              os        = std::cerr                       )

Nearly Equal

Checks if two values are nearly equal; i.e., if

   std::fabs(x - y) <= abs_error

where the absolute error abs_error is defined below.

Type

is the type of the operands we are checking.

x

is the left operand in the nearly equal check.

y

is the right operand in the nearly equal check.

rel_error

is the relative error used during the comparison.

vec

This vector specifies extra values to include when scaling the relative error.

os

If the values are not nearly equal, a messages is written to this stream describing the comparison failure.

abs_error

  1. scale is the maximum of the absolute value of x, y , and the elements of vec

  2. tiny is the minimum positive normalized value of type Scalar

  3. abs_error is maximum of tiny and rel_error times scale .

xam_near_equal.cpp contains an example and test of this routine.