rounding.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Boost interval/rounding.hpp template implementation file
  2. *
  3. * Copyright 2002-2003 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_ROUNDING_HPP
  10. #define BOOST_NUMERIC_INTERVAL_ROUNDING_HPP
  11. namespace boost {
  12. namespace numeric {
  13. namespace interval_lib {
  14. /*
  15. * Default rounding_control class (does nothing)
  16. */
  17. template<class T>
  18. struct rounding_control
  19. {
  20. typedef int rounding_mode;
  21. static void get_rounding_mode(rounding_mode&) {}
  22. static void set_rounding_mode(rounding_mode) {}
  23. static void upward() {}
  24. static void downward() {}
  25. static void to_nearest() {}
  26. static const T& to_int(const T& x) { return x; }
  27. static const T& force_rounding(const T& x) { return x; }
  28. };
  29. /*
  30. * A few rounding control classes (exact/std/opp: see documentation)
  31. * rounded_arith_* control the rounding of the arithmetic operators
  32. * rounded_transc_* control the rounding of the transcendental functions
  33. */
  34. template<class T, class Rounding = rounding_control<T> >
  35. struct rounded_arith_exact;
  36. template<class T, class Rounding = rounding_control<T> >
  37. struct rounded_arith_std;
  38. template<class T, class Rounding = rounding_control<T> >
  39. struct rounded_arith_opp;
  40. template<class T, class Rounding>
  41. struct rounded_transc_dummy;
  42. template<class T, class Rounding = rounded_arith_exact<T> >
  43. struct rounded_transc_exact;
  44. template<class T, class Rounding = rounded_arith_std<T> >
  45. struct rounded_transc_std;
  46. template<class T, class Rounding = rounded_arith_opp<T> >
  47. struct rounded_transc_opp;
  48. /*
  49. * State-saving classes: allow to set and reset rounding control
  50. */
  51. namespace detail {
  52. template<class Rounding>
  53. struct save_state_unprotected: Rounding
  54. {
  55. typedef save_state_unprotected<Rounding> unprotected_rounding;
  56. };
  57. } // namespace detail
  58. template<class Rounding>
  59. struct save_state: Rounding
  60. {
  61. typename Rounding::rounding_mode mode;
  62. save_state() {
  63. this->get_rounding_mode(mode);
  64. this->init();
  65. }
  66. ~save_state() { this->set_rounding_mode(mode); }
  67. typedef detail::save_state_unprotected<Rounding> unprotected_rounding;
  68. };
  69. template<class Rounding>
  70. struct save_state_nothing: Rounding
  71. {
  72. typedef save_state_nothing<Rounding> unprotected_rounding;
  73. };
  74. template<class T>
  75. struct rounded_math: save_state_nothing<rounded_arith_exact<T> >
  76. {};
  77. } // namespace interval_lib
  78. } // namespace numeric
  79. } // namespace boost
  80. #endif // BOOST_NUMERIC_INTERVAL_ROUNDING_HPP