Boost logo

Boost :

From: Andy Little (andy_at_[hidden])
Date: 2006-04-24 05:13:10


"Alberto Ganesh Barbati" <abarbati_at_[hidden]> wrote> template <typename T>
> struct compare_absolute_error : std::binary_function<T, T, bool>
> {
> T eps;
> compare_absolute_error(T eps_) : eps(eps_) {}
> bool operator()(T x, T y) const { return abs(x - y) < eps; }
> };

BTW An alternate signature for the function:

template <typename T>
struct compare_absolute_error /*not derived from binary_function!*/ {
 T eps;
compare_absolute_error(T eps_) : eps(eps_) {}

//Signature as thus to prevent too many conversions
template <typename T1, typename T2>
 bool operator()(T1 const & x, T2 const & y) const { return abs(x - y) < eps; }
 };

// ............. possible useage :

compare_absolute_error<int> comp(1);

int pixel_x =100;
float current_pos_x = 100.5;

bool grabbed = comp(current_pos_x, pixel_x);

regards
Andy Little


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk