Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed max_precision for real #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions include/real/real.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ namespace boost {
_kind(other._kind),
_explicit_number(other._explicit_number),
_algorithmic_number(other._algorithmic_number),
_maximum_precision(other.max_precision()),
_operation(other._operation) { this->copy_operands(other); };

/**
Expand Down Expand Up @@ -555,12 +556,21 @@ namespace boost {
*
* @return and integer with the maximum allowed precision.
*/

unsigned int max_precision() const {
if (this->_maximum_precision == 0) {
return boost::real::real::maximum_precision;
}

return this->_maximum_precision;
switch (this->_kind) {

case KIND::EXPLICIT:
return _explicit_number.max_precision();


case KIND::ALGORITHM:
return _algorithmic_number.max_precision();

case KIND::OPERATION:
return this->_maximum_precision;
}
}

/**
Expand All @@ -573,6 +583,8 @@ namespace boost {
*/
void set_maximum_precision(unsigned int maximum_precision) {
this->_maximum_precision = maximum_precision;
this->_algorithmic_number.set_maximum_precision(maximum_precision);
this->_explicit_number.set_maximum_precision(maximum_precision);
}

/**
Expand Down Expand Up @@ -644,6 +656,7 @@ namespace boost {
this->_rhs_ptr = new real(other);
this->_kind = KIND::OPERATION;
this->_operation = OPERATION::ADDITION;
this->set_maximum_precision(std::max(this->max_precision(), other.max_precision()));
return *this;
}

Expand Down Expand Up @@ -674,6 +687,7 @@ namespace boost {
this->_rhs_ptr = new real(other);
this->_kind = KIND::OPERATION;
this->_operation = OPERATION::SUBTRACT;
this->set_maximum_precision(std::max(this->max_precision(), other.max_precision()));
return *this;
}

Expand Down Expand Up @@ -704,6 +718,7 @@ namespace boost {
this->_rhs_ptr = new real(other);
this->_kind = KIND::OPERATION;
this->_operation = OPERATION::MULTIPLICATION;
this->set_maximum_precision(std::max(this->max_precision(), other.max_precision()));
return *this;
}

Expand Down
12 changes: 8 additions & 4 deletions include/real/real_algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ namespace boost {
* @param exponent - an integer representing the number exponent.
*/
explicit real_algorithm(int (*get_nth_digit)(unsigned int), int exponent)
: _get_nth_digit(get_nth_digit), _exponent(exponent), _positive(true) {}
: _get_nth_digit(get_nth_digit), _exponent(exponent), _positive(true) {
set_maximum_precision(15);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this max precision hardcoded with a 15? It should be the value of boost::real::real::maximum_precision which is the default max precision.

Suggested change
set_maximum_precision(15);
set_maximum_precision(boost::real::real::maximum_precision);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this max precision hardcoded with a 15? It should be the value of boost::real::real::maximum_precision which is the default max precision.

Suggested change
set_maximum_precision(15);
set_maximum_precision(boost::real::real::maximum_precision);

maximum_precision is an uninitialised variable currently and seems to take the value 10. Also, that hardcoded 15 is for algorithm numbers. I don't know what to use as its precision so I just chose 15. Please suggest default max precision for algorithm numbers which might have infinite precision. Or what show max_precision be initialised with for algorithm numbers?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

boost::real::real::maximum_precision and boost::real::real_algorithm::maximum_precision are global variables the user must set at the start of their code to specify the global maximum precision that is ever allowed. If you check the tests, you can notice in test/include/test_helpers.hpp in lines 9 and 10 that those variables are set in 10 and that is why they have that value (It is never hardcoded). With no exceptions, these variable is used as the max precision ever accepted and even for explicit numbers that have more digits than this maximum precision, they will throw a precision_exception.

The max precision is used to allow having some control in the algorithm memory and time complexities.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no...
I never noticed the initialisations in test_helpers which has led to a lot of confusion for me as I couldn't figure out why the precision cap was at 10...
Now it seems like there wasn't any issue in the first place. The precision error I thought was a mistake was due to this max_precision of 10. And then the algorithm precision can just be kept as the max_precision set in test_helpers then.

}

/**
* @brief *Lambda function constructor with exponent and sign:* Creates a boost::real::real_algorithm instance
Expand All @@ -224,7 +226,9 @@ namespace boost {
bool positive)
: _get_nth_digit(get_nth_digit),
_exponent(exponent),
_positive(positive) {}
_positive(positive) {
set_maximum_precision(15);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
set_maximum_precision(15);
set_maximum_precision(boost::real::real::maximum_precision);

}

/**
* @brief Returns the maximum allowed precision, if that precision is reached and an
Expand All @@ -233,7 +237,7 @@ namespace boost {
* @return an integer with the maximum allowed precision.
*/
unsigned int max_precision() const {
return boost::real::real_algorithm::maximum_precision;
return this->_maximum_precision;
}

/**
Expand Down Expand Up @@ -281,7 +285,7 @@ namespace boost {
*/
const_precision_iterator cend() const {
const_precision_iterator it(this);
it.iterate_n_times(boost::real::real_algorithm::maximum_precision - 1);
it.iterate_n_times(this->max_precision() - 1);
return it;
}

Expand Down
2 changes: 1 addition & 1 deletion test/real_algorithm_iterator_unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ TEST_CASE("Iterator cend") {

SECTION("Iterate until the maximum set precision returns the end of the iterator") {

for(int i = 0; i < 9; i++) {
for(int i = 0; i < 14; i++) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for(int i = 0; i < 14; i++) {
for(int i = 0; i < boost::real::real::maximum_precision-1; i++) {

CHECK_FALSE( approximation_it == end_it );
++approximation_it;
}
Expand Down
Loading