Boost.Real  1.0.0
Boost.Real numerical data type for real numbers representation using range arithmetic.
interval.hpp
1 #ifndef BOOST_REAL_INTERVAL_HPP
2 #define BOOST_REAL_INTERVAL_HPP
3 
4 #include <vector>
5 
6 #include <real/boundary.hpp>
7 #include <real/boundary_helper.hpp>
8 
9 namespace boost {
10  namespace real {
11 
18  struct interval {
19  boost::real::boundary lower_bound;
20  boost::real::boundary upper_bound;
21 
25  interval() = default;
26 
33  std::string as_string() const {
34 
35  std::string result = "";
36  std::string lb = this->lower_bound.as_string();
37  std::string ub = this->upper_bound.as_string();
38 
39  if (lb == ub) {
40  result = lb;
41  } else {
42  result = '[' + lb + ", " + ub + ']';
43  }
44 
45  return result;
46  }
47 
52  void swap_bounds() {
53  this->lower_bound.swap(this->upper_bound);
54  }
55 
64  bool operator<(const boost::real::interval& other) const {
65  return this->upper_bound < other.lower_bound;
66  }
67 
76  bool operator>(const boost::real::interval& other) const {
77  return this->lower_bound > other.upper_bound;
78  }
79 
85  bool positive() const {
86  // If the lower bound of a approximation_interval is positive, then the upper bound is also positive
87  // and the approximation_interval is fully contained in the positive number line
88  return this->lower_bound.positive;
89  }
90 
96  bool negative() const {
97  // If the upper bound of a approximation_interval is negative, then the lower bound is also negative
98  // and the approximation_interval is fully contained in the negative number line
99  return !this->upper_bound.positive;
100  }
101 
108  bool operator==(const boost::real::interval& other) const {
109  return this->lower_bound == other.lower_bound && this->upper_bound == other.upper_bound;
110  }
111 
118  bool is_a_number() const {
119  return this->lower_bound == this->upper_bound;
120  }
121  };
122  }
123 }
124 
125 std::ostream& operator<<(std::ostream& os, const boost::real::interval& interval) {
126  return os << interval.as_string();
127 }
128 
129 #endif //BOOST_REAL_INTERVAL_HPP
bool operator<(const boost::real::interval &other) const
Compares two boost::real::interval to determine if *this is a lower interval than other according to ...
Definition: interval.hpp:64
bool is_a_number() const
Determines if the interval represent a single number. i.e. the lower and upper boundaries are equals...
Definition: interval.hpp:118
Definition: boundary.hpp:7
std::string as_string() const
Generates a string representation of the boost::real::interval. The string represent the interval wit...
Definition: interval.hpp:33
interval()=default
default constructor: It construct a representation of the interval [0,0].
std::basic_string< char > as_string() const
Generates a string representation of the boost::real::boundary.
Definition: boundary.hpp:114
Represent an interval composed by two boundaries, a lower boundary and an upper boundary. The boundaries are boost::real::boundary structs that represent fully represented numbers.
Definition: interval.hpp:18
bool positive() const
Determine if the interval is fully contained in the positive real number line.
Definition: interval.hpp:85
void swap(boundary &other)
Swaps the boost::real::boundary value with the value of the other boost::real::boundary. This operation is a more preformant form of swapping to boost::real::boundaries.
Definition: boundary.hpp:174
bool negative() const
Determine if the interval is fully contained in the negative real number line.
Definition: interval.hpp:96
bool operator==(const boost::real::interval &other) const
Equality comparator. Determines if *this is equal or not to other.
Definition: interval.hpp:108
bool operator>(const boost::real::interval &other) const
Compares two boost::real::interval to determine if *this is a greater interval than other according t...
Definition: interval.hpp:76
Explicitly represents a number as a vector of digits with a sign and an exponent. ...
Definition: boundary.hpp:15
void swap_bounds()
Swaps the lower boundary with the upper boundary. After this method is called the boost::real::interv...
Definition: interval.hpp:52