rational.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* Boost example/rational.cpp
  2. * example program of how to use interval< rational<> >
  3. *
  4. * Copyright 2002-2003 Guillaume Melquiond, Sylvain Pion
  5. *
  6. * Distributed under the Boost Software License, Version 1.0.
  7. * (See accompanying file LICENSE_1_0.txt or
  8. * copy at http://www.boost.org/LICENSE_1_0.txt)
  9. */
  10. // it would have been enough to only include:
  11. // <boost/numeric/interval.hpp>
  12. // but it's a bit overkill to include processor intrinsics
  13. // and transcendental functions, so we do it by ourselves
  14. #include <boost/numeric/interval/interval.hpp> // base class
  15. #include <boost/numeric/interval/rounded_arith.hpp> // default arithmetic rounding policy
  16. #include <boost/numeric/interval/checking.hpp> // default checking policy
  17. #include <boost/numeric/interval/arith.hpp> // += *= -= etc
  18. #include <boost/numeric/interval/policies.hpp> // default policy
  19. #include <boost/rational.hpp>
  20. #include <iostream>
  21. typedef boost::rational<int> Rat;
  22. typedef boost::numeric::interval<Rat> Interval;
  23. std::ostream& operator<<(std::ostream& os, const Interval& r) {
  24. os << "[" << r.lower() << "," << r.upper() << "]";
  25. return os;
  26. }
  27. int main() {
  28. Rat p(2, 3), q(3, 4);
  29. Interval z(4, 5);
  30. Interval a(p, q);
  31. a += z;
  32. z *= q;
  33. a -= p;
  34. a /= q;
  35. std::cout << z << std::endl;
  36. std::cout << a << std::endl;
  37. }