Boost.Real  1.0.0
Boost.Real numerical data type for real numbers representation using range arithmetic.
boundary.hpp
1 #ifndef BOOST_REAL_BOUNDARY_HPP
2 #define BOOST_REAL_BOUNDARY_HPP
3 
4 #include <vector>
5 #include <real/boundary_helper.hpp>
6 
7 namespace boost {
8  namespace real {
9 
15  struct boundary {
16  std::vector<int> digits = {};
17  int exponent = 0;
18  bool positive = true;
19 
23  boundary() = default;
24 
31  boundary(const boundary &other) = default;
32 
38  boundary &operator=(const boundary& other) = default;
39 
47  bool operator<(const boundary& other) const {
48 
49  if (this->positive != other.positive) {
50  return !this->positive;
51  }
52 
53  if (this->positive) {
54  if (this->exponent == other.exponent) {
55  return boost::real::helper::aligned_vectors_is_lower(this->digits,
56  other.digits);
57  }
58 
59  return this->exponent < other.exponent;
60  }
61 
62  if (this->exponent == other.exponent) {
63  return boost::real::helper::aligned_vectors_is_lower(other.digits,
64  this->digits);
65  }
66 
67  return other.exponent < this->exponent;
68  }
69 
77  bool operator>(const boundary& other) const {
78 
79  if (this->positive != other.positive) {
80  return this->positive;
81  }
82 
83  if (this->positive) {
84  if (this->exponent == other.exponent) {
85  return boost::real::helper::aligned_vectors_is_lower(other.digits, this->digits);
86  }
87 
88  return this->exponent > other.exponent;
89  }
90 
91  if (this->exponent == other.exponent) {
92  return boost::real::helper::aligned_vectors_is_lower(this->digits, other.digits);
93  }
94 
95  return other.exponent > this->exponent;
96  }
97 
105  bool operator==(const boundary& other) const {
106  return !(*this < other || other < *this);
107  }
108 
114  std::basic_string<char> as_string() const {
115  std::basic_string<char> result = "";
116 
117  if (!this->positive) {
118  result = "-";
119  }
120 
121  // If the number is to large, scientific notation is used to print it.
122  if ((this->exponent < -10) || (this->exponent > (int)this->digits.size() + 10)) {
123  result += "0.";
124 
125  for (const auto& d: this->digits) {
126  result += std::to_string(d);
127  }
128 
129  result += "e" + std::to_string(this->exponent);
130  return result;
131  }
132 
133  if (this->exponent <= 0) {
134  result += "0.";
135 
136  for (int i = this->exponent; i < (int) this->digits.size(); ++i) {
137  if (i < 0) {
138  result += "0";
139  } else {
140  result += std::to_string(this->digits[i]);
141  }
142  }
143  } else {
144 
145  int digit_amount = std::max(this->exponent, (int) this->digits.size());
146  for (int i = 0; i < digit_amount; ++i) {
147 
148  if (i == this->exponent) {
149  result += ".";
150  }
151 
152  if (i < (int) this->digits.size()) {
153  result += std::to_string(this->digits[i]);
154  } else {
155  result += "0";
156  }
157  }
158 
159  if (result.back() == '.') {
160  result.pop_back();
161  }
162  }
163 
164 
165  return result;
166  }
167 
174  void swap(boundary &other) {
175  this->digits.swap(other.digits);
176  std::swap(this->exponent, other.exponent);
177  std::swap(this->positive, other.positive);
178  }
179 
186  void push_back(int digit) {
187  this->digits.push_back(digit);
188  }
189 
196  void push_front(int digit) {
197  this->digits.insert(this->digits.begin(), digit);
198  }
199 
204  void normalize() {
205  while (this->digits.size() > 1 && this->digits.front() == 0) {
206  this->digits.erase(this->digits.begin());
207  this->exponent--;
208  }
209 
210  while (this->digits.size() > 1 && this->digits.back() == 0) {
211  this->digits.pop_back();
212  }
213 
214  // Zero could have many representation, and the normalized is the next one.
215  if (this->digits.size() == 1 && this->digits.front() == 0) {
216  this->exponent = 0;
217  this->positive = true;
218  }
219  }
220 
225  void normalize_left() {
226  while (this->digits.size() > 1 && this->digits.front() == 0) {
227  this->digits.erase(this->digits.begin());
228  this->exponent--;
229  }
230  }
231 
235  void clear() {
236  this->digits.clear();
237  }
238 
245  int &operator[](int n) {
246  return this->digits[n];
247  }
248 
254  unsigned long size() {
255  return this->digits.size();
256  }
257  };
258  }
259 }
260 
261 #endif //BOOST_REAL_BOUNDARY_HPP
bool operator<(const boundary &other) const
Lower comparator operator: It compares the *this boost::real::boundary with the other boost::real::bo...
Definition: boundary.hpp:47
Definition: boundary.hpp:7
std::basic_string< char > as_string() const
Generates a string representation of the boost::real::boundary.
Definition: boundary.hpp:114
void clear()
ir clears the number digits.
Definition: boundary.hpp:235
bool operator==(const boundary &other) const
Equality comparator operator: It compares the *this boost::real::boundary with the other boost::real:...
Definition: boundary.hpp:105
void push_front(int digit)
add the digit parameter as a new digit of the boost::real::boundary. The digit is added in the left s...
Definition: boundary.hpp:196
void push_back(int digit)
add the digit parameter as a new digit of the boost::real::boundary. The digit is added in the right ...
Definition: boundary.hpp:186
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 operator>(const boundary &other) const
Greater comparator operator: It compares the *this boost::real::boundary with the other boost::real::...
Definition: boundary.hpp:77
void normalize()
Removes extra zeros at the sides to convert the number representation into a normalized representatio...
Definition: boundary.hpp:204
boundary & operator=(const boundary &other)=default
Default asignment operator.
void normalize_left()
Removes extra zeros at the left side to convert the number representation into a semi normalized repr...
Definition: boundary.hpp:225
Explicitly represents a number as a vector of digits with a sign and an exponent. ...
Definition: boundary.hpp:15
boundary()=default
default constructor: It construct a representation of the number zero.
int & operator[](int n)
Returns the n-th digit of the boost::real::boundary.
Definition: boundary.hpp:245
unsigned long size()
It return the amount of digit of the boost::real::boundary.
Definition: boundary.hpp:254