From 91c5b20edc58c999d338bd435a87711dfd28042f Mon Sep 17 00:00:00 2001 From: Laouen Date: Mon, 13 Aug 2018 19:40:51 +0200 Subject: [PATCH] Implemented new maximum precision sytem with default static and particular values and a setter. --- README.md | 12 +++++++++--- include/real/real.hpp | 17 ++++++++++------- include/real/real_algorithm.hpp | 4 ++-- include/real/real_explicit.hpp | 4 ++-- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 45ec57a..6104790 100644 --- a/README.md +++ b/README.md @@ -118,12 +118,18 @@ The boost::real::const_precision_iterator is a forward iterator [4] that iterate ### Other methods 1. boost::real::const_precision_iterator boost::real::cbegin() - 1. boost::real::const_precision_iterator boost::real::cend() + 2. boost::real::const_precision_iterator boost::real::cend() + 3. unsigned int boost::real::maximum_precision() + 4. void boost::real::set_maximum_precision(unsigned int) > (1) Construct a new const_precision_iterator that iterate over the *this number precisions. The constructed iterator points to the first approximation interval (the ine with less precision). > (2) Construct a new const_precision_iterator that iterate over the *this number precisions. The constructed iterator points to the last approximation interval (the one with more precision) within the maximum precision. +> (3) Returns the instance maximum precision at which the operators will throw a boost::real::precision_exception if they cannot determine the operation value before reaching the maximum precision. + +> (4) Sets a new maximum precision. If the set maximum precision is zero, the static default maximum precision will be used instead. + ## boost::real::const_precision_iterator interface ### Constructors @@ -158,8 +164,8 @@ The boost::real::const_precision_iterator is a forward iterator [4] that iterate #include #include -int boost::real::real::maximum_precision = 10; -int boost::real::real_algorithm::maximum_precision = 10; +unsigned int boost::real::real::maximum_precision = 10; +unsigned int boost::real::real_algorithm::maximum_precision = 10; int main() { boost::real::real c = (std::string)"0.999999"; diff --git a/include/real/real.hpp b/include/real/real.hpp index 36ca1ff..434b631 100644 --- a/include/real/real.hpp +++ b/include/real/real.hpp @@ -555,7 +555,7 @@ namespace boost { * * @return and integer with the maximum allowed precision. */ - int max_precision() const { + unsigned int max_precision() const { if (this->_maximum_precision == 0) { return boost::real::real::maximum_precision; } @@ -566,6 +566,9 @@ namespace boost { /** * @brief Set a new maximum precision for the instance. * + * @note Setting zero as the maximum precision causes the instance to use the default + * static unsigned int boost::real::real::maximum_precision. + * * @param maximum_precision - an unsigned int to set as the new precision. */ void set_maximum_precision(unsigned int maximum_precision) { @@ -759,8 +762,8 @@ namespace boost { auto this_it = this->cbegin(); auto other_it = other.cbegin(); - int current_precision = std::max(this->max_precision(), other.max_precision()); - for (int p = 0; p < current_precision; ++p) { + unsigned int current_precision = std::max(this->max_precision(), other.max_precision()); + for (unsigned int p = 0; p < current_precision; ++p) { // Get more precision ++this_it; ++other_it; @@ -800,8 +803,8 @@ namespace boost { auto this_it = this->cbegin(); auto other_it = other.cbegin(); - int current_precision = std::max(this->max_precision(), other.max_precision()); - for (int p = 0; p < current_precision; ++p) { + unsigned int current_precision = std::max(this->max_precision(), other.max_precision()); + for (unsigned int p = 0; p < current_precision; ++p) { // Get more precision ++this_it; ++other_it; @@ -841,8 +844,8 @@ namespace boost { auto this_it = this->cbegin(); auto other_it = other.cbegin(); - int current_precision = std::max(this->max_precision(), other.max_precision()); - for (int p = 0; p < current_precision; ++p) { + unsigned int current_precision = std::max(this->max_precision(), other.max_precision()); + for (unsigned int p = 0; p < current_precision; ++p) { // Get more precision ++this_it; ++other_it; diff --git a/include/real/real_algorithm.hpp b/include/real/real_algorithm.hpp index 45c03e7..62d2f86 100644 --- a/include/real/real_algorithm.hpp +++ b/include/real/real_algorithm.hpp @@ -232,7 +232,7 @@ namespace boost { * * @return an integer with the maximum allowed precision. */ - int max_precision() const { + unsigned int max_precision() const { return boost::real::real_algorithm::maximum_precision; } @@ -313,7 +313,7 @@ namespace boost { */ std::ostream& operator<<(std::ostream& os, const boost::real::real_algorithm& r) { auto it = r.cbegin(); - for (int i = 0; i <= r.max_precision(); i++) { + for (unsigned int i = 0; i <= r.max_precision(); i++) { ++it; } os << it.approximation_interval; diff --git a/include/real/real_explicit.hpp b/include/real/real_explicit.hpp index 9613341..1b3f01d 100644 --- a/include/real/real_explicit.hpp +++ b/include/real/real_explicit.hpp @@ -358,7 +358,7 @@ namespace boost { * * @return and integer with the maximum allowed precision. */ - int max_precision() const { + unsigned int max_precision() const { return this->_maximum_precision; } @@ -452,7 +452,7 @@ namespace boost { */ std::ostream& operator<<(std::ostream& os, const boost::real::real_explicit& r) { auto it = r.cbegin(); - for (int i = 0; i <= r.max_precision(); i++) { + for (unsigned int i = 0; i <= r.max_precision(); i++) { ++it; } os << it.approximation_interval;