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