arith3.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Boost interval/arith3.hpp template implementation file
  2. *
  3. * This headers provides arithmetical functions
  4. * which compute an interval given some base
  5. * numbers. The resulting interval encloses the
  6. * real result of the arithmetic operation.
  7. *
  8. * Copyright 2003 Guillaume Melquiond
  9. *
  10. * Distributed under the Boost Software License, Version 1.0.
  11. * (See accompanying file LICENSE_1_0.txt or
  12. * copy at http://www.boost.org/LICENSE_1_0.txt)
  13. */
  14. #ifndef BOOST_NUMERIC_INTERVAL_ARITH3_HPP
  15. #define BOOST_NUMERIC_INTERVAL_ARITH3_HPP
  16. #include <boost/numeric/interval/detail/interval_prototype.hpp>
  17. #include <boost/numeric/interval/detail/test_input.hpp>
  18. namespace boost {
  19. namespace numeric {
  20. namespace interval_lib {
  21. template<class I> inline
  22. I add(const typename I::base_type& x, const typename I::base_type& y)
  23. {
  24. typedef typename I::traits_type Policies;
  25. if (detail::test_input<typename I::base_type, Policies>(x, y))
  26. return I::empty();
  27. typename Policies::rounding rnd;
  28. return I(rnd.add_down(x, y), rnd.add_up(x, y), true);
  29. }
  30. template<class I> inline
  31. I sub(const typename I::base_type& x, const typename I::base_type& y)
  32. {
  33. typedef typename I::traits_type Policies;
  34. if (detail::test_input<typename I::base_type, Policies>(x, y))
  35. return I::empty();
  36. typename Policies::rounding rnd;
  37. return I(rnd.sub_down(x, y), rnd.sub_up(x, y), true);
  38. }
  39. template<class I> inline
  40. I mul(const typename I::base_type& x, const typename I::base_type& y)
  41. {
  42. typedef typename I::traits_type Policies;
  43. if (detail::test_input<typename I::base_type, Policies>(x, y))
  44. return I::empty();
  45. typename Policies::rounding rnd;
  46. return I(rnd.mul_down(x, y), rnd.mul_up(x, y), true);
  47. }
  48. template<class I> inline
  49. I div(const typename I::base_type& x, const typename I::base_type& y)
  50. {
  51. typedef typename I::traits_type Policies;
  52. if (detail::test_input<typename I::base_type, Policies>(x, y) || user::is_zero(y))
  53. return I::empty();
  54. typename Policies::rounding rnd;
  55. return I(rnd.div_down(x, y), rnd.div_up(x, y), true);
  56. }
  57. } // namespace interval_lib
  58. } // namespace numeric
  59. } // namespace boost
  60. #endif // BOOST_NUMERIC_INTERVAL_ARITH3_HPP