1 #ifndef BOOST_REAL_REAL_EXPLICIT_HPP 2 #define BOOST_REAL_REAL_EXPLICIT_HPP 6 #include <initializer_list> 9 #include <real/real_exception.hpp> 10 #include <real/real_helpers.hpp> 11 #include <real/interval.hpp> 27 std::vector<int> _digits = {};
29 bool _positive =
true;
32 unsigned int _maximum_precision = 1;
51 void check_and_swap_boundaries() {
52 if (!this->_real_ptr->_positive) {
86 this->approximation_interval.lower_bound.exponent = this->_real_ptr->_exponent;
87 this->approximation_interval.upper_bound.exponent = this->_real_ptr->_exponent;
88 this->approximation_interval.lower_bound.positive = this->_real_ptr->_positive;
89 this->approximation_interval.upper_bound.positive = this->_real_ptr->_positive;
91 int first_digit = this->_real_ptr->_digits[0];
92 this->approximation_interval.lower_bound.digits.push_back(first_digit);
94 if (first_digit == 10) {
95 this->approximation_interval.upper_bound.digits.push_back(1);
96 this->approximation_interval.upper_bound.exponent++;
97 }
else if (this->_n < (
int)this->_real_ptr->_digits.size()) {
98 this->approximation_interval.upper_bound.digits.push_back(first_digit + 1);
100 this->approximation_interval.upper_bound.digits.push_back(first_digit);
103 this->check_and_swap_boundaries();
121 if (this->_n >= (
int)this->_real_ptr->_digits.size()) {
132 this->check_and_swap_boundaries();
136 if (this->_n + n >= (
int)this->_real_ptr->_digits.size()) {
138 for(
int i = this->_n; i < (int)this->_real_ptr->_digits.size(); i++) {
139 this->approximation_interval.lower_bound.
push_back(this->_real_ptr->_digits[i]);
141 this->approximation_interval.upper_bound = this->approximation_interval.lower_bound;
149 for(
int i = 0; i < n; i++) {
150 this->approximation_interval.lower_bound.
push_back(this->_real_ptr->_digits[this->_n]);
153 this->approximation_interval.upper_bound.
clear();
154 this->approximation_interval.upper_bound.digits.resize(this->approximation_interval.lower_bound.
size());
157 for (
int i = (
int)this->approximation_interval.lower_bound.
size() - 1; i >= 0; --i) {
158 if (this->approximation_interval.lower_bound[i] + carry == 10) {
159 this->approximation_interval.upper_bound[i] = 0;
161 this->approximation_interval.upper_bound[i] = this->approximation_interval.lower_bound[i] + carry;
167 this->approximation_interval.upper_bound.
push_front(carry);
168 this->approximation_interval.upper_bound.exponent = this->approximation_interval.lower_bound.exponent + 1;
170 this->approximation_interval.upper_bound.exponent = this->approximation_interval.lower_bound.exponent;
178 this->check_and_swap_boundaries();
179 this->_n = std::min(this->_n + n, (
int)this->_real_ptr->_digits.size());
191 if (this->_real_ptr ==
nullptr || other._real_ptr ==
nullptr) {
195 return (other._real_ptr == this->_real_ptr) &&
196 (other._n == this->_n) &&
197 (other.approximation_interval == this->approximation_interval);
207 return !(*
this == other);
235 if (number.length() == 0) {
240 unsigned int dot_amount = 0;
241 for (
const auto& c : number ) {
242 if (c ==
'.' && dot_amount >= 1) {
244 }
else if (c ==
'.') {
249 bool there_is_dot = dot_amount > 0;
254 unsigned int first_index = 0;
255 if (number.at(first_index) ==
'+') {
256 this->_positive =
true;
258 }
else if (number.at(first_index) ==
'-') {
259 this->_positive =
false;
265 unsigned int last_index = (
unsigned int)number.length() - 1;
266 while (last_index > first_index && (number.at(last_index) ==
'0' || number.at(last_index) ==
'.')) {
267 if (number.at(last_index) ==
'.') {
271 if (!there_is_dot || dot_amount > 0) {
277 if (last_index == first_index && number.at(last_index) ==
'0') {
286 while (number.at(first_index) ==
'0') {
290 if (number.at(first_index) ==
'.') {
293 while (number.at(first_index) ==
'0') {
299 for (
unsigned int i = first_index; i <= last_index; i++) {
301 if (number.at(i) ==
'.') {
307 if (dot_amount == 0) exponent++;
309 this->_digits.push_back(number.at(i) -
'0');
310 }
catch (
const std::invalid_argument& e) {
318 this->_maximum_precision = (int)this->_digits.size();
333 _maximum_precision((int)this->_digits.size())
352 _maximum_precision((int)this->_digits.size())
362 return this->_maximum_precision;
371 this->_maximum_precision = maximum_precision;
378 return this->_exponent;
385 return this->_positive;
392 return this->_digits;
428 if (n < this->_digits.size()) {
429 return this->_digits.at(n);
458 os << it.approximation_interval;
463 #endif //BOOST_REAL_REAL_EXPLICIT_HPP bool positive() const
Definition: real_explicit.hpp:384
void iterate_n_times(int n)
It recalculates the approximation interval boundaries increasing the used precision n times...
Definition: real_explicit.hpp:118
boost::real::real_explicit is a C++ class that fully represents real numbers as a vector of digits...
Definition: real_explicit.hpp:22
Definition: boundary.hpp:7
int operator[](unsigned int n) const
Returns the n-th digit the number.
Definition: real_explicit.hpp:427
is a forward iterator that iterates a boost::real::real_explicit number approximation intervals...
Definition: real_explicit.hpp:42
real_explicit(const std::string &number)
String constructor: Creates a boost::real::real_explicit instance by parsing the string. The string must have a valid number, in other case, the constructor will throw an boost::real::invalid_string_number exception.
Definition: real_explicit.hpp:233
void clear()
ir clears the number digits.
Definition: boundary.hpp:235
const_precision_iterator(real_explicit const *real_number)
Pointer constructor: Construct a new boost::real::real_explicit::const_precision_iterator pointing to...
Definition: real_explicit.hpp:85
void operator++()
It recalculates the approximation interval boundaries increasing the used precision, the new pointed approximation interval is smaller than the current one.
Definition: real_explicit.hpp:110
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
int exponent() const
Definition: real_explicit.hpp:377
real_explicit(std::initializer_list< int > digits, int exponent, bool positive)
Initializer list constructor with exponent and sign: Creates a boost::real::real_explicit instance th...
Definition: real_explicit.hpp:348
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
bool operator!=(const const_precision_iterator &other) const
It compare by value not equal; two boost::real::real_explicit::const_precision_iterators.
Definition: real_explicit.hpp:206
real_explicit()=default
Default constructor: Construct an empty boost::real::real_explicit with undefined representation and ...
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
const_precision_iterator cbegin() const
Construct a new boost::real::real_explicit::const_precision_iterator that iterates the number approxi...
Definition: real_explicit.hpp:403
const_precision_iterator()=default
Default constructor: Constructs an empty boost::real::real_explicit::const_precision_iterator that po...
unsigned int max_precision() const
Returns te maximum allowed precision, if that precision is reached and an operator need more precisio...
Definition: real_explicit.hpp:361
void set_maximum_precision(unsigned int maximum_precision)
Set a new maximum precision for the instance.
Definition: real_explicit.hpp:370
real_explicit(std::initializer_list< int > digits, int exponent)
Initializer list constructor with exponent: Creates a boost::real::real_explicit instance that repres...
Definition: real_explicit.hpp:330
void normalize_left()
Removes extra zeros at the left side to convert the number representation into a semi normalized repr...
Definition: boundary.hpp:225
void swap_bounds()
Swaps the lower boundary with the upper boundary. After this method is called the boost::real::interv...
Definition: interval.hpp:52
const_precision_iterator cend() const
Construct a new boost::real::real_explicit::const_precision_iterator that iterates the number approxi...
Definition: real_explicit.hpp:415
Definition: real_exception.hpp:29
const std::vector< int > & digits() const
Definition: real_explicit.hpp:391
unsigned long size()
It return the amount of digit of the boost::real::boundary.
Definition: boundary.hpp:254
bool operator==(const const_precision_iterator &other) const
It compare by value equality; two boost::real::real_explicit::const_precision_iterators are equals if...
Definition: real_explicit.hpp:189
real_explicit & operator=(const real_explicit &other)=default
It assign a new copy of the other boost::real::real_explicit number in the *this boost::real::real_ex...