pp_pi.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //[preprocessed_pi
  2. // Preprocessed pi constant, annotated.
  3. namespace boost
  4. {
  5. namespace math
  6. {
  7. namespace constants
  8. {
  9. namespace detail
  10. {
  11. template <class T> struct constant_pi
  12. {
  13. private:
  14. // Default implementations from string of decimal digits:
  15. static inline T get_from_string()
  16. {
  17. static const T result
  18. = detail::convert_from_string<T>("3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651e+00",
  19. boost::is_convertible<const char*, T>());
  20. return result;
  21. }
  22. template <int N> static T compute();
  23. public:
  24. // Default implementations from string of decimal digits:
  25. static inline T get(const mpl::int_<construct_from_string>&)
  26. {
  27. constant_initializer<T, & constant_pi<T>::get_from_string >::do_nothing();
  28. return get_from_string();
  29. }
  30. // Float, double and long double versions:
  31. static inline T get(const mpl::int_<construct_from_float>)
  32. {
  33. return 3.141592653589793238462643383279502884e+00F;
  34. }
  35. static inline T get(const mpl::int_<construct_from_double>&)
  36. {
  37. return 3.141592653589793238462643383279502884e+00;
  38. }
  39. static inline T get(const mpl::int_<construct_from_long_double>&)
  40. {
  41. return 3.141592653589793238462643383279502884e+00L;
  42. }
  43. // For very high precision that is nonetheless can be calculated at compile time:
  44. template <int N> static inline T get(const mpl::int_<N>& n)
  45. {
  46. constant_initializer2<T, N, & constant_pi<T>::template compute<N> >::do_nothing();
  47. return compute<N>();
  48. }
  49. //For true arbitrary precision, which may well vary at runtime.
  50. static inline T get(const mpl::int_<0>&)
  51. {
  52. return tools::digits<T>() > max_string_digits ? compute<0>() : get(mpl::int_<construct_from_string>());
  53. }
  54. }; // template <class T> struct constant_pi
  55. } // namespace detail
  56. // The actual forwarding function (including policy to control precision).
  57. template <class T, class Policy> inline T pi( )
  58. {
  59. return detail:: constant_pi<T>::get(typename construction_traits<T, Policy>::type());
  60. }
  61. // The actual forwarding function (using default policy to control precision).
  62. template <class T> inline T pi()
  63. {
  64. return pi<T, boost::math::policies::policy<> >()
  65. }
  66. } // namespace constants
  67. // Namespace specific versions, for the three built-in floats:
  68. namespace float_constants
  69. {
  70. static const float pi = 3.141592653589793238462643383279502884e+00F;
  71. }
  72. namespace double_constants
  73. {
  74. static const double pi = 3.141592653589793238462643383279502884e+00;
  75. }
  76. namespace long_double_constants
  77. {
  78. static const long double pi = 3.141592653589793238462643383279502884e+00L;
  79. }
  80. namespace constants{;
  81. } // namespace constants
  82. } // namespace math
  83. } // namespace boost
  84. //] [/preprocessed_pi]
  85. /*
  86. Copyright 2012 John Maddock and Paul A. Bristow.
  87. Distributed under the Boost Software License, Version 1.0.
  88. (See accompanying file LICENSE_1_0.txt or copy at
  89. http://www.boost.org/LICENSE_1_0.txt)
  90. */