Skip to content
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
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ matrix:
include:
- os: linux
dist: trusty
env: CC=gcc-7 CXX=g++-7 BOOST_ROOT=/usr/include/boost
env: CC=gcc-8 CXX=g++-8 BOOST_ROOT=/usr/include/boost
compiler: gcc
before_install:
- sudo -E apt-add-repository -y "ppa:ubuntu-toolchain-r/test"
- sudo apt-get update
- sudo apt-get install g++-7
- sudo apt-get install g++-8
- export BOOST_URL="https://downloads.sourceforge.net/project/boost/boost/1.67.0/boost_1_67_0.tar.gz"
- mkdir -p /tmp/boost
- wget --quiet -O - ${BOOST_URL} | tar --strip-components=1 -xz -C /tmp/boost || exit 1
- cd /tmp/boost/tools/build && ./bootstrap.sh && ./b2 install --prefix=/tmp/b2
- export PATH=/tmp/b2/bin:${PATH}
- cd /tmp/boost && b2 toolset=gcc cxxflags="-std=c++14 ${CXXFLAGS}"
- cd /tmp/boost && b2 toolset=gcc cxxflags="-std=c++17 ${CXXFLAGS}"
install: export CXX="g++-8"
- os: osx
osx_image: xcode9.4
env:
Expand Down
87 changes: 52 additions & 35 deletions include/real/real.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <algorithm>
#include <initializer_list>
#include <utility>
#include <variant>

#include <real/real_exception.hpp>
#include <real/real_helpers.hpp>
Expand Down Expand Up @@ -59,11 +60,8 @@ namespace boost {

KIND _kind;

// Explicit number
real_explicit _explicit_number;

// Algorithmic number
real_algorithm _algorithmic_number;
std::variant<boost::real::real_explicit, boost::real::real_algorithm> _explicit_algorithm_number;
//Stores both explicit and algorithmic numbers

// Composed number
OPERATION _operation;
Expand Down Expand Up @@ -103,11 +101,10 @@ namespace boost {
// Internal number to iterate
real const* _real_ptr = nullptr;

// Explicit number iterator
boost::real::real_explicit::const_precision_iterator _explicit_it;
std::variant<boost::real::real_explicit::const_precision_iterator,
boost::real::real_algorithm::const_precision_iterator> _explicit_algorithm_it;
//Stores both explicit and algorithmic iterators

// Algorithmic number iterator
boost::real::real_algorithm::const_precision_iterator _algorithmic_it;

// If the number is a composition, the const_precision_iterator uses the operand iterators
const_precision_iterator* _lhs_it_ptr = nullptr;
Expand Down Expand Up @@ -303,13 +300,17 @@ namespace boost {
switch (this->_real_ptr->_kind) {

case KIND::EXPLICIT:
this->_explicit_it = this->_real_ptr->_explicit_number.cbegin();
this->approximation_interval = this->_explicit_it.approximation_interval;
this->_explicit_algorithm_it = std::get<boost::real::real_explicit>(
this->_real_ptr->_explicit_algorithm_number).cbegin();
this->approximation_interval = std::get<boost::real::real_explicit::const_precision_iterator>
(this -> _explicit_algorithm_it).approximation_interval;
break;

case KIND::ALGORITHM:
this->_algorithmic_it = this->_real_ptr->_algorithmic_number.cbegin();
this->approximation_interval = this->_algorithmic_it.approximation_interval;
this->_explicit_algorithm_it = std::get<boost::real::real_algorithm>
(this->_real_ptr->_explicit_algorithm_number).cbegin();
this->approximation_interval = std::get<boost::real::real_algorithm::const_precision_iterator>
(this->_explicit_algorithm_it).approximation_interval;
break;

case KIND::OPERATION:
Expand Down Expand Up @@ -339,20 +340,27 @@ namespace boost {

case KIND::EXPLICIT:
if (cend) {
this->_explicit_it = this->_real_ptr->_explicit_number.cend();
this->_explicit_algorithm_it = std::get<boost::real::real_explicit>
(this->_real_ptr->_explicit_algorithm_number).cend();
} else {
this->_explicit_it = this->_real_ptr->_explicit_number.cbegin();
this->_explicit_algorithm_it=
std::get<boost::real::real_explicit>
(this->_real_ptr->_explicit_algorithm_number).cbegin();
}
this->approximation_interval = this->_explicit_it.approximation_interval;
this->approximation_interval = std::get<boost::real::real_explicit::const_precision_iterator>
(this->_explicit_algorithm_it).approximation_interval;
break;

case KIND::ALGORITHM:
if (cend) {
this->_algorithmic_it = this->_real_ptr->_algorithmic_number.cend();
this->_explicit_algorithm_it = std::get<boost::real::real_algorithm>(
this->_real_ptr->_explicit_algorithm_number).cend();
} else {
this->_algorithmic_it = this->_real_ptr->_algorithmic_number.cbegin();
this->_explicit_algorithm_it = std::get<boost::real::real_algorithm>
(this->_real_ptr->_explicit_algorithm_number).cbegin();
}
this->approximation_interval = this->_algorithmic_it.approximation_interval;
this->approximation_interval = std::get<boost::real::real_algorithm::const_precision_iterator>
(this->_explicit_algorithm_it).approximation_interval;
break;

case KIND::OPERATION:
Expand All @@ -372,13 +380,17 @@ namespace boost {
switch (this->_real_ptr->_kind) {

case KIND::EXPLICIT:
++this->_explicit_it;
this->approximation_interval = this->_explicit_it.approximation_interval;
++(std::get<boost::real::real_explicit::const_precision_iterator>
(this->_explicit_algorithm_it));
this->approximation_interval = std::get<boost::real::real_explicit::const_precision_iterator>
(this->_explicit_algorithm_it).approximation_interval;
break;

case KIND::ALGORITHM:
++this->_algorithmic_it;
this->approximation_interval = this->_algorithmic_it.approximation_interval;
++(std::get<boost::real::real_algorithm::const_precision_iterator>
(this->_explicit_algorithm_it));
this->approximation_interval = std::get<boost::real::real_algorithm::const_precision_iterator>
(this->_explicit_algorithm_it).approximation_interval;
break;

case KIND::OPERATION:
Expand Down Expand Up @@ -440,8 +452,7 @@ namespace boost {
*/
real(const real& other) :
_kind(other._kind),
_explicit_number(other._explicit_number),
_algorithmic_number(other._algorithmic_number),
_explicit_algorithm_number(other._explicit_algorithm_number),
_operation(other._operation) { this->copy_operands(other); };

/**
Expand All @@ -454,7 +465,7 @@ namespace boost {
* @throws boost::real::invalid_string_number exception
*/
real(const std::string& number)
: _kind(KIND::EXPLICIT), _explicit_number(number) {}
: _kind(KIND::EXPLICIT), _explicit_algorithm_number(std::in_place_type<boost::real::real_explicit>, number) {}

/**
* @brief *Initializer list constructor:* Creates a boost::real::real_explicit instance
Expand All @@ -464,7 +475,8 @@ namespace boost {
* @param digits - a initializer_list<int> that represents the number digits.
*/
real(std::initializer_list<int> digits)
: _kind(KIND::EXPLICIT), _explicit_number(digits, digits.size()) {}
: _kind(KIND::EXPLICIT), _explicit_algorithm_number
(std::in_place_type<boost::real::real_explicit>, digits, digits.size()) {}


/**
Expand All @@ -478,7 +490,8 @@ namespace boost {
* the number is positive, otherwise is negative.
*/
real(std::initializer_list<int> digits, bool positive)
: _kind(KIND::EXPLICIT), _explicit_number(digits, digits.size(), positive) {}
: _kind(KIND::EXPLICIT), _explicit_algorithm_number
(std::in_place_type<boost::real::real_explicit>, digits, digits.size(), positive) {}

/**
* @brief *Initializer list constructor with exponent:* Creates a boost::real::real
Expand All @@ -490,7 +503,8 @@ namespace boost {
* @param exponent - an integer representing the number exponent.
*/
real(std::initializer_list<int> digits, int exponent)
: _kind(KIND::EXPLICIT), _explicit_number(digits, exponent) {};
: _kind(KIND::EXPLICIT), _explicit_algorithm_number
(std::in_place_type<boost::real::real_explicit>, digits, exponent) {};

/**
* @brief *Initializer list constructor with exponent and sign:* Creates a boost::real::real instance
Expand All @@ -504,7 +518,8 @@ namespace boost {
* the number is positive, otherwise is negative.
*/
real(std::initializer_list<int> digits, int exponent, bool positive)
: _kind(KIND::EXPLICIT), _explicit_number(digits, exponent, positive) {};
: _kind(KIND::EXPLICIT), _explicit_algorithm_number
(std::in_place_type<boost::real::real_explicit>, digits, exponent, positive) {};

/**
* @brief *Lambda function constructor with exponent:* Creates a boost::real::real
Expand All @@ -517,7 +532,8 @@ namespace boost {
* @param exponent - an integer representing the number exponent.
*/
real(int (*get_nth_digit)(unsigned int), int exponent)
: _kind(KIND::ALGORITHM), _algorithmic_number(get_nth_digit, exponent) {}
: _kind(KIND::ALGORITHM), _explicit_algorithm_number
(std::in_place_type<boost::real::real_algorithm>, get_nth_digit, exponent) {}

/**
* @brief *Lambda function constructor with exponent and sign:* Creates a boost::real::real instance
Expand All @@ -536,7 +552,8 @@ namespace boost {
int exponent,
bool positive)
: _kind(KIND::ALGORITHM),
_algorithmic_number(get_nth_digit, exponent, positive) {}
_explicit_algorithm_number
(std::in_place_type<boost::real::real_algorithm>, get_nth_digit, exponent, positive) {}

/**
* @brief *Default destructor:* If the number is an operator, the destructor destroys its operands.
Expand Down Expand Up @@ -616,11 +633,11 @@ namespace boost {
switch (this->_kind) {

case KIND::EXPLICIT:
result = this->_explicit_number[n];
result = std::get<boost::real::real_explicit>(this -> _explicit_algorithm_number)[n];
break;

case KIND::ALGORITHM:
result = this->_algorithmic_number[n];
result = std::get<boost::real::real_algorithm>(this->_explicit_algorithm_number)[n];
break;

case KIND::OPERATION:
Expand Down Expand Up @@ -729,7 +746,7 @@ namespace boost {
*/
real& operator=(const real& other) {
this->_kind = other._kind;
this->_explicit_number = other._explicit_number;
this->_explicit_algorithm_number = other._explicit_algorithm_number;
this->_operation = other._operation;
this->copy_operands(other);
return *this;
Expand Down