Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r53345 - in sandbox/numeric_adaptor/libs/numeric_adaptor: . example test
From: barend.gehrels_at_[hidden]
Date: 2009-05-28 11:43:40


Author: barendgehrels
Date: 2009-05-28 11:43:39 EDT (Thu, 28 May 2009)
New Revision: 53345
URL: http://svn.boost.org/trac/boost/changeset/53345

Log:
Added Bruno's string conversion solution
Added namespaces
Bugfix in CLN string-assignment (should have precision otherwise too low)
Added sample "heron"
Added "HAVE_xxx" in samples

Added:
   sandbox/numeric_adaptor/libs/numeric_adaptor/example/
   sandbox/numeric_adaptor/libs/numeric_adaptor/example/heron.cpp (contents, props changed)
Text files modified:
   sandbox/numeric_adaptor/libs/numeric_adaptor/sample.cpp | 32 +++++++++++++++++---------------
   sandbox/numeric_adaptor/libs/numeric_adaptor/test/test_heron.cpp | 29 +++++++++++++++++------------
   2 files changed, 34 insertions(+), 27 deletions(-)

Added: sandbox/numeric_adaptor/libs/numeric_adaptor/example/heron.cpp
==============================================================================
--- (empty file)
+++ sandbox/numeric_adaptor/libs/numeric_adaptor/example/heron.cpp 2009-05-28 11:43:39 EDT (Thu, 28 May 2009)
@@ -0,0 +1,66 @@
+// Numeric Adaptor Library testing suite
+//
+// Copyright Barend Gehrels 2009, Geodan Holding B.V. Amsterdam, the Netherlands.
+// Copyright Bruno Lalande 2009
+// Use, modification and distribution is subject to the Boost Software License,
+// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+
+#include <boost/numeric_adaptor/numeric_adaptor.hpp>
+#include <boost/numeric_adaptor/ieee_policy.hpp>
+
+#if defined(HAVE_GMP)
+# include <boost/numeric_adaptor/gmp_policy.hpp>
+#endif
+
+#if defined(HAVE_CLN)
+# include <boost/numeric_adaptor/cln_policy.hpp>
+#endif
+
+
+using namespace boost::numeric_adaptor;
+
+
+template <typename T, typename Policy>
+void heron(std::string const& header, T const& ta, T const& tb, T const& tc)
+{
+ typedef numeric_adaptor<Policy> num;
+
+ num a = ta;
+ num b = tb;
+ num c = tc;
+ num s = (a + b + c) / num(2.0);
+ num C = num::sqrt(s * (s - a) * (s - b) * (s - c));
+ std::cout << header << ": " << "C = " << T(C) << std::endl;
+}
+
+
+int main()
+{
+ try
+ {
+ heron<std::string, ieee_policy<float> > ("float / string", "31622.77662", "0.000023", "31622.77661");
+ heron<std::string, ieee_policy<double> >("double / string", "31622.77662", "0.000023", "31622.77661");
+
+#if defined(HAVE_CLN)
+ heron<std::string, cln_policy>("CLN / string", "31622.77662", "0.000023", "31622.77661");
+#endif
+
+#if defined(HAVE_GMP)
+ heron<std::string, gmp_policy>("GMP / string", "31622.77662", "0.000023", "31622.77661");
+#endif
+
+
+ }
+ catch(std::exception const& e)
+ {
+ std::cout << "Exception: " << e.what() << std::endl;
+ }
+ catch(...)
+ {
+ std::cout << "Exception, unknown" << std::endl;
+ }
+
+ return 0;
+};

Modified: sandbox/numeric_adaptor/libs/numeric_adaptor/sample.cpp
==============================================================================
--- sandbox/numeric_adaptor/libs/numeric_adaptor/sample.cpp (original)
+++ sandbox/numeric_adaptor/libs/numeric_adaptor/sample.cpp 2009-05-28 11:43:39 EDT (Thu, 28 May 2009)
@@ -13,19 +13,18 @@
 #include <boost/numeric_adaptor/numeric_adaptor.hpp>
 #include <boost/numeric_adaptor/ieee_policy.hpp>
 
-#if ! defined(_MSC_VER)
+#if defined(HAVE_GMP)
 # include <boost/numeric_adaptor/gmp_policy.hpp>
+#endif
+
+#if defined(HAVE_CLN)
 # include <boost/numeric_adaptor/cln_policy.hpp>
 #endif
 
-template <typename T, typename N>
-inline T get(N const& bn)
-{
- return bn.template get<T>();
-}
+
 
 template <typename T, typename Num>
-void sample1(const std::string& header, T const& t_a, T const& t_b, T const& t_c)
+void sample1(std::string const& header, T const& t_a, T const& t_b, T const& t_c)
 {
     std::cout << std::endl << "---" << header << std::endl;
     Num a = t_a;
@@ -33,28 +32,28 @@
     Num a_plus_b = a + b;
 
     //T t3 = a_plus_b; // does NOT compile for strings
- std::cout << "a + b: " << a_plus_b.template get<T>() << std::endl;
+ std::cout << "a + b: " << T(a_plus_b) << std::endl;
 
     Num c = t_c;
     Num par = (a + b) * c;
- std::cout << "(a + b) c: " << par.template get<T>() << std::endl;
+ std::cout << "(a + b) c: " << T(par) << std::endl;
     par = a + b * c;
- std::cout << "a + bc: " << par.template get<T>() << std::endl;
+ std::cout << "a + bc: " << T(par) << std::endl;
 
     Num sqrt_a_plus_b = Num::sqrt(a_plus_b);
- std::cout << "sqrt(a+b): " << sqrt_a_plus_b.template get<T>() << std::endl;
+ std::cout << "sqrt(a+b): " << T(sqrt_a_plus_b) << std::endl;
 
     // Calc the hypot function
     Num hypot_b_c = Num::hypot(b, c);
- std::cout << "hypot: " << hypot_b_c.template get<T>() << std::endl;
- std::cout << "hypot2: " << get<T>(Num::sqrt(b * b + c * c)) << std::endl;
+ std::cout << "hypot: " << T(hypot_b_c) << std::endl;
+ std::cout << "hypot2: " << T(Num::sqrt(b * b + c * c)) << std::endl;
 }
 
 
 
 
 template <typename T, typename Num>
-void sample2(const std::string& header, T const& c1, T const& c2, T const& c4)
+void sample2(
 {
     std::cout << std::endl << "---" << header << std::endl;
 
@@ -94,7 +93,7 @@
 void long_double_issue()
 {
     long double ld = 2.0000000002;
- std::cout << "Strange: " << sizeof(long double) << " " << ld << std::endl;
+ std::cout << "Strange: " << sizeof(long double) << " " << double(ld) << std::endl;
     double d = 2.0000000002;
     std::cout << "OK: " << sizeof(double) << " " << d << std::endl;
 }
@@ -102,6 +101,9 @@
 
 int main()
 {
+
+ using namespace boost::numeric_adaptor;
+
     mpf_set_default_prec(128);
     std::cout << std::setprecision(18);
 

Modified: sandbox/numeric_adaptor/libs/numeric_adaptor/test/test_heron.cpp
==============================================================================
--- sandbox/numeric_adaptor/libs/numeric_adaptor/test/test_heron.cpp (original)
+++ sandbox/numeric_adaptor/libs/numeric_adaptor/test/test_heron.cpp 2009-05-28 11:43:39 EDT (Thu, 28 May 2009)
@@ -11,17 +11,20 @@
 #include <boost/test/floating_point_comparison.hpp>
 #include <boost/numeric_adaptor/numeric_adaptor.hpp>
 #include <boost/numeric_adaptor/ieee_policy.hpp>
-#ifndef NO_CLN
-#include <boost/numeric_adaptor/cln_policy.hpp>
+
+#if defined(HAVE_GMP)
+# include <boost/numeric_adaptor/gmp_policy.hpp>
 #endif
 
-#include <boost/numeric_adaptor/gmp_policy.hpp>
+#if defined(HAVE_CLN)
+# include <boost/numeric_adaptor/cln_policy.hpp>
+#endif
 
 
 template <typename Policy>
 double heron()
 {
- typedef numeric_adaptor::numeric_adaptor<Policy> num;
+ typedef boost::numeric_adaptor::numeric_adaptor<Policy> num;
 
     num a = 31622.77662;
     num b = 0.000023;
@@ -32,17 +35,19 @@
 
 int test_main(int, char*[])
 {
- std::cout << sizeof(long double) << std::endl;
-
     double epsilon = 0.0000001;
 
- BOOST_CHECK_CLOSE(heron<numeric_adaptor::ieee_policy<float> >(), 0.0, epsilon);
- BOOST_CHECK_CLOSE(heron<numeric_adaptor::ieee_policy<double> >(), 0.327490532778257, epsilon);
- BOOST_CHECK_CLOSE(heron<numeric_adaptor::ieee_policy<long double> >(), 0.327490459921098, epsilon);
-#ifndef NO_CLN
- BOOST_CHECK_CLOSE(heron<numeric_adaptor::cln_policy>(), 0.32749045994262366, epsilon);
+ BOOST_CHECK_CLOSE(heron<boost::numeric_adaptor::ieee_policy<float> >(), 0.0, epsilon);
+ BOOST_CHECK_CLOSE(heron<boost::numeric_adaptor::ieee_policy<double> >(), 0.327490532778257, epsilon);
+ BOOST_CHECK_CLOSE(heron<boost::numeric_adaptor::ieee_policy<long double> >(), 0.327490459921098, epsilon);
+
+#if defined(HAVE_CLN)
+ BOOST_CHECK_CLOSE(heron<boost::numeric_adaptor::cln_policy>(), 0.32749045994262366, epsilon);
+#endif
+
+#if defined(HAVE_GMP)
+ BOOST_CHECK_CLOSE(heron<boost::numeric_adaptor::gmp_policy>(), 0.327490459942623, epsilon);
 #endif
- BOOST_CHECK_CLOSE(heron<numeric_adaptor::gmp_policy>(), 0.327490459942623, epsilon);
 
     return 0;
 };


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk