|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r54855 - sandbox/numeric_adaptor/boost/numeric_adaptor
From: bruno.lalande_at_[hidden]
Date: 2009-07-10 09:40:32
Author: bruno.lalande
Date: 2009-07-10 09:40:30 EDT (Fri, 10 Jul 2009)
New Revision: 54855
URL: http://svn.boost.org/trac/boost/changeset/54855
Log:
Mathematical functions now act on policies, not on values.
Text files modified:
sandbox/numeric_adaptor/boost/numeric_adaptor/cln_policy.hpp | 28 ++++++------
sandbox/numeric_adaptor/boost/numeric_adaptor/default_policy.hpp | 71 +++++++++++++++---------------
sandbox/numeric_adaptor/boost/numeric_adaptor/gmp_policy.hpp | 60 ++++++++++++++-----------
sandbox/numeric_adaptor/boost/numeric_adaptor/ieee_policy.hpp | 20 ++++++--
sandbox/numeric_adaptor/boost/numeric_adaptor/numeric_adaptor.hpp | 92 +++++++++++++++++----------------------
5 files changed, 138 insertions(+), 133 deletions(-)
Modified: sandbox/numeric_adaptor/boost/numeric_adaptor/cln_policy.hpp
==============================================================================
--- sandbox/numeric_adaptor/boost/numeric_adaptor/cln_policy.hpp (original)
+++ sandbox/numeric_adaptor/boost/numeric_adaptor/cln_policy.hpp 2009-07-10 09:40:30 EDT (Fri, 10 Jul 2009)
@@ -49,40 +49,40 @@
//value = cln::cl_float(atof(v.c_str()), cln::float_format(256));
}
- static inline void abs(value_type& r, value_type const& a)
+ static inline void abs(cln_policy& r, cln_policy const& a)
{
- r = cln::abs(a);
+ r.value = cln::abs(a.value);
}
- static inline void sqrt(value_type& r, value_type const& a)
+ static inline void sqrt(cln_policy& r, cln_policy const& a)
{
- r = cln::sqrt(a);
+ r.value = cln::sqrt(a.value);
}
- static inline void cos(value_type& r, value_type const& a)
+ static inline void cos(cln_policy& r, cln_policy const& a)
{
- r = cln::cos(a);
+ r.value = cln::cos(a.value);
}
- static inline void sin(value_type& r, value_type const& a)
+ static inline void sin(cln_policy& r, cln_policy const& a)
{
- r = cln::sin(a);
+ r.value = cln::sin(a.value);
}
- static inline void tan(value_type& r, value_type const& a)
+ static inline void tan(cln_policy& r, cln_policy const& a)
{
- r = cln::tan(a);
+ r.value = cln::tan(a.value);
}
- static inline void atan(value_type& r, value_type const& a)
+ static inline void atan(cln_policy& r, cln_policy const& a)
{
- r = cln::atan(a);
+ r.value = cln::atan(a.value);
}
- static inline void hypot(value_type& r, value_type const& a, value_type const& b)
+ static inline void hypot(cln_policy& r, cln_policy const& a, cln_policy const& b)
{
- r = cln::sqrt(a * a + b * b);
+ r.value = cln::sqrt(a.value * a.value + b.value * b.value);
}
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-07-10 09:40:30 EDT (Fri, 10 Jul 2009)
@@ -22,8 +22,6 @@
{
typedef T value_type;
- value_type value;
-
// Default no initialization or pre-destruction is necessary
static inline void init(T& value) {}
static inline void destruct(T& value) {}
@@ -32,53 +30,53 @@
static inline void copy(T const& source, T& dest) { dest = source; }
// The default policy uses the default operators +, -, *, /
- static inline void add(value_type& r, const value_type& a, const value_type& b)
- { r = a + b; }
- static inline void add(value_type& a, const value_type& b)
- { a += b; }
- static inline void subtract(value_type& r, const value_type& a, const value_type& b)
- { r = a - b; }
- static inline void subtract(value_type& a, const value_type& b)
- { a -= b; }
- static inline void multiply(value_type& r, const value_type& a, const value_type& b)
- { r = a * b; }
- static inline void multiply(value_type& a, const value_type& b)
- { a *= b; }
- static inline void divide(value_type& r, const value_type& a, const value_type& b)
- { r = a / b; }
- static inline void divide(value_type& a, const value_type& b)
- { a /= b; }
- static inline void neg(value_type& r, const value_type& n)
- { r = -n; }
+ static inline void add(default_policy& r, const default_policy& a, const default_policy& b)
+ { r.value = a.value + b.value; }
+ static inline void add(default_policy& a, const default_policy& b)
+ { a.value += b.value; }
+ static inline void subtract(default_policy& r, const default_policy& a, const default_policy& b)
+ { r.value = a.value - b.value; }
+ static inline void subtract(default_policy& a, const default_policy& b)
+ { a.value -= b.value; }
+ static inline void multiply(default_policy& r, const default_policy& a, const default_policy& b)
+ { r.value = a.value * b.value; }
+ static inline void multiply(default_policy& a, const default_policy& b)
+ { a.value *= b.value; }
+ static inline void divide(default_policy& r, const default_policy& a, const default_policy& b)
+ { r.value = a.value / b.value; }
+ static inline void divide(default_policy& a, const default_policy& b)
+ { a.value /= b.value; }
+ static inline void neg(default_policy& r, const default_policy& n)
+ { r.value = -n.value; }
- static inline void cos(value_type& r, value_type const& a)
+ static inline void cos(default_policy& r, default_policy const& a)
{
- double d = Derived::template big_numeric_cast<double>(a);
- Derived::set(r, std::cos(d));
+ double d = Derived::template big_numeric_cast<double>(static_cast<const Derived&>(a));
+ Derived::set(r.value, std::cos(d));
}
- static inline void sin(value_type& r, value_type const& a)
+ static inline void sin(default_policy& r, default_policy const& a)
{
- double d = Derived::template big_numeric_cast<double>(a);
- Derived::set(r, std::sin(d));
+ double d = Derived::template big_numeric_cast<double>(static_cast<const Derived&>(a));
+ Derived::set(r.value, std::sin(d));
}
- static inline void tan(value_type& r, value_type const& a)
+ static inline void tan(default_policy& r, default_policy const& a)
{
- double d = Derived::template big_numeric_cast<double>(a);
- Derived::set(r, std::tan(d));
+ double d = Derived::template big_numeric_cast<double>(static_cast<const Derived&>(a));
+ Derived::set(r.value, std::tan(d));
}
- static inline void atan(value_type& r, value_type const& a)
+ static inline void atan(default_policy& r, default_policy const& a)
{
- double d = Derived::template big_numeric_cast<double>(a);
- Derived::set(r, std::atan(d));
+ double d = Derived::template big_numeric_cast<double>(static_cast<const Derived&>(a));
+ Derived::set(r.value, std::atan(d));
}
- static inline void sqrt(value_type& r, value_type const& a)
+ static inline void sqrt(default_policy& r, default_policy const& a)
{
- double d = Derived::template big_numeric_cast<double>(a);
- Derived::set(r, std::sqrt(d));
+ double d = Derived::template big_numeric_cast<double>(static_cast<const Derived&>(a));
+ Derived::set(r.value, std::sqrt(d));
}
// Default use the comparison operators
@@ -93,6 +91,9 @@
out << std::setprecision(20) << a;
return out.str();
}
+
+protected:
+ value_type value;
};
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-07-10 09:40:30 EDT (Fri, 10 Jul 2009)
@@ -51,69 +51,69 @@
// TODO should we add specific overloads for function like mpf_add_ui?
- static inline void add(value_type& r, value_type const& a, value_type const& b)
+ static inline void add(gmp_policy& r, gmp_policy const& a, gmp_policy const& b)
{
- mpf_add(r, a, b);
+ mpf_add(r.value, a.value, b.value);
}
- static inline void add(value_type& a, value_type const& b)
+ static inline void add(gmp_policy& a, gmp_policy const& b)
{
- mpf_add(a, a, b);
+ mpf_add(a.value, a.value, b.value);
}
- static inline void subtract(value_type& r, value_type const& a, value_type const& b)
+ static inline void subtract(gmp_policy& r, gmp_policy const& a, gmp_policy const& b)
{
- mpf_sub(r, a, b);
+ mpf_sub(r.value, a.value, b.value);
}
- static inline void subtract(value_type& a, value_type const& b)
+ static inline void subtract(gmp_policy& a, gmp_policy const& b)
{
- mpf_sub(a, a, b);
+ mpf_sub(a.value, a.value, b.value);
}
- static inline void multiply(value_type& r, value_type const& a, value_type const& b)
+ static inline void multiply(gmp_policy& r, gmp_policy const& a, gmp_policy const& b)
{
- mpf_mul(r, a, b);
+ mpf_mul(r.value, a.value, b.value);
}
- static inline void multiply(value_type& a, value_type const& b)
+ static inline void multiply(gmp_policy& a, gmp_policy const& b)
{
- mpf_mul(a, a, b);
+ mpf_mul(a.value, a.value, b.value);
}
- static inline void divide(value_type& r, value_type const& a, value_type const& b)
+ static inline void divide(gmp_policy& r, gmp_policy const& a, gmp_policy const& b)
{
- mpf_div(r, a, b);
+ mpf_div(r.value, a.value, b.value);
}
- static inline void divide(value_type& a, value_type const& b)
+ static inline void divide(gmp_policy& a, gmp_policy const& b)
{
- mpf_div(a, a, b);
+ mpf_div(a.value, a.value, b.value);
}
- static inline void neg(value_type& r, value_type const& n)
+ static inline void neg(gmp_policy& r, gmp_policy const& n)
{
- mpf_neg(r, n);
+ mpf_neg(r.value, n.value);
}
- static inline void abs(value_type& r, value_type const& a)
+ static inline void abs(gmp_policy& r, gmp_policy const& a)
{
- mpf_abs(r, a);
+ mpf_abs(r.value, a.value);
}
- static inline void sqrt(value_type& r, value_type const& a)
+ static inline void sqrt(gmp_policy& r, gmp_policy const& a)
{
- mpf_sqrt(r, a);
+ mpf_sqrt(r.value, a.value);
}
- static inline void hypot(value_type& r, value_type const& a, value_type const& b)
+ static inline void hypot(gmp_policy& r, gmp_policy const& a, gmp_policy const& b)
{
- mpf_mul(r, a, a);
+ mpf_mul(r.value, a.value, a.value);
value_type t;
mpf_init(t);
- mpf_mul(t, b, b);
- mpf_add(t, r, t);
- mpf_sqrt(r, t);
+ mpf_mul(t, b.value, b.value);
+ mpf_add(t, r.value, t);
+ mpf_sqrt(r.value, t);
mpf_clear(t);
}
@@ -124,6 +124,12 @@
return mpf_get_d(b);
}
+ template <typename OtherType>
+ static inline OtherType big_numeric_cast(gmp_policy const& b)
+ {
+ return mpf_get_d(b.value);
+ }
+
static inline std::string as_string(value_type const& a)
{
Modified: sandbox/numeric_adaptor/boost/numeric_adaptor/ieee_policy.hpp
==============================================================================
--- sandbox/numeric_adaptor/boost/numeric_adaptor/ieee_policy.hpp (original)
+++ sandbox/numeric_adaptor/boost/numeric_adaptor/ieee_policy.hpp 2009-07-10 09:40:30 EDT (Fri, 10 Jul 2009)
@@ -32,14 +32,18 @@
typedef T value_type;
template <typename OtherType>
- static inline void set(value_type& value, OtherType const& v) { value = v; } //boost::numeric_cast<T>(v); }
+ static inline void set(value_type& value, OtherType const& v)
+ { value = v; } //boost::numeric_cast<T>(v); }
- static inline void set(value_type& value, std::string const& v) { value = boost::lexical_cast<T>(v); }
+ static inline void set(value_type& value, std::string const& v)
+ { value = boost::lexical_cast<T>(v); }
- static inline void abs(value_type& r, value_type const& a) { r = std::abs(a); }
- static inline void hypot(value_type& r, value_type const& a, value_type const& b)
+ static inline void abs(ieee_policy& r, ieee_policy const& a)
+ { r.value = std::abs(a.value); }
+
+ static inline void hypot(ieee_policy& r, ieee_policy const& a, ieee_policy const& b)
{
- r = boost::math::hypot(a, b);
+ r.value = boost::math::hypot(a.value, b.value);
}
template <typename OtherType>
@@ -47,6 +51,12 @@
{
return boost::numeric_cast<OtherType>(v);
}
+
+ template <typename OtherType>
+ static inline OtherType big_numeric_cast(ieee_policy const& v)
+ {
+ return boost::numeric_cast<OtherType>(v.value);
+ }
};
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-07-10 09:40:30 EDT (Fri, 10 Jul 2009)
@@ -130,15 +130,14 @@
numeric_adaptor<Policy> const& a,
numeric_adaptor<Policy> const& b)
{
- typename Policy::value_type r;
- Policy::init(r);
- Policy::add(r, a.value, b.value);
- return numeric_adaptor<Policy>(r, true);
+ numeric_adaptor<Policy> r;
+ Policy::add(r, a, b);
+ return r;
}
numeric_adaptor<Policy>& operator+=(numeric_adaptor<Policy> const& other)
{
- Policy::add(Policy::value, other.value);
+ Policy::add(*this, other);
return *this;
}
@@ -146,15 +145,14 @@
numeric_adaptor<Policy> const& a,
numeric_adaptor<Policy> const& b)
{
- typename Policy::value_type r;
- Policy::init(r);
- Policy::multiply(r, a.value, b.value);
- return numeric_adaptor<Policy>(r, true);
+ numeric_adaptor<Policy> r;
+ Policy::multiply(r, a, b);
+ return r;
}
numeric_adaptor<Policy>& operator*=(numeric_adaptor<Policy> const& other)
{
- Policy::multiply(Policy::value, other.value);
+ Policy::multiply(*this, other);
return *this;
}
@@ -162,15 +160,14 @@
numeric_adaptor<Policy> const& a,
numeric_adaptor<Policy> const& b)
{
- typename Policy::value_type r;
- Policy::init(r);
- Policy::subtract(r, a.value, b.value);
- return numeric_adaptor<Policy>(r, true);
+ numeric_adaptor<Policy> r;
+ Policy::subtract(r, a, b);
+ return r;
}
numeric_adaptor<Policy>& operator-=(numeric_adaptor<Policy> const& other)
{
- Policy::subtract(Policy::value, other.value);
+ Policy::subtract(*this, other);
return *this;
}
@@ -178,24 +175,22 @@
numeric_adaptor<Policy> const& a,
numeric_adaptor<Policy> const& b)
{
- typename Policy::value_type r;
- Policy::init(r);
- Policy::divide(r, a.value, b.value);
- return numeric_adaptor<Policy>(r, true);
+ numeric_adaptor<Policy> r;
+ Policy::divide(r, a, b);
+ return r;
}
numeric_adaptor<Policy>& operator/=(numeric_adaptor<Policy> const& other)
{
- Policy::divide(Policy::value, other.value);
+ Policy::divide(*this, other);
return *this;
}
friend inline numeric_adaptor<Policy> operator-(numeric_adaptor<Policy> const& n)
{
- typename Policy::value_type r;
- Policy::init(r);
- Policy::neg(r, n.value);
- return numeric_adaptor<Policy>(r, true);
+ numeric_adaptor<Policy> r;
+ Policy::neg(r, n);
+ return r;
}
// Construct from a policy-type. Bool (or any other signature changing parameter)
@@ -211,55 +206,49 @@
template <typename Policy>
inline numeric_adaptor<Policy> abs(numeric_adaptor<Policy> const& v)
{
- typename Policy::value_type r;
- Policy::init(r);
- Policy::abs(r, v.value);
- return numeric_adaptor<Policy>(r, true);
+ numeric_adaptor<Policy> r;
+ Policy::abs(r, v);
+ return r;
}
template <typename Policy>
inline numeric_adaptor<Policy> sqrt(numeric_adaptor<Policy> const& v)
{
- typename Policy::value_type r;
- Policy::init(r);
- Policy::sqrt(r, v.value);
- return numeric_adaptor<Policy>(r, true);
+ numeric_adaptor<Policy> r;
+ Policy::sqrt(r, v);
+ return r;
}
template <typename Policy>
inline numeric_adaptor<Policy> cos(numeric_adaptor<Policy> const& v)
{
- typename Policy::value_type r;
- Policy::init(r);
- Policy::cos(r, v.value);
- return numeric_adaptor<Policy>(r, true);
+ numeric_adaptor<Policy> r;
+ Policy::cos(r, v);
+ return r;
}
template <typename Policy>
inline numeric_adaptor<Policy> sin(numeric_adaptor<Policy> const& v)
{
- typename Policy::value_type r;
- Policy::init(r);
- Policy::sin(r, v.value);
- return numeric_adaptor<Policy>(r, true);
+ numeric_adaptor<Policy> r;
+ Policy::sin(r, v);
+ return r;
}
template <typename Policy>
inline numeric_adaptor<Policy> tan(numeric_adaptor<Policy> const& v)
{
- typename Policy::value_type r;
- Policy::init(r);
- Policy::tan(r, v.value);
- return numeric_adaptor<Policy>(r, true);
+ numeric_adaptor<Policy> r;
+ Policy::tan(r, v);
+ return r;
}
template <typename Policy>
inline numeric_adaptor<Policy> atan(numeric_adaptor<Policy> const& v)
{
- typename Policy::value_type r;
- Policy::init(r);
- Policy::atan(r, v.value);
- return numeric_adaptor<Policy>(r, true);
+ numeric_adaptor<Policy> r;
+ Policy::atan(r, v);
+ return r;
}
@@ -267,10 +256,9 @@
inline numeric_adaptor<Policy> hypot(numeric_adaptor<Policy> const& a,
numeric_adaptor<Policy> const& b)
{
- typename Policy::value_type r;
- Policy::init(r);
- Policy::hypot(r, a.value, b.value);
- return numeric_adaptor<Policy>(r, true);
+ numeric_adaptor<Policy> r;
+ Policy::hypot(r, a, b);
+ return r;
}
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