| 1 |
// Copyright John Maddock 2010. |
| 2 |
// Use, modification and distribution are subject to the |
| 3 |
// Boost Software License, Version 1.0. (See accompanying file |
| 4 |
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 |
|
| 6 |
#ifndef BOOST_MATH_CONSTANTS_GENERATE_INCLUDED |
| 7 |
#define BOOST_MATH_CONSTANTS_GENERATE_INCLUDED |
| 8 |
|
| 9 |
#include <boost/math/constants/constants.hpp> |
| 10 |
#include <boost/regex.hpp> |
| 11 |
#include <iostream> |
| 12 |
#include <iomanip> |
| 13 |
#include <sstream> |
| 14 |
|
| 15 |
#ifdef USE_MPFR |
| 16 |
#include <boost/math/bindings/mpfr.hpp> |
| 17 |
#elif defined(USE_MPREAL) |
| 18 |
#include <boost/math/bindings/mpreal.hpp> |
| 19 |
#elif defined(USE_CPP_FLOAT) |
| 20 |
#include <boost/multiprecision/cpp_dec_float.hpp> |
| 21 |
#else |
| 22 |
#include <boost/math/bindings/rr.hpp> |
| 23 |
#endif |
| 24 |
|
| 25 |
namespace boost{ namespace math{ namespace constants{ |
| 26 |
|
| 27 |
#ifdef USE_MPFR |
| 28 |
typedef mpfr_class generator_type; |
| 29 |
#elif defined(USE_MPREAL) |
| 30 |
typedef mpfr::mpreal generator_type; |
| 31 |
#elif defined(USE_CPP_FLOAT) |
| 32 |
typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float<500> > generator_type; |
| 33 |
#else |
| 34 |
typedef ntl::RR generator_type; |
| 35 |
#endif |
| 36 |
|
| 37 |
inline void print_constant(const char* name, generator_type(*f)(const std::integral_constant<int, 0>&)) |
| 38 |
{ |
| 39 |
#ifdef USE_MPFR |
| 40 |
mpfr_class::set_dprec(((200 + 1) * 1000L) / 301L); |
| 41 |
#elif defined(USE_MPREAL) |
| 42 |
mpfr::mpreal::set_default_prec(((200 + 1) * 1000L) / 301L); |
| 43 |
#elif defined(USE_CPP_FLOAT) |
| 44 |
// Nothing to do, precision is already set. |
| 45 |
#else |
| 46 |
ntl::RR::SetPrecision(((200 + 1) * 1000L) / 301L); |
| 47 |
ntl::RR::SetOutputPrecision(102); |
| 48 |
#endif |
| 49 |
generator_type value = f(std::integral_constant<int, 0>()); |
| 50 |
std::stringstream os; |
| 51 |
os << std::setprecision(110) << std::scientific; |
| 52 |
os << value; |
| 53 |
std::string s = os.str(); |
| 54 |
static const regex e("([+-]?\\d+(?:\\.\\d{0,36})?)(\\d*)(?:e([+-]?\\d+))?"); |
| 55 |
smatch what; |
| 56 |
if(regex_match(s, what, e)) |
| 57 |
{ |
| 58 |
std::cout << |
| 59 |
"BOOST_DEFINE_MATH_CONSTANT(" << name << ", " |
| 60 |
<< what[1] << "e" << (what[3].length() ? what[3].str() : std::string("0")) << ", " |
| 61 |
<< "\"" << what[1] << what[2] << "e" << (what[3].length() ? what[3].str() : std::string("0")) |
| 62 |
<< "\");" << std::endl; |
| 63 |
} |
| 64 |
else |
| 65 |
{ |
| 66 |
std::cout << "Format of numeric constant was not recognised!!" << std::endl; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
#define BOOST_CONSTANTS_GENERATE(name) \ |
| 71 |
boost::math::constants::print_constant(#name, \ |
| 72 |
& boost::math::constants::detail::BOOST_JOIN(constant_, name)<boost::math::constants::generator_type>::get) |
| 73 |
|
| 74 |
}}} // namespaces |
| 75 |
|
| 76 |
#endif // BOOST_MATH_CONSTANTS_GENERATE_INCLUDED |