Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r66905 - in sandbox/math_constants: boost/math/constants libs/math/test
From: john_at_[hidden]
Date: 2010-11-30 12:59:25


Author: johnmaddock
Date: 2010-11-30 12:59:24 EST (Tue, 30 Nov 2010)
New Revision: 66905
URL: http://svn.boost.org/trac/boost/changeset/66905

Log:
Added generator to make adding new constants easier.
Added:
   sandbox/math_constants/boost/math/constants/generate.hpp (contents, props changed)
   sandbox/math_constants/libs/math/test/test_constant_generate.cpp (contents, props changed)

Added: sandbox/math_constants/boost/math/constants/generate.hpp
==============================================================================
--- (empty file)
+++ sandbox/math_constants/boost/math/constants/generate.hpp 2010-11-30 12:59:24 EST (Tue, 30 Nov 2010)
@@ -0,0 +1,63 @@
+// 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_CONSTANTS_GENERATE_INCLUDED
+#define BOOST_MATH_CONSTANTS_GENERATE_INCLUDED
+
+#include <boost/math/constants/constants.hpp>
+#include <boost/regex.hpp>
+#include <iostream>
+#include <iomanip>
+#include <sstream>
+
+#ifdef USE_MPFR
+#include <boost/math/bindings/mpfr.hpp>
+#else
+#include <boost/math/bindings/rr.hpp>
+#endif
+
+namespace boost{ namespace math{ namespace constants{
+
+#ifdef USE_MPFR
+typedef mpfr_class generator_type;
+#else
+typedef ntl::RR generator_type;
+#endif
+
+inline void print_constant(const char* name, generator_type(*f)(const mpl::int_<0>&))
+{
+#ifdef USE_MPFR
+ mpfr_class::set_dprec(((200 + 1) * 1000L) / 301L);
+#else
+ ntl::RR::SetPrecision(((200 + 1) * 1000L) / 301L);
+ ntl::RR::SetOutputPrecision(102);
+#endif
+ generator_type value = f(boost::mpl::int_<0>());
+ std::stringstream os;
+ os << std::setprecision(102) << std::scientific;
+ os << value;
+ std::string s = os.str();
+ static const regex e("([+-]?\\d+(?:\\.\\d{0,36})?)(\\d*)(?:e([+-]?\\d+))?");
+ smatch what;
+ if(regex_match(s, what, e))
+ {
+ std::cout <<
+ "BOOST_DEFINE_MATH_CONSTANT(" << name << ", " << what[1]
+ << ", " << (what[2].length() ? what[2].str() : std::string("0")) << ", "
+ << (what[3].length() ? what[3].str() : std::string("0")) << ");" << std::endl;
+ }
+ else
+ {
+ std::cout << "Format of numeric constant was not recognised!!" << std::endl;
+ }
+}
+
+#define BOOST_CONSTANTS_GENERATE(name) \
+ boost::math::constants::print_constant(#name, \
+ & boost::math::constants::detail::BOOST_JOIN(calculate_, name)<boost::math::constants::generator_type>)
+
+}}} // namespaces
+
+#endif // BOOST_MATH_CONSTANTS_GENERATE_INCLUDED

Added: sandbox/math_constants/libs/math/test/test_constant_generate.cpp
==============================================================================
--- (empty file)
+++ sandbox/math_constants/libs/math/test/test_constant_generate.cpp 2010-11-30 12:59:24 EST (Tue, 30 Nov 2010)
@@ -0,0 +1,15 @@
+// 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)
+
+#include <boost/math/constants/generate.hpp>
+
+int main()
+{
+ BOOST_CONSTANTS_GENERATE(pi);
+ 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