Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r66800 - in sandbox/math_constants: boost/math/constants libs/math/test
From: john_at_[hidden]
Date: 2010-11-27 13:42:17


Author: johnmaddock
Date: 2010-11-27 13:42:16 EST (Sat, 27 Nov 2010)
New Revision: 66800
URL: http://svn.boost.org/trac/boost/changeset/66800

Log:
Add first cut at calculating the constants when required.
Added:
   sandbox/math_constants/boost/math/constants/calculate_constants.hpp (contents, props changed)
Text files modified:
   sandbox/math_constants/boost/math/constants/constants.hpp | 12 +++++++++---
   sandbox/math_constants/libs/math/test/test_constants.cpp | 2 +-
   2 files changed, 10 insertions(+), 4 deletions(-)

Added: sandbox/math_constants/boost/math/constants/calculate_constants.hpp
==============================================================================
--- (empty file)
+++ sandbox/math_constants/boost/math/constants/calculate_constants.hpp 2010-11-27 13:42:16 EST (Sat, 27 Nov 2010)
@@ -0,0 +1,159 @@
+// Copyright John Maddock 2010.
+// Use, modification and distribution are 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)
+
+#ifndef BOOST_MATH_CALCULATE_CONSTANTS_CONSTANTS_INCLUDED
+#define BOOST_MATH_CALCULATE_CONSTANTS_CONSTANTS_INCLUDED
+
+namespace boost{ namespace math{ namespace constants{
+
+template <class T, int N>
+inline T calculate_pi(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ BOOST_MATH_STD_USING
+ return 2 * acos(static_cast<T>(0));
+}
+
+template <class T, int N>
+inline T calculate_two_pi(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ return 2 * pi<T, policies::policy<policies::digits2<N> > >();
+}
+
+template <class T, int N>
+inline T calculate_one_div_two_pi(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ return 1 / two_pi<T, policies::policy<policies::digits2<N> > >();
+}
+
+template <class T, int N>
+inline T calculate_root_pi(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ BOOST_MATH_STD_USING
+ return sqrt(pi<T, policies::policy<policies::digits2<N> > >());
+}
+
+template <class T, int N>
+inline T calculate_root_half_pi(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ BOOST_MATH_STD_USING
+ return sqrt(pi<T, policies::policy<policies::digits2<N> > >() / 2);
+}
+
+template <class T, int N>
+inline T calculate_root_two_pi(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ BOOST_MATH_STD_USING
+ return sqrt(two_pi<T, policies::policy<policies::digits2<N> > >());
+}
+
+template <class T, int N>
+inline T calculate_root_ln_four(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ BOOST_MATH_STD_USING
+ return sqrt(log(static_cast<T>(4)));
+}
+
+template <class T, int N>
+inline T calculate_e(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ BOOST_MATH_STD_USING
+ return exp(static_cast<T>(1));
+}
+
+template <class T, int N>
+inline T calculate_half(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ return static_cast<T>(1) / static_cast<T>(2);
+}
+
+template <class T, int N>
+inline T calculate_euler(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ return 0;
+}
+
+template <class T, int N>
+inline T calculate_root_two(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ BOOST_MATH_STD_USING
+ return sqrt(static_cast<T>(2));
+}
+
+template <class T, int N>
+inline T calculate_half_root_two(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ BOOST_MATH_STD_USING
+ return sqrt(static_cast<T>(2)) / 2;
+}
+
+template <class T, int N>
+inline T calculate_ln_two(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ BOOST_MATH_STD_USING
+ return log(static_cast<T>(2));
+}
+
+template <class T, int N>
+inline T calculate_ln_ln_two(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ BOOST_MATH_STD_USING
+ return log(log(static_cast<T>(2)));
+}
+
+template <class T, int N>
+inline T calculate_third(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ BOOST_MATH_STD_USING
+ return static_cast<T>(1) / static_cast<T>(3);
+}
+
+template <class T, int N>
+inline T calculate_twothirds(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ BOOST_MATH_STD_USING
+ return static_cast<T>(2) / static_cast<T>(3);
+}
+
+template <class T, int N>
+inline T calculate_pi_minus_three(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ return pi<T, policies::policy<policies::digits2<N> > >() - static_cast<T>(3);
+}
+
+template <class T, int N>
+inline T calculate_four_minus_pi(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ return static_cast<T>(4) - pi<T, policies::policy<policies::digits2<N> > >();
+}
+
+template <class T, int N>
+inline T calculate_pow23_four_minus_pi(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ BOOST_MATH_STD_USING
+ return pow(four_minus_pi<T, policies::policy<policies::digits2<N> > >(), static_cast<T>(1.5));
+}
+
+template <class T, int N>
+inline T calculate_exp_minus_half(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ BOOST_MATH_STD_USING
+ return exp(static_cast<T>(-0.5));
+}
+
+template <class T, int N>
+inline T calculate_one_div_root_two(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ return static_cast<T>(1) / root_two<T, policies::policy<policies::digits2<N> > >();
+}
+
+template <class T, int N>
+inline T calculate_one_div_root_two_pi(const mpl::int_<N>&BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))
+{
+ return static_cast<T>(1) / root_two_pi<T, policies::policy<policies::digits2<N> > >();
+}
+
+}}} // namespaces
+
+#endif // BOOST_MATH_CALCULATE_CONSTANTS_CONSTANTS_INCLUDED

Modified: sandbox/math_constants/boost/math/constants/constants.hpp
==============================================================================
--- sandbox/math_constants/boost/math/constants/constants.hpp (original)
+++ sandbox/math_constants/boost/math/constants/constants.hpp 2010-11-27 13:42:16 EST (Sat, 27 Nov 2010)
@@ -84,7 +84,7 @@
 
    #define BOOST_DEFINE_MATH_CONSTANT(name, x, y, exp)\
    /* Forward declaration of the calculation method, just in case it's not been provided yet */ \
- template <class T> T BOOST_JOIN(calculate_, name)(int BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T)); \
+ template <class T, int N> T BOOST_JOIN(calculate_, name)(const mpl::int_<N>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T)); \
    /* The default implementations come next: */ \
    template <class T> inline T BOOST_JOIN(get_, name)(const mpl::int_<construct_from_string>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE(T))\
    {\
@@ -97,8 +97,8 @@
    { return BOOST_JOIN(x, BOOST_JOIN(e, exp)); }\
    template <class T> inline T BOOST_JOIN(get_, name)(const mpl::int_<construct_from_long_double>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))\
    { return BOOST_JOIN(BOOST_JOIN(x, BOOST_JOIN(e, exp)), L); }\
- template <class T, int N> inline T BOOST_JOIN(get_, name)(const mpl::int_<N>& BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))\
- { static const T result = BOOST_JOIN(calculate_, name)<T>(N ? N : tools::digits<T>()); return result; }\
+ template <class T, int N> inline T BOOST_JOIN(get_, name)(const mpl::int_<N>& n BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(T))\
+ { static const T result = BOOST_JOIN(calculate_, name)<T>(n); return result; }\
    /* The actual forwarding function: */ \
    template <class T, class Policy> inline T name(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(T) BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(Policy))\
    { return BOOST_JOIN(get_, name)<T>(typename construction_traits<T, Policy>::type()); }\
@@ -138,4 +138,10 @@
 } // namespace math
 } // namespace boost
 
+//
+// We deliberately include this after all the declarations above,
+// that way the calculation routines can call on other constants:
+//
+#include <boost/math/constants/calculate_constants.hpp>
+
 #endif // BOOST_MATH_CONSTANTS_CONSTANTS_INCLUDED

Modified: sandbox/math_constants/libs/math/test/test_constants.cpp
==============================================================================
--- sandbox/math_constants/libs/math/test/test_constants.cpp (original)
+++ sandbox/math_constants/libs/math/test/test_constants.cpp 2010-11-27 13:42:16 EST (Sat, 27 Nov 2010)
@@ -232,7 +232,7 @@
 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
    test_spots(0.0L); // Test long double.
 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0582))
- //test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
+ test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
 #endif
 #else
   std::cout << "<note>The long double tests have been disabled on this platform "


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