Boost logo

Boost :

From: Alberto Ganesh Barbati (abarbati_at_[hidden])
Date: 2006-04-24 12:47:18


Andy Little ha scritto:
> 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);
>

The idea is nice, but in this particular case I think the "fixed"
version is more suitable. First: the user can be sure about what
approximation is going to be used for the comparison (in the realm of
floating point, it's a big help). Second: knowing the type in advance
allows you to deal correctly with corner cases (infinities, denorm,
etc.). With the member-template approach that you suggest you would have
to handle more cases and it might easily get tedious.

Ganesh


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