1 #ifndef BOOST_REAL_REAL_HPP 2 #define BOOST_REAL_REAL_HPP 7 #include <initializer_list> 10 #include <real/real_exception.hpp> 11 #include <real/real_helpers.hpp> 12 #include <real/real_explicit.hpp> 13 #include <real/real_algorithm.hpp> 57 enum class OPERATION {ADDITION, SUBTRACT, MULTIPLICATION};
58 enum class KIND {EXPLICIT, OPERATION, ALGORITHM};
70 real* _lhs_ptr =
nullptr;
71 real* _rhs_ptr =
nullptr;
74 unsigned int _maximum_precision = 0;
76 void copy_operands(
const real& other) {
77 if (other._lhs_ptr !=
nullptr) {
78 this->_lhs_ptr =
new real(*other._lhs_ptr);
81 if (other._rhs_ptr !=
nullptr) {
82 this->_rhs_ptr =
new real(*other._rhs_ptr);
104 real const* _real_ptr =
nullptr;
116 void calculate_operation_boundaries() {
118 switch (this->_real_ptr->_operation) {
120 case OPERATION::ADDITION:
121 boost::real::helper::add_boundaries(
122 this->_lhs_it_ptr->approximation_interval.lower_bound,
123 this->_rhs_it_ptr->approximation_interval.lower_bound,
124 this->approximation_interval.lower_bound
127 boost::real::helper::add_boundaries(
128 this->_lhs_it_ptr->approximation_interval.upper_bound,
129 this->_rhs_it_ptr->approximation_interval.upper_bound,
130 this->approximation_interval.upper_bound
135 case OPERATION::SUBTRACT:
136 boost::real::helper::subtract_boundaries(
137 this->_lhs_it_ptr->approximation_interval.lower_bound,
138 this->_rhs_it_ptr->approximation_interval.upper_bound,
139 this->approximation_interval.lower_bound
142 boost::real::helper::subtract_boundaries(
143 this->_lhs_it_ptr->approximation_interval.upper_bound,
144 this->_rhs_it_ptr->approximation_interval.lower_bound,
145 this->approximation_interval.upper_bound
150 case OPERATION::MULTIPLICATION: {
151 bool lhs_positive = this->_lhs_it_ptr->approximation_interval.
positive();
152 bool rhs_positive = this->_rhs_it_ptr->approximation_interval.
positive();
153 bool lhs_negative = this->_lhs_it_ptr->approximation_interval.
negative();
154 bool rhs_negative = this->_rhs_it_ptr->approximation_interval.
negative();
156 if (lhs_positive && rhs_positive) {
157 boost::real::helper::multiply_boundaries(
158 this->_lhs_it_ptr->approximation_interval.lower_bound,
159 this->_rhs_it_ptr->approximation_interval.lower_bound,
160 this->approximation_interval.lower_bound
163 boost::real::helper::multiply_boundaries(
164 this->_lhs_it_ptr->approximation_interval.upper_bound,
165 this->_rhs_it_ptr->approximation_interval.upper_bound,
166 this->approximation_interval.upper_bound
169 }
else if (lhs_negative && rhs_negative) {
170 boost::real::helper::multiply_boundaries(
171 this->_lhs_it_ptr->approximation_interval.upper_bound,
172 this->_rhs_it_ptr->approximation_interval.upper_bound,
173 this->approximation_interval.lower_bound
176 boost::real::helper::multiply_boundaries(
177 this->_lhs_it_ptr->approximation_interval.lower_bound,
178 this->_rhs_it_ptr->approximation_interval.lower_bound,
179 this->approximation_interval.upper_bound
181 }
else if (lhs_negative && rhs_positive) {
182 boost::real::helper::multiply_boundaries(
183 this->_lhs_it_ptr->approximation_interval.lower_bound,
184 this->_rhs_it_ptr->approximation_interval.upper_bound,
185 this->approximation_interval.lower_bound
188 boost::real::helper::multiply_boundaries(
189 this->_lhs_it_ptr->approximation_interval.upper_bound,
190 this->_rhs_it_ptr->approximation_interval.lower_bound,
191 this->approximation_interval.upper_bound
194 }
else if (lhs_positive && rhs_negative) {
195 boost::real::helper::multiply_boundaries(
196 this->_lhs_it_ptr->approximation_interval.upper_bound,
197 this->_rhs_it_ptr->approximation_interval.lower_bound,
198 this->approximation_interval.lower_bound
201 boost::real::helper::multiply_boundaries(
202 this->_lhs_it_ptr->approximation_interval.lower_bound,
203 this->_rhs_it_ptr->approximation_interval.upper_bound,
204 this->approximation_interval.upper_bound
212 boost::real::helper::multiply_boundaries(
213 this->_lhs_it_ptr->approximation_interval.lower_bound,
214 this->_rhs_it_ptr->approximation_interval.lower_bound,
218 this->approximation_interval.lower_bound = current_boundary;
219 this->approximation_interval.upper_bound = current_boundary;
222 boost::real::helper::multiply_boundaries(
223 this->_lhs_it_ptr->approximation_interval.upper_bound,
224 this->_rhs_it_ptr->approximation_interval.upper_bound,
228 if (current_boundary < this->approximation_interval.lower_bound) {
229 this->approximation_interval.lower_bound = current_boundary;
232 if (this->approximation_interval.upper_bound < current_boundary) {
233 this->approximation_interval.upper_bound = current_boundary;
237 boost::real::helper::multiply_boundaries(
238 this->_lhs_it_ptr->approximation_interval.lower_bound,
239 this->_rhs_it_ptr->approximation_interval.upper_bound,
243 if (current_boundary < this->approximation_interval.lower_bound) {
244 this->approximation_interval.lower_bound = current_boundary;
247 if (this->approximation_interval.upper_bound < current_boundary) {
248 this->approximation_interval.upper_bound = current_boundary;
252 boost::real::helper::multiply_boundaries(
253 this->_lhs_it_ptr->approximation_interval.upper_bound,
254 this->_rhs_it_ptr->approximation_interval.lower_bound,
258 if (current_boundary < this->approximation_interval.lower_bound) {
259 this->approximation_interval.lower_bound = current_boundary;
262 if (this->approximation_interval.upper_bound < current_boundary) {
263 this->approximation_interval.upper_bound = current_boundary;
303 switch (this->_real_ptr->_kind) {
306 this->_explicit_it = this->_real_ptr->_explicit_number.cbegin();
307 this->approximation_interval = this->_explicit_it.approximation_interval;
310 case KIND::ALGORITHM:
311 this->_algorithmic_it = this->_real_ptr->_algorithmic_number.cbegin();
312 this->approximation_interval = this->_algorithmic_it.approximation_interval;
315 case KIND::OPERATION:
318 this->calculate_operation_boundaries();
338 switch (this->_real_ptr->_kind) {
342 this->_explicit_it = this->_real_ptr->_explicit_number.cend();
344 this->_explicit_it = this->_real_ptr->_explicit_number.cbegin();
346 this->approximation_interval = this->_explicit_it.approximation_interval;
349 case KIND::ALGORITHM:
351 this->_algorithmic_it = this->_real_ptr->_algorithmic_number.cend();
353 this->_algorithmic_it = this->_real_ptr->_algorithmic_number.cbegin();
355 this->approximation_interval = this->_algorithmic_it.approximation_interval;
358 case KIND::OPERATION:
361 this->calculate_operation_boundaries();
372 switch (this->_real_ptr->_kind) {
375 ++this->_explicit_it;
376 this->approximation_interval = this->_explicit_it.approximation_interval;
379 case KIND::ALGORITHM:
380 ++this->_algorithmic_it;
381 this->approximation_interval = this->_algorithmic_it.approximation_interval;
384 case KIND::OPERATION:
386 this->approximation_interval.lower_bound.
clear();
387 this->approximation_interval.upper_bound.
clear();
390 ++(*this->_lhs_it_ptr);
391 ++(*this->_rhs_it_ptr);
394 this->calculate_operation_boundaries();
408 if (this->_real_ptr ==
nullptr || other._real_ptr ==
nullptr) {
412 return (other._real_ptr == this->_real_ptr) && (other.approximation_interval == this->approximation_interval);
422 return !(*
this == other);
443 _explicit_number(other._explicit_number),
444 _algorithmic_number(other._algorithmic_number),
445 _operation(other._operation) { this->copy_operands(other); };
456 real(
const std::string& number)
457 : _kind(KIND::EXPLICIT), _explicit_number(number) {}
466 real(std::initializer_list<int> digits)
467 : _kind(KIND::EXPLICIT), _explicit_number(digits, digits.size()) {}
480 real(std::initializer_list<int> digits,
bool positive)
481 : _kind(KIND::EXPLICIT), _explicit_number(digits, digits.size(), positive) {}
492 real(std::initializer_list<int> digits,
int exponent)
493 : _kind(KIND::EXPLICIT), _explicit_number(digits, exponent) {};
506 real(std::initializer_list<int> digits,
int exponent,
bool positive)
507 : _kind(KIND::EXPLICIT), _explicit_number(digits, exponent, positive) {};
519 real(
int (*get_nth_digit)(
unsigned int),
int exponent)
520 : _kind(KIND::ALGORITHM), _algorithmic_number(get_nth_digit, exponent) {}
535 real(
int (*get_nth_digit)(
unsigned int),
538 : _kind(KIND::ALGORITHM),
539 _algorithmic_number(get_nth_digit, exponent, positive) {}
545 delete this->_lhs_ptr;
546 this->_lhs_ptr =
nullptr;
548 delete this->_rhs_ptr;
549 this->_rhs_ptr =
nullptr;
559 if (this->_maximum_precision == 0) {
563 return this->_maximum_precision;
616 switch (this->_kind) {
619 result = this->_explicit_number[n];
622 case KIND::ALGORITHM:
623 result = this->_algorithmic_number[n];
626 case KIND::OPERATION:
643 this->_lhs_ptr =
new real(*
this);
644 this->_rhs_ptr =
new real(other);
645 this->_kind = KIND::OPERATION;
646 this->_operation = OPERATION::ADDITION;
673 this->_lhs_ptr =
new real(*
this);
674 this->_rhs_ptr =
new real(other);
675 this->_kind = KIND::OPERATION;
676 this->_operation = OPERATION::SUBTRACT;
703 this->_lhs_ptr =
new real(*
this);
704 this->_rhs_ptr =
new real(other);
705 this->_kind = KIND::OPERATION;
706 this->_operation = OPERATION::MULTIPLICATION;
731 this->_kind = other._kind;
732 this->_explicit_number = other._explicit_number;
733 this->_operation = other._operation;
734 this->copy_operands(other);
746 *
this =
real(number);
762 auto this_it = this->
cbegin();
763 auto other_it = other.cbegin();
765 unsigned int current_precision = std::max(this->
max_precision(), other.max_precision());
766 for (
unsigned int p = 0; p < current_precision; ++p) {
771 bool this_full_precision = this_it.approximation_interval.is_a_number();
772 bool other_full_precision = other_it.approximation_interval.is_a_number();
773 if (this_full_precision && other_full_precision) {
774 return this_it.approximation_interval < other_it.approximation_interval;
777 if (this_it.approximation_interval < other_it.approximation_interval) {
781 if (other_it.approximation_interval < this_it.approximation_interval) {
803 auto this_it = this->
cbegin();
804 auto other_it = other.cbegin();
806 unsigned int current_precision = std::max(this->
max_precision(), other.max_precision());
807 for (
unsigned int p = 0; p < current_precision; ++p) {
812 bool this_full_precision = this_it.approximation_interval.is_a_number();
813 bool other_full_precision = other_it.approximation_interval.is_a_number();
814 if (this_full_precision && other_full_precision) {
815 return this_it.approximation_interval > other_it.approximation_interval;
818 if (this_it.approximation_interval > other_it.approximation_interval) {
822 if (other_it.approximation_interval > this_it.approximation_interval) {
844 auto this_it = this->
cbegin();
845 auto other_it = other.cbegin();
847 unsigned int current_precision = std::max(this->
max_precision(), other.max_precision());
848 for (
unsigned int p = 0; p < current_precision; ++p) {
853 bool this_full_precision = this_it.approximation_interval.is_a_number();
854 bool other_full_precision = other_it.approximation_interval.is_a_number();
855 if (this_full_precision && other_full_precision) {
856 return this_it.approximation_interval == other_it.approximation_interval;
859 bool this_is_lower = this_it.approximation_interval < other_it.approximation_interval;
860 bool other_is_lower = other_it.approximation_interval < this_it.approximation_interval;
861 if (this_is_lower || other_is_lower) {
882 os << r.
cend().approximation_interval;
886 #endif //BOOST_REAL_REAL_HPP real(int(*get_nth_digit)(unsigned int), int exponent)
Lambda function constructor with exponent: Creates a boost::real::real instance that represents the n...
Definition: real.hpp:519
Definition: real_exception.hpp:8
Definition: real_exception.hpp:22
boost::real::real_explicit is a C++ class that fully represents real numbers as a vector of digits...
Definition: real_explicit.hpp:22
real operator-(const real &other) const
Creates a new boost::real::real representing an operation number where the operands are copies of the...
Definition: real.hpp:688
Definition: boundary.hpp:7
is a forward iterator that iterates a boost::real::real_algorithm number approximation intervals...
Definition: real_algorithm.hpp:41
real(int(*get_nth_digit)(unsigned int), int exponent, bool positive)
Lambda function constructor with exponent and sign: Creates a boost::real::real instance that represe...
Definition: real.hpp:535
bool operator==(const real &other) const
Compares the *this boost::real::real number against the other boost::real::real number to determine i...
Definition: real.hpp:843
is a forward iterator that iterates a boost::real::real_explicit number approximation intervals...
Definition: real_explicit.hpp:42
~real()
Default destructor: If the number is an operator, the destructor destroys its operands.
Definition: real.hpp:544
const_precision_iterator(real const *real_number)
Pointer constructor: Construct a new boost::real::real::const_precision_iterator pointing to the boos...
Definition: real.hpp:301
Definition: real_exception.hpp:15
const_precision_iterator(real const *real_number, bool cend)
Pointer constructor for cend: Construct a new boost::real::real::const_precision_iterator pointing to...
Definition: real.hpp:336
real(std::initializer_list< int > digits, int exponent)
Initializer list constructor with exponent: Creates a boost::real::real instance that represents the ...
Definition: real.hpp:492
void clear()
ir clears the number digits.
Definition: boundary.hpp:235
real operator+(const real &other) const
Creates a new boost::real::real representing an operation number where the operands are copies of the...
Definition: real.hpp:658
boost::real::real_algorithm is a C++ class that represents real numbers as a a function that calculat...
Definition: real_algorithm.hpp:19
real & operator=(const std::string &number)
It constructs and boost::real::real number from the std::string and assign it in the *this boost::rea...
Definition: real.hpp:745
bool operator==(const const_precision_iterator &other) const
It compare by value equality; two boost::real::real::const_precision_iterators are equals if they are...
Definition: real.hpp:406
real & operator+=(const real &other)
Convert the number from its current representation to an operation number representation where the op...
Definition: real.hpp:642
real(const std::string &number)
String constructor: Creates a boost::real::real instance by parsing the string. The string must have ...
Definition: real.hpp:456
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 operator<(const real &other) const
Compares the *this boost::real::real number against the other boost::real::real number to determine i...
Definition: real.hpp:761
real & operator*=(const real &other)
Convert the number from its current representation to an operation number representation where the op...
Definition: real.hpp:702
real(std::initializer_list< int > digits, int exponent, bool positive)
Initializer list constructor with exponent and sign: Creates a boost::real::real instance that repres...
Definition: real.hpp:506
const_precision_iterator cbegin() const
Construct a new boost::real::real::con_precision_iterator that iterates the number approximation inte...
Definition: real.hpp:586
bool positive() const
Determine if the interval is fully contained in the positive real number line.
Definition: interval.hpp:85
unsigned int max_precision() const
Returns te maximum allowed precision, if that precision is reached and an operator need more precisio...
Definition: real.hpp:558
real(std::initializer_list< int > digits, bool positive)
Signed initializer list constructor: Creates a boost::real::real instance that represents the number ...
Definition: real.hpp:480
boost::real::real is a C++ class that represent real numbers as abstract entities that can be dynamic...
Definition: real.hpp:54
real(std::initializer_list< int > digits)
Initializer list constructor: Creates a boost::real::real_explicit instance that represents an intege...
Definition: real.hpp:466
bool operator>(const real &other) const
Compares the *this boost::real::real number against the other boost::real::real number to determine i...
Definition: real.hpp:802
bool negative() const
Determine if the interval is fully contained in the negative real number line.
Definition: interval.hpp:96
const_precision_iterator cend() const
Construct a new boost::real::real::con_precision_iterator that iterates the number approximation inte...
Definition: real.hpp:598
bool operator!=(const const_precision_iterator &other) const
It compare by value not equal; two boost::real::real::const_precision_iterators.
Definition: real.hpp:421
real operator*(const real &other) const
Creates a new boost::real::real representing an operation number where the operands are copies of the...
Definition: real.hpp:718
void set_maximum_precision(unsigned int maximum_precision)
Set a new maximum precision for the instance.
Definition: real.hpp:574
real()=default
Default constructor: Constructr a boost::real::real with undefined representation and behaviour...
real & operator=(const real &other)
It assign a new copy of the other boost::real::real number in the *this boost::real::real number...
Definition: real.hpp:730
is a forward iterator that iterates a boost::real::real number approximation intervals. The iterator calculates the initial interval with the initial precision and then it increase the precision in each iteration (++) and recalculate the interval.
Definition: real.hpp:100
Explicitly represents a number as a vector of digits with a sign and an exponent. ...
Definition: boundary.hpp:15
real & operator-=(const real &other)
Convert the number from its current representation to an operation number representation where the op...
Definition: real.hpp:672
static unsigned int maximum_precision
Determines the maximum precision to use.
Definition: real.hpp:91
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.hpp:370
real(const real &other)
Copy constructor: Creates a copy of the boost::real::real number other, if the number is an operation...
Definition: real.hpp:441
int operator[](unsigned int n) const
If the number is an explicit or algorithm number it returns the n-th digit of the represented number...
Definition: real.hpp:613