• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 // it would have been enough to only include:
12 //   <boost/numeric/interval.hpp>
13 // but it's a bit overkill to include processor intrinsics
14 // and transcendental functions, so we do it by ourselves
15 
16 #include <boost/numeric/interval/interval.hpp>      // base class
17 #include <boost/numeric/interval/rounded_arith.hpp> // default arithmetic rounding policy
18 #include <boost/numeric/interval/checking.hpp>      // default checking policy
19 #include <boost/numeric/interval/arith.hpp>         // += *= -= etc
20 #include <boost/numeric/interval/policies.hpp>      // default policy
21 
22 #include <boost/rational.hpp>
23 #include <iostream>
24 
25 typedef boost::rational<int> Rat;
26 typedef boost::numeric::interval<Rat> Interval;
27 
operator <<(std::ostream & os,const Interval & r)28 std::ostream& operator<<(std::ostream& os, const Interval& r) {
29   os << "[" << r.lower() << "," << r.upper() << "]";
30   return os;
31 }
32 
main()33 int main() {
34   Rat p(2, 3), q(3, 4);
35   Interval z(4, 5);
36   Interval a(p, q);
37   a += z;
38   z *= q;
39   a -= p;
40   a /= q;
41   std::cout << z << std::endl;
42   std::cout << a << std::endl;
43 }
44