Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards


Class template close_at_tolerance

boost::math::fpc::close_at_tolerance — Predicate for comparing floating point numbers.


// In header: <boost/test/tools/floating_point_comparison.hpp>

template<typename FPT> 
class close_at_tolerance {
  // types
  typedef bool result_type;

  // construct/copy/destruct
  template<typename ToleranceType> 
    explicit close_at_tolerance(ToleranceType, fpc::strength = FPC_STRONG);

  // public member functions
  FPT fraction_tolerance() const;
  fpc::strength strength() const;
  FPT tested_rel_diff() const;
  bool operator()(FPT, FPT) const;


This predicate is used to compare floating point numbers. In addition the comparison produces maximum related difference, which can be used to generate detailed error message The methods for comparing floating points are detailed in the documentation. The method is chosen by the boost::math::fpc::strength given at construction.

This predicate is not suitable for comparing to 0 or to infinity.

close_at_tolerance public construct/copy/destruct

  1. template<typename ToleranceType> 
      explicit close_at_tolerance(ToleranceType tolerance, 
                                  fpc::strength fpc_strength = FPC_STRONG);

close_at_tolerance public member functions

  1. FPT fraction_tolerance() const;
    Returns the tolerance.
  2. fpc::strength strength() const;
    Returns the comparison method.
  3. FPT tested_rel_diff() const;
    Returns the failing fraction.
  4. bool operator()(FPT left, FPT right) const;

    Compares two floating point numbers a and b such that their "left" relative difference |a-b|/a and/or "right" relative difference |a-b|/b does not exceed specified relative (fraction) tolerance.

    What is reported by tested_rel_diff in case of failure depends on the comparison method:

    • for FPC_STRONG: the max of the two fractions

    • for FPC_WEAK: the min of the two fractions The rationale behind is to report the tolerance to set in order to make a test pass.



    first floating point number to be compared


    second floating point number to be compared