constants.hpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Boost interval/constants.hpp template implementation file
  2. *
  3. * Copyright 2002 Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion
  4. *
  5. * Distributed under the Boost Software License, Version 1.0.
  6. * (See accompanying file LICENSE_1_0.txt or
  7. * copy at http://www.boost.org/LICENSE_1_0.txt)
  8. */
  9. #ifndef BOOST_NUMERIC_INTERVAL_CONSTANTS_HPP
  10. #define BOOST_NUMERIC_INTERVAL_CONSTANTS_HPP
  11. namespace boost {
  12. namespace numeric {
  13. namespace interval_lib {
  14. namespace constants {
  15. // These constants should be exactly computed.
  16. // Decimal representations wouldn't do it since the standard doesn't
  17. // specify the rounding (even nearest) that should be used.
  18. static const float pi_f_l = 13176794.0f/(1<<22);
  19. static const float pi_f_u = 13176795.0f/(1<<22);
  20. static const double pi_d_l = (3373259426.0 + 273688.0 / (1<<21)) / (1<<30);
  21. static const double pi_d_u = (3373259426.0 + 273689.0 / (1<<21)) / (1<<30);
  22. template<class T> inline T pi_lower() { return 3; }
  23. template<class T> inline T pi_upper() { return 4; }
  24. template<class T> inline T pi_half_lower() { return 1; }
  25. template<class T> inline T pi_half_upper() { return 2; }
  26. template<class T> inline T pi_twice_lower() { return 6; }
  27. template<class T> inline T pi_twice_upper() { return 7; }
  28. template<> inline float pi_lower<float>() { return pi_f_l; }
  29. template<> inline float pi_upper<float>() { return pi_f_u; }
  30. template<> inline float pi_half_lower<float>() { return pi_f_l / 2; }
  31. template<> inline float pi_half_upper<float>() { return pi_f_u / 2; }
  32. template<> inline float pi_twice_lower<float>() { return pi_f_l * 2; }
  33. template<> inline float pi_twice_upper<float>() { return pi_f_u * 2; }
  34. template<> inline double pi_lower<double>() { return pi_d_l; }
  35. template<> inline double pi_upper<double>() { return pi_d_u; }
  36. template<> inline double pi_half_lower<double>() { return pi_d_l / 2; }
  37. template<> inline double pi_half_upper<double>() { return pi_d_u / 2; }
  38. template<> inline double pi_twice_lower<double>() { return pi_d_l * 2; }
  39. template<> inline double pi_twice_upper<double>() { return pi_d_u * 2; }
  40. template<> inline long double pi_lower<long double>() { return pi_d_l; }
  41. template<> inline long double pi_upper<long double>() { return pi_d_u; }
  42. template<> inline long double pi_half_lower<long double>() { return pi_d_l / 2; }
  43. template<> inline long double pi_half_upper<long double>() { return pi_d_u / 2; }
  44. template<> inline long double pi_twice_lower<long double>() { return pi_d_l * 2; }
  45. template<> inline long double pi_twice_upper<long double>() { return pi_d_u * 2; }
  46. } // namespace constants
  47. template<class I> inline
  48. I pi()
  49. {
  50. typedef typename I::base_type T;
  51. return I(constants::pi_lower<T>(),
  52. constants::pi_upper<T>(), true);
  53. }
  54. template<class I> inline
  55. I pi_half()
  56. {
  57. typedef typename I::base_type T;
  58. return I(constants::pi_half_lower<T>(),
  59. constants::pi_half_upper<T>(), true);
  60. }
  61. template<class I> inline
  62. I pi_twice()
  63. {
  64. typedef typename I::base_type T;
  65. return I(constants::pi_twice_lower<T>(),
  66. constants::pi_twice_upper<T>(), true);
  67. }
  68. } // namespace interval_lib
  69. } // namespace numeric
  70. } // namespace boost
  71. #endif // BOOST_NUMERIC_INTERVAL_CONSTANTS_HPP