lambda : fabs( x - y )< eps

I'm submitting this in case someone easily spots a less verbose representation of fabs( x - y )< eps than the one shown below and would like to share it. Using lambda (or variants) only (I know I can write my own functor class). Thanks. #include <cmath> #include <boost/function.hpp> #include <boost/lambda/bind.hpp> #include <boost/lambda/lambda.hpp> #include <boost/numeric/conversion/bounds.hpp> typedef boost::function<double(double)> f1_; typedef boost::function<double(double, double)> f2_; typedef boost::function<bool(double)> g_; double eps = boost::numeric::bounds<double>::smallest(); f2_ f2 = lambda::bind( f1_( fabs ), lambda::_1 - lambda::_2 ) ; g_ g = ( eps > lambda::_1 ); BOOST_ASSERT( lambda::bind( g, lambda::bind( f2, lambda::_1, lambda::_2 ) )( 0.0, 0 * eps ) ); BOOST_ASSERT( !lambda::bind( g, lambda::bind( f2, lambda::_1, lambda::_2 ) )( 0.0, eps ) );

AMDG On 03/10/2011 04:40 PM, er wrote:
I'm submitting this in case someone easily spots a less verbose representation of fabs( x - y )< eps than the one shown below and would like to share it. Using lambda (or variants) only (I know I can write my own functor class). Thanks.
How about using the attached cmath.hpp for phoenix? #include <cmath> #include <boost/function.hpp> #include <boost/lambda/bind.hpp> #include <boost/lambda/lambda.hpp> #include <boost/phoenix/core.hpp> #include <boost/phoenix/operator.hpp> #include "cmath.hpp" int main() { double eps = 0.000001; using namespace boost::phoenix::arg_names; boost::function<bool(double, double)> f = fabs(_1 - _2) < eps; BOOST_ASSERT( f( 0.0, 0 * eps ) ); BOOST_ASSERT( !f( 0.0, eps ) ); } In Christ, Steven Watanabe

On 3/10/11 11:14 PM, Steven Watanabe wrote:
AMDG
On 03/10/2011 04:40 PM, er wrote:
I'm submitting this in case someone easily spots a less verbose representation of fabs( x - y )< eps than the one shown below and would like to share it. Using lambda (or variants) only (I know I can write my own functor class). Thanks.
How about using the attached cmath.hpp for phoenix?
Thanks, it looks worthwhile.
#include <cmath> #include <boost/function.hpp> #include <boost/lambda/bind.hpp> #include <boost/lambda/lambda.hpp> #include <boost/phoenix/core.hpp> #include <boost/phoenix/operator.hpp> #include "cmath.hpp"
int main() { double eps = 0.000001; using namespace boost::phoenix::arg_names; boost::function<bool(double, double)> f = fabs(_1 - _2) < eps;
BOOST_ASSERT( f( 0.0, 0 * eps ) ); BOOST_ASSERT( !f( 0.0, eps ) ); }
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Fri, Mar 11, 2011 at 5:14 AM, Steven Watanabe <watanabesj@gmail.com> wrote:
AMDG
On 03/10/2011 04:40 PM, er wrote:
I'm submitting this in case someone easily spots a less verbose representation of fabs( x - y )< eps than the one shown below and would like to share it. Using lambda (or variants) only (I know I can write my own functor class). Thanks.
How about using the attached cmath.hpp for phoenix?
#include <cmath> #include <boost/function.hpp> #include <boost/lambda/bind.hpp> #include <boost/lambda/lambda.hpp> #include <boost/phoenix/core.hpp> #include <boost/phoenix/operator.hpp> #include "cmath.hpp"
int main() { double eps = 0.000001; using namespace boost::phoenix::arg_names; boost::function<bool(double, double)> f = fabs(_1 - _2) < eps;
BOOST_ASSERT( f( 0.0, 0 * eps ) ); BOOST_ASSERT( !f( 0.0, eps ) ); }
In Christ, Steven Watanabe
Thanks! This is a nice and welcome contribution! I just committed it under the "stdlib" subdirectory. Thanks, Thomas
participants (3)
-
er
-
Steven Watanabe
-
Thomas Heller