|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r53517 - in sandbox/numeric_adaptor: boost/numeric_adaptor libs/numeric_adaptor/test
From: bruno.lalande_at_[hidden]
Date: 2009-05-31 18:35:27
Author: bruno.lalande
Date: 2009-05-31 18:35:26 EDT (Sun, 31 May 2009)
New Revision: 53517
URL: http://svn.boost.org/trac/boost/changeset/53517
Log:
Added a few operators, and a test for all arithmetic operations.
Added:
sandbox/numeric_adaptor/libs/numeric_adaptor/test/test_arithmetic.cpp (contents, props changed)
Text files modified:
sandbox/numeric_adaptor/boost/numeric_adaptor/default_policy.hpp | 6 +++++-
sandbox/numeric_adaptor/boost/numeric_adaptor/gmp_policy.hpp | 27 +++++++++++++++++++++++++++
sandbox/numeric_adaptor/boost/numeric_adaptor/numeric_adaptor.hpp | 32 ++++++++++++++++++++++++++++++++
sandbox/numeric_adaptor/libs/numeric_adaptor/test/Jamroot | 5 +++--
4 files changed, 67 insertions(+), 3 deletions(-)
Modified: sandbox/numeric_adaptor/boost/numeric_adaptor/default_policy.hpp
==============================================================================
--- sandbox/numeric_adaptor/boost/numeric_adaptor/default_policy.hpp (original)
+++ sandbox/numeric_adaptor/boost/numeric_adaptor/default_policy.hpp 2009-05-31 18:35:26 EDT (Sun, 31 May 2009)
@@ -31,10 +31,14 @@
// The default policy uses the default operators +, -, *, /
static inline void add(type& r, const type& a, const type& b) { r = a + b; }
+ static inline void add(type& a, const type& b) { a += b; }
static inline void subtract(type& r, const type& a, const type& b) { r = a - b; }
+ static inline void subtract(type& a, const type& b) { a -= b; }
static inline void multiply(type& r, const type& a, const type& b) { r = a * b; }
+ static inline void multiply(type& a, const type& b) { a *= b; }
static inline void divide(type& r, const type& a, const type& b) { r = a / b; }
-
+ static inline void divide(type& a, const type& b) { a /= b; }
+ static inline void neg(type& r, const type& n) { r = -n; }
// Default use the comparison operators
static inline int compare(T const& a, T const& b)
Modified: sandbox/numeric_adaptor/boost/numeric_adaptor/gmp_policy.hpp
==============================================================================
--- sandbox/numeric_adaptor/boost/numeric_adaptor/gmp_policy.hpp (original)
+++ sandbox/numeric_adaptor/boost/numeric_adaptor/gmp_policy.hpp 2009-05-31 18:35:26 EDT (Sun, 31 May 2009)
@@ -51,26 +51,53 @@
mpf_set(dest, source);
}
+ // TODO should we add specific overloads for function like mpf_add_ui?
+
static inline void add(type& r, type const& a, type const& b)
{
mpf_add(r, a, b);
}
+ static inline void add(type& a, type const& b)
+ {
+ mpf_add(a, a, b);
+ }
+
static inline void subtract(type& r, type const& a, type const& b)
{
mpf_sub(r, a, b);
}
+ static inline void subtract(type& a, type const& b)
+ {
+ mpf_sub(a, a, b);
+ }
+
static inline void multiply(type& r, type const& a, type const& b)
{
mpf_mul(r, a, b);
}
+ static inline void multiply(type& a, type const& b)
+ {
+ mpf_mul(a, a, b);
+ }
+
static inline void divide(type& r, type const& a, type const& b)
{
mpf_div(r, a, b);
}
+ static inline void divide(type& a, type const& b)
+ {
+ mpf_div(a, a, b);
+ }
+
+ static inline void neg(type& r, type const& n)
+ {
+ mpf_neg(r, n);
+ }
+
static inline void abs(type& r, type const& a)
{
mpf_abs(r, a);
Modified: sandbox/numeric_adaptor/boost/numeric_adaptor/numeric_adaptor.hpp
==============================================================================
--- sandbox/numeric_adaptor/boost/numeric_adaptor/numeric_adaptor.hpp (original)
+++ sandbox/numeric_adaptor/boost/numeric_adaptor/numeric_adaptor.hpp 2009-05-31 18:35:26 EDT (Sun, 31 May 2009)
@@ -134,6 +134,12 @@
return numeric_adaptor<Policy>(r, true);
}
+ numeric_adaptor<Policy>& operator+=(numeric_adaptor<Policy> const& other)
+ {
+ Policy::add(value, other.value);
+ return *this;
+ }
+
friend inline numeric_adaptor<Policy> operator*(
numeric_adaptor<Policy> const& a,
numeric_adaptor<Policy> const& b)
@@ -144,6 +150,12 @@
return numeric_adaptor<Policy>(r, true);
}
+ numeric_adaptor<Policy>& operator*=(numeric_adaptor<Policy> const& other)
+ {
+ Policy::multiply(value, other.value);
+ return *this;
+ }
+
friend inline numeric_adaptor<Policy> operator-(
numeric_adaptor<Policy> const& a,
numeric_adaptor<Policy> const& b)
@@ -154,6 +166,12 @@
return numeric_adaptor<Policy>(r, true);
}
+ numeric_adaptor<Policy>& operator-=(numeric_adaptor<Policy> const& other)
+ {
+ Policy::subtract(value, other.value);
+ return *this;
+ }
+
friend inline numeric_adaptor<Policy> operator/(
numeric_adaptor<Policy> const& a,
numeric_adaptor<Policy> const& b)
@@ -164,6 +182,20 @@
return numeric_adaptor<Policy>(r, true);
}
+ numeric_adaptor<Policy>& operator/=(numeric_adaptor<Policy> const& other)
+ {
+ Policy::divide(value, other.value);
+ return *this;
+ }
+
+ friend inline numeric_adaptor<Policy> operator-(numeric_adaptor<Policy> const& n)
+ {
+ typename Policy::type r;
+ Policy::init(r);
+ Policy::neg(r, n.value);
+ return numeric_adaptor<Policy>(r, true);
+ }
+
// Functions
static inline numeric_adaptor<Policy> abs(numeric_adaptor<Policy> const& v)
{
Modified: sandbox/numeric_adaptor/libs/numeric_adaptor/test/Jamroot
==============================================================================
--- sandbox/numeric_adaptor/libs/numeric_adaptor/test/Jamroot (original)
+++ sandbox/numeric_adaptor/libs/numeric_adaptor/test/Jamroot 2009-05-31 18:35:26 EDT (Sun, 31 May 2009)
@@ -14,5 +14,6 @@
import testing ;
-run test_heron.cpp : gmp ;
-run test_conversions.cpp : gmp ;
+run test_heron.cpp ;
+run test_arithmetic.cpp ;
+run test_conversions.cpp ;
Added: sandbox/numeric_adaptor/libs/numeric_adaptor/test/test_arithmetic.cpp
==============================================================================
--- (empty file)
+++ sandbox/numeric_adaptor/libs/numeric_adaptor/test/test_arithmetic.cpp 2009-05-31 18:35:26 EDT (Sun, 31 May 2009)
@@ -0,0 +1,74 @@
+// 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/test/included/test_exec_monitor.hpp>
+#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
+
+
+template <typename Policy>
+void test_all()
+{
+ typedef boost::numeric_adaptor::numeric_adaptor<Policy> num;
+
+ num n1 = 1.0;
+ num n2 = 2.0;
+
+ num n3 = n1 + n2;
+ BOOST_CHECK_EQUAL((double)n3, 3.0);
+ n3 += 4.0;
+ BOOST_CHECK_EQUAL((double)n3, 7.0);
+
+ num n4 = n3 - n1;
+ BOOST_CHECK_EQUAL((double)n4, 6.0);
+ n4 -= 2.0;
+ BOOST_CHECK_EQUAL((double)n4, 4.0);
+
+ num n5 = n4*n3;
+ BOOST_CHECK_EQUAL((double)n5, 28.0);
+ n5 *= n2;
+ BOOST_CHECK_EQUAL((double)n5, 56.0);
+
+ num n6 = n5/n4;
+ BOOST_CHECK_EQUAL((double)n6, 14.0);
+ n6 /= n3;
+ BOOST_CHECK_EQUAL((double)n6, 2.0);
+
+ BOOST_CHECK_EQUAL((double)-n1, -1.0);
+ BOOST_CHECK_EQUAL((double)-n2, -2.0);
+ BOOST_CHECK_EQUAL((double)-n3, -7.0);
+ BOOST_CHECK_EQUAL((double)-n4, -4.0);
+ BOOST_CHECK_EQUAL((double)-n5, -56.0);
+ BOOST_CHECK_EQUAL((double)-n6, -2.0);
+}
+
+int test_main(int, char*[])
+{
+ test_all<boost::numeric_adaptor::ieee_policy<float> >();
+ test_all<boost::numeric_adaptor::ieee_policy<double> >();
+ test_all<boost::numeric_adaptor::ieee_policy<long double> >();
+
+#if defined(HAVE_GMP)
+ test_all<boost::numeric_adaptor::gmp_policy>();
+#endif
+
+#if defined(HAVE_CLN)
+ test_all<boost::numeric_adaptor::cln_policy>();
+#endif
+
+ 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