
"Alberto Ganesh Barbati" <abarbati@iaanus.com> wrote in message news:e2h7lb$mtt$1@sea.gmane.org...
Andy Little ha scritto:
"Alberto Ganesh Barbati" wrote
bool operator()(T x, T y) const { return abs(x - y) < eps; }
Maybe should be:
bool operator()(T x, T y) const { return abs(x - y) < abs(eps); }
Because I have been caught out when eps was negative ;-) Negaitive eps is easy to do if it is some function of x y or both, which is common e.g percentage etc.
Thanks for pointing that out. I would prefer getting the absolute value of eps once and for all in the constructor, in order to avoid repeated calls to abs() if the functor is invoked several times. eps could be made private to improve data hiding.
However, I am inclined to require eps to be positive as a precondition, leaving to the programmer the responsibility to take the abs() if he deems it necessary. I bet most of the times eps will be positive if not even a compile time constant, so this design would fit in the "don't pay for what you don't use" paradigm.
Many times the "float error less than epsilon" test is used to terminate a successive approximation routine, which will be an infinite loop if eps is negative. Does a negative eps trigger an exception? regards Andy little