generate.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #ifndef BOOST_MATH_CONSTANTS_GENERATE_INCLUDED
  6. #define BOOST_MATH_CONSTANTS_GENERATE_INCLUDED
  7. #include <boost/math/constants/constants.hpp>
  8. #include <boost/regex.hpp>
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <sstream>
  12. #ifdef USE_MPFR
  13. #include <boost/math/bindings/mpfr.hpp>
  14. #elif defined(USE_MPREAL)
  15. #include <boost/math/bindings/mpreal.hpp>
  16. #elif defined(USE_CPP_FLOAT)
  17. #include <boost/multiprecision/cpp_dec_float.hpp>
  18. #else
  19. #include <boost/math/bindings/rr.hpp>
  20. #endif
  21. namespace boost{ namespace math{ namespace constants{
  22. #ifdef USE_MPFR
  23. typedef mpfr_class generator_type;
  24. #elif defined(USE_MPREAL)
  25. typedef mpfr::mpreal generator_type;
  26. #elif defined(USE_CPP_FLOAT)
  27. typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float<500> > generator_type;
  28. #else
  29. typedef ntl::RR generator_type;
  30. #endif
  31. inline void print_constant(const char* name, generator_type(*f)(const mpl::int_<0>&))
  32. {
  33. #ifdef USE_MPFR
  34. mpfr_class::set_dprec(((200 + 1) * 1000L) / 301L);
  35. #elif defined(USE_MPREAL)
  36. mpfr::mpreal::set_default_prec(((200 + 1) * 1000L) / 301L);
  37. #elif defined(USE_CPP_FLOAT)
  38. // Nothing to do, precision is already set.
  39. #else
  40. ntl::RR::SetPrecision(((200 + 1) * 1000L) / 301L);
  41. ntl::RR::SetOutputPrecision(102);
  42. #endif
  43. generator_type value = f(boost::mpl::int_<0>());
  44. std::stringstream os;
  45. os << std::setprecision(110) << std::scientific;
  46. os << value;
  47. std::string s = os.str();
  48. static const regex e("([+-]?\\d+(?:\\.\\d{0,36})?)(\\d*)(?:e([+-]?\\d+))?");
  49. smatch what;
  50. if(regex_match(s, what, e))
  51. {
  52. std::cout <<
  53. "BOOST_DEFINE_MATH_CONSTANT(" << name << ", "
  54. << what[1] << "e" << (what[3].length() ? what[3].str() : std::string("0")) << ", "
  55. << "\"" << what[1] << what[2] << "e" << (what[3].length() ? what[3].str() : std::string("0"))
  56. << "\");" << std::endl;
  57. }
  58. else
  59. {
  60. std::cout << "Format of numeric constant was not recognised!!" << std::endl;
  61. }
  62. }
  63. #define BOOST_CONSTANTS_GENERATE(name) \
  64. boost::math::constants::print_constant(#name, \
  65. & boost::math::constants::detail::BOOST_JOIN(constant_, name)<boost::math::constants::generator_type>::get)
  66. }}} // namespaces
  67. #endif // BOOST_MATH_CONSTANTS_GENERATE_INCLUDED